运行 spring 引导中的本机插入查询

Running a native insert query in spring boot

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

查询:

@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().

插入语句不return插入的记录。由于您希望列表被 returned executeQuery() 由 Spring 执行。你想要的是 executeUpdate() 的执行,它只对 void 方法执行。

另见 Cannot issue data manipulation statements with executeQuery()