Development tools: STS code download Link: https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870786eb3f1a1a5b629 preface:
When we insert a pair of one or one-to-many, many-to-many relationship data, we often need to insert the table, then we may need to get the auto-generated primary key for the subsequent insert operation, so today to introduce the next mybatis in the primary key backfill.
First, the code implementation: 1. Data manipulation Layer Interface Mapper:
1 PackageCom.xm.mapper;2 3 Importjava.util.List;4 5 Importcom.xm.pojo.Student;6 7 Public InterfaceStudentmapper {8 9 /**Ten * Search by ID One * @paramID A * @return - */ - PublicStudent getById (Integer ID); the - /** - * Search All - * @return + */ - PublicList<student>list (); + A /** at * Insert - * @paramStudent - */ - Public intInsert (Student Student); - /** - * Insert of PRIMARY key backfill in * @paramStudent - * @return to */ + Public intinserttoid (Student Student); - the /** * * Modified according to student's ID $ * @paramStudentPanax Notoginseng */ - Public voidUpdate (Student Student); the + /** A * deleted by ID the * @paramID + */ - Public voidDelete (Integer ID); $ $ -}
Studentmapper.java2. Relational mapping xml:
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd ">3 <Mappernamespace= "Com.xm.mapper.StudentMapper">4 5 <!--Query by ID -6 <SelectID= "GetById"ParameterType= "int"Resulttype= "Student">7 SELECT * from student where Id=#{id}8 </Select>9 <!--Query All -Ten <SelectID= "List"ParameterType= "int"Resulttype= "Student"> One SELECT * FROM Student A </Select> - - <!--Insert a student - the <InsertID= "Insert"ParameterType= "Student"> - INSERT into student (name) values (#{name}) - </Insert> - <!--insert for primary key backfill - + <InsertID= "Inserttoid"ParameterType= "Student"Usegeneratedkeys= "true"Keyproperty= "id"> - INSERT into student (name) values (#{name}) + </Insert> A at <!--Modify student information by ID - - <UpdateID= "Update"ParameterType= "Student"> - update student set Name=#{name} where Id=#{id} - </Update> - - <!--Delete students by ID - in <DeleteID= "Delete"ParameterType= "int"> - Delete from student where Id=#{id} to </Delete> + </Mapper>
Studentmapper.xml3. Test class:
1 Packagecom.xm;2 3 Importorg.junit.Test;4 ImportOrg.junit.runner.RunWith;5 Importorg.springframework.beans.factory.annotation.Autowired;6 Importorg.springframework.boot.test.context.SpringBootTest;7 ImportOrg.springframework.test.context.junit4.SpringRunner;8 9 ImportCom.xm.mapper.StudentMapper;Ten Importcom.xm.pojo.Student; One A@RunWith (Springrunner.class) - @SpringBootTest - Public classStudenttest { the @Autowired - PrivateStudentmapper Studentmapper; - - @Test + Public voidinsertstudent () { -Student Student =NewStudent (); +Student.setname ("Zhang Dasa"); A intA=Studentmapper.insert (student); at System.out.println (a); - System.out.println (Student.getid ()); - -A=studentmapper.inserttoid (student); - System.out.println (a); - System.out.println (Student.getid ()); in - } to + -}
Studenttest.javaSecond, the test results:
The primary key is automatically added to the student, eliminating the need for additional
2018-06-19
3, Springboot+mybatis integration------PRIMARY Key Backfill