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

开发工具:STS

代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/tree/d68efe51774fc4d96e5c6870786eb3f1a1a5b629

前言:

当我们插入一个一对一、一对多、多对多的关系数据时,往往需要分表插入,那么我们可能需要获取自动生成的主键用于后面的插入操作,因此今天来介绍下mybatis里的主键回填。

一、代码实现:

1.数据操作层接口mapper:

图片描述

图片描述

 package com.xm.mapper;
 
 import java.util.List;
 
 import com.xm.pojo.Student;
 
 public interface StudentMapper {
 
     /**
      * 根据id查询
      * @param id
      * @return
      */
     public Student getById(Integer id);
     
     /**
      * 查询全部
      * @return
      */
     public List<Student> list();
     
     /**
      * 插入
      * @param student
      */
     public int insert(Student student);
     /**
      * 主键回填的插入
      * @param student
      * @return
      */
     public int insertToId(Student student);
     
     /**
      * 根据student的id修改
      * @param student
      */
     public void update(Student student);
     
     /**
      * 根据id删除
      * @param id
      */
     public void delete(Integer id);
     
     
 }

StudentMapper.java

2.关系映射xml:

图片描述

图片描述

 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
 <mapper namespace="com.xm.mapper.StudentMapper">
 
     <!-- 根据id查询 -->
     <select id="getById" parameterType="int" resultType="student">
     select * from student where id=#{id}
     </select>
     <!-- 查询所有 -->
     <select id="list" parameterType="int" resultType="student">
     select * from student
     </select>
     
     <!-- 插入一个学生 -->
     <insert id="insert" parameterType="student">
     insert into student(name) values(#{name})
     </insert>
     <!-- 主键回填的插入 -->
     <insert id="insertToId" parameterType="student" useGeneratedKeys="true" keyProperty="id">
     insert into student(name) values(#{name})
     </insert>
     
     <!-- 根据id修改学生信息 -->
     <update id="update" parameterType="student">
     update student set name=#{name} where id=#{id}
     </update>
     
     <!-- 根据id删除学生 -->
     <delete id="delete" parameterType="int">
     delete  from student where id=#{id}
     </delete>
 </mapper>

StudentMapper.xml

3.测试类:

图片描述

图片描述

 package com.xm;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
 import com.xm.mapper.StudentMapper;
 import com.xm.pojo.Student;
 
 @RunWith(SpringRunner.class)
 @SpringBootTest
 public class StudentTest {
     @Autowired
     private StudentMapper studentMapper;
     
     @Test
     public void insertStudent() {
         Student student = new Student();
         student.setName("张大萨");
         int a= studentMapper.insert(student);
         System.out.println(a);
         System.out.println(student.getId());
         
         a= studentMapper.insertToId(student);
         System.out.println(a);
         System.out.println(student.getId());
         
     }
     
 
 }

StudentTest.java

二、测试结果:

主键自动补充到student中,无需另外获取

图片描述

2018-06-19