mapper
Add two properties: useGeneratedKeys="true" keyProperty="id"
<insert id="add" parameterType="Project" useGeneratedKeys="true" keyProperty="id">
insert into project (name, description, level, picture, deleted, create_time, update_time, version)
value (#{name}, #{description}, #{level}, #{picture}, #{deleted}, #{createTime}, #{updateTime},#{version} );
</insert>
test
@Test
public void ddTest(){
Project project = new Project();
project.setName("计算机网络");
project.setDeleted(0);
project.setCreateTime(new Date());
project.setDescription("这是一个描述");
project.setLevel(1);
project.setPicture("www.baidu.com");
project.setUpdateTime(new Date());
project.setVersion(1);
boolean add = projectService.add(project);
System.out.println(add);
}
Error:
The reason is that although mybatis primary key auto-increment is turned on, the database does not buy it, so it needs to be manually set to enable auto-increment