3、SpringBoot+Mybatis整合------主鍵回填


開發工具:STS

代碼下載鏈接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870786eb3f1a1a5b629

前言:

當我們插入一個一對一、一對多、多對多的關系數據時,往往需要分表插入,那么我們可能需要獲取自動生成的主鍵用於后面的插入操作,因此今天來介紹下mybatis里的主鍵回填。

一、代碼實現:

1.數據操作層接口mapper:

 1 package com.xm.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.xm.pojo.Student;
 6 
 7 public interface StudentMapper {
 8 
 9     /**
10      * 根據id查詢
11      * @param id
12      * @return
13      */
14     public Student getById(Integer id);
15     
16     /**
17      * 查詢全部
18      * @return
19      */
20     public List<Student> list();
21     
22     /**
23      * 插入
24      * @param student
25      */
26     public int insert(Student student);
27     /**
28      * 主鍵回填的插入
29      * @param student
30      * @return
31      */
32     public int insertToId(Student student);
33     
34     /**
35      * 根據student的id修改
36      * @param student
37      */
38     public void update(Student student);
39     
40     /**
41      * 根據id刪除
42      * @param id
43      */
44     public void delete(Integer id);
45     
46     
47 }
StudentMapper.java

2.關系映射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 <mapper namespace="com.xm.mapper.StudentMapper">
 4 
 5     <!-- 根據id查詢 -->
 6     <select id="getById" parameterType="int" resultType="student">
 7     select * from student where id=#{id}
 8     </select>
 9     <!-- 查詢所有 -->
10     <select id="list" parameterType="int" resultType="student">
11     select * from student
12     </select>
13     
14     <!-- 插入一個學生 -->
15     <insert id="insert" parameterType="student">
16     insert into student(name) values(#{name})
17     </insert>
18     <!-- 主鍵回填的插入 -->
19     <insert id="insertToId" parameterType="student" useGeneratedKeys="true" keyProperty="id">
20     insert into student(name) values(#{name})
21     </insert>
22     
23     <!-- 根據id修改學生信息 -->
24     <update id="update" parameterType="student">
25     update student set name=#{name} where id=#{id}
26     </update>
27     
28     <!-- 根據id刪除學生 -->
29     <delete id="delete" parameterType="int">
30     delete  from student where id=#{id}
31     </delete>
32 </mapper>
StudentMapper.xml

3.測試類:

 1 package com.xm;
 2 
 3 import org.junit.Test;
 4 import org.junit.runner.RunWith;
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.boot.test.context.SpringBootTest;
 7 import org.springframework.test.context.junit4.SpringRunner;
 8 
 9 import com.xm.mapper.StudentMapper;
10 import com.xm.pojo.Student;
11 
12 @RunWith(SpringRunner.class)
13 @SpringBootTest
14 public class StudentTest {
15     @Autowired
16     private StudentMapper studentMapper;
17     
18     @Test
19     public void insertStudent() {
20         Student student = new Student();
21         student.setName("張大薩");
22         int a= studentMapper.insert(student);
23         System.out.println(a);
24         System.out.println(student.getId());
25         
26         a= studentMapper.insertToId(student);
27         System.out.println(a);
28         System.out.println(student.getId());
29         
30     }
31     
32 
33 }
StudentTest.java

二、測試結果:

主鍵自動補充到student中,無需另外獲取

2018-06-19


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM