如何在 XML 风格中使用 MyBatis-SpringBoot-MySQL 执行正确的 SQL INSERT?
How to Perform proper SQL INSERT using MyBatis-SpringBoot-MySQL in XML-style?
我很难在 SQL 数据库中插入新数据。有人可以检查我的 mapper.xml 和 mapper.java 吗?
Actor.java POJO
@Component
public class Actor {
private static final long serialVersionUID = 1L;
private Integer actor_id;
private String first_name;
private String last_name;
private String last_update;
//Getters and Setters
}
ActorMapper.xml
<insert id="insertNewActor" parameterClass="com.helios.mybatissakila.model.Actor"
useGeneratedKeys="true" keyColumn="actor_id" keyProperty="actor_id">
insert into actor (first_name,last_name,last_update)
values (#{first_name},#{last_name},#{last_update})
</insert>
Mapper.java方法
List <Actor> insertNewActor(Map<String, Object> actor);
RESTController 方法
@PostMapping(value="/actors", consumes = "application/json", produces = "application/json")
public List<Actor> insertNewActor(@RequestBody Map<String, Object> actor) {
return actorMapper.insertNewActor(actor);
}
数据库table结构:
此外,请注意我是否应该在 mapper.xml 中使用 parameterClass 或 parameterType?
首先移除不需要的@Component Actor。
然后改变 mapper.java ,像这样
int insertNewActor(Actor actor);
如果失败,显示错误
我很难在 SQL 数据库中插入新数据。有人可以检查我的 mapper.xml 和 mapper.java 吗?
Actor.java POJO
@Component
public class Actor {
private static final long serialVersionUID = 1L;
private Integer actor_id;
private String first_name;
private String last_name;
private String last_update;
//Getters and Setters
}
ActorMapper.xml
<insert id="insertNewActor" parameterClass="com.helios.mybatissakila.model.Actor"
useGeneratedKeys="true" keyColumn="actor_id" keyProperty="actor_id">
insert into actor (first_name,last_name,last_update)
values (#{first_name},#{last_name},#{last_update})
</insert>
Mapper.java方法
List <Actor> insertNewActor(Map<String, Object> actor);
RESTController 方法
@PostMapping(value="/actors", consumes = "application/json", produces = "application/json")
public List<Actor> insertNewActor(@RequestBody Map<String, Object> actor) {
return actorMapper.insertNewActor(actor);
}
数据库table结构:
此外,请注意我是否应该在 mapper.xml 中使用 parameterClass 或 parameterType?
首先移除不需要的@Component Actor。 然后改变 mapper.java ,像这样
int insertNewActor(Actor actor);
如果失败,显示错误