0

我正在尝试在 Spring Boot 中运行一个简单的插入查询,但我无法这样做。

询问:

@Modifying
    @Query(value = "insert into Local(userId,name,address,pin) VALUES (:userId,:name,:address,:pin)", nativeQuery = true)

    List < Local > insertattributes(@Param("userId")String userId,@Param("address") String address ,@Param("name")String name,@Param("pin") String pin);

控制器:

@RequestMapping("insertattributes/{userId}/{name}/{address}/{pin}")
    @ResponseBody
    public List < Local > insertAttributes(@PathVariable String userId, @PathVariable String name, @PathVariable String address, @PathVariable String pin) {

        return localService.insertattributes(userId,name,address,pin);
    }

错误:

o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: S1009
o.h.engine.jdbc.spi.SqlExceptionHelper   : Can not issue data manipulation statements with executeQuery().
[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause

java.sql.SQLException: Can not issue data manipulation statements with executeQuery().
4

1 回答 1

1

插入语句不返回插入的记录。由于您期望返回的列表executeQuery()由 Spring 执行。您想要的executeUpdate()是仅对 void 方法执行的执行。

另请参阅无法使用 executeQuery() 发出数据操作语句

于 2018-04-06T07:00:09.723 回答