开发工具: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

3、SpringBoot+Mybatis整合------主键回填的更多相关文章

  1. MyBatis中主键回填的两种实现方式

    主键回填其实是一个非常常见的需求,特别是在数据添加的过程中,我们经常需要添加完数据之后,需要获取刚刚添加的数据 id,无论是 Jdbc 还是各种各样的数据库框架都对此提供了相关的支持,本文我就来和和大 ...

  2. MyBatis 示例-主键回填

    测试类:com.yjw.demo.PrimaryKeyTest 自增长列 数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了 ...

  3. 4、SpringBoot+Mybatis整合------一对多

    开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd99020 ...

  4. MyBatis返回主键,MyBatis Insert操作返回主键

    MyBatis返回主键,MyBatis Insert操作返回主键 >>>>>>>>>>>>>>>>> ...

  5. SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

    SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...

  6. 2017.9.15 mybatis批量插入后实现主键回填

    参考来自:mybatis mysql 批量insert 返回主键 注意:必须要在mybatis3.3.1及其以上才能实现. 1.service List branchEntryList = (Arra ...

  7. 1、SpringBoot+Mybatis整合------简单CRUD的实现

    编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...

  8. postgresql + mybatis insert主键自增方法

    postgresql + mybatis插入记录时设置自增主键方法: 一.数据库设置主键自增 1.数据库中id字段选择serial4类型后,会在默认值中生成 nextval('app_id_seq': ...

  9. mybatis plus 主键生成 Twitter雪花算法 id 及修改id为字符型

    mybatis plus配置主键生成策略为2,就是 使用Twitter雪花算法 生成id spring boot中配置为: GlobalConfiguration conf = new GlobalC ...

随机推荐

  1. python 缺失值的处理

  2. STM32F3 浮点运算使用

    在keil中使用浮点运算的步骤:在程序中包含#include <math.h>

  3. Play on Words UVA - 10129 (欧拉回路)

    题目链接:https://vjudge.net/problem/UVA-10129 题目大意:输入N  代表有n个字符串  每个字符串最长1000  要求你把所有的字符串连成一个序列  每个字符串的第 ...

  4. Trees on the level UVA - 122 (二叉树的层次遍历)

    题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出 ...

  5. 8597 石子划分问题 dpdp,只考虑第一次即可

    8597 石子划分问题 时间限制:500MS  内存限制:1000K提交次数:155 通过次数:53 题型: 编程题   语言: G++;GCC;VC Description 给定n个石子,其重量分别 ...

  6. Django重新整理4---ModelForm-set(批量处理数据)

    1. #引用modelformset from django.forms.models import modelformset_factory #必须继承forms.ModelForm! class ...

  7. js 页面按钮提交后 创建显示loading div 操作完成后 再隐藏或删除 进度div

    预期效果: 1.点击Save按钮,创建及显示loading div框 2.Save操作完成后,再删除loading 及弹出提示结果 <html> <head> </hea ...

  8. hdu 3530 区间和在一定范围内最长区间

    http://acm.hust.edu.cn/vjudge/problem/11253 这题要找到区间和在[m,k]范围内的最长区间 用两个单调序列保存区间最大值和最小值.当最大值-最小值>k时 ...

  9. FTPUtil 多文件上传参考代码

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  10. 缩小javascript文件大小之缩编、混淆

    写前端的相信都遇到过要提高网页的性能,其中javascript文件越小,浏览器的下载速度面对文件的读取和解析就更快.而一般我们在开发又需要一定的代码规范来使我们的代码更加的容易维护和读懂,但是大量空格 ...