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
3、SpringBoot+Mybatis整合------主键回填的更多相关文章
- MyBatis中主键回填的两种实现方式
主键回填其实是一个非常常见的需求,特别是在数据添加的过程中,我们经常需要添加完数据之后,需要获取刚刚添加的数据 id,无论是 Jdbc 还是各种各样的数据库框架都对此提供了相关的支持,本文我就来和和大 ...
- MyBatis 示例-主键回填
测试类:com.yjw.demo.PrimaryKeyTest 自增长列 数据库表的主键为自增长列,在写业务代码的时候,经常需要在表中新增一条数据后,能获得这条数据的主键 ID,MyBatis 提供了 ...
- 4、SpringBoot+Mybatis整合------一对多
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd99020 ...
- MyBatis返回主键,MyBatis Insert操作返回主键
MyBatis返回主键,MyBatis Insert操作返回主键 >>>>>>>>>>>>>>>>> ...
- SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版) ================================ ©Copyright 蕃薯耀 2 ...
- 2017.9.15 mybatis批量插入后实现主键回填
参考来自:mybatis mysql 批量insert 返回主键 注意:必须要在mybatis3.3.1及其以上才能实现. 1.service List branchEntryList = (Arra ...
- 1、SpringBoot+Mybatis整合------简单CRUD的实现
编译工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis01/commit/b757cd9bfa4e2de551b2e9 ...
- postgresql + mybatis insert主键自增方法
postgresql + mybatis插入记录时设置自增主键方法: 一.数据库设置主键自增 1.数据库中id字段选择serial4类型后,会在默认值中生成 nextval('app_id_seq': ...
- mybatis plus 主键生成 Twitter雪花算法 id 及修改id为字符型
mybatis plus配置主键生成策略为2,就是 使用Twitter雪花算法 生成id spring boot中配置为: GlobalConfiguration conf = new GlobalC ...
随机推荐
- Flag-2019上半年
1.坚持做博客记录,把日常学习到的技能以日记的形势进行呈现,每周进行总结: 2.2月底完成吴老师的机器学习视频课程: 3.6月底完成吴老师的深度学习视频课程: 相信自己,可以的,半年后见!
- C# 判断字符串为空(长度为0),或者是null(没有new)
string str = null; if (string.IsNullOrWhiteSpace(str)) { MessageBox.Show("字符串为null"); } ) ...
- Windows 10家庭版升级到专业版,系统蓝屏
Log Name: SystemSource: Microsoft-Windows-DistributedCOMDate: 9/9/2018 7:56:57 PMEvent ID: 10016Task ...
- CSS选择器笔记,element element和element > element 的区别
看官方解释 element element 例子: div p 官方解释:div内部所有的p元素 就是说 只要p在div内部.如果 p在span内部,span在div内部,p也算在div内部 < ...
- windows下端口转发 netsh
添加映射表: netsh interface portproxy add v4tov4 listenport=(监听端口) connectaddress=(虚机IP) connectport=(虚机端 ...
- 用java实现删除文件夹里的所有文件
package com.org.improve.contact; import java.io.File; public class DeletePaper { /** * @param args * ...
- Linux 下 zip 文件解压乱码解决方案,ubuntu16.10亲测可用
文章来源: https://www.zhihu.com/question/20523036 今天邮件中收到了一个压缩文件,解压后却是乱码,从网上也找了几个方法,目前这个方法还是比较可靠的,如下所示: ...
- [巩固C#] 二、什么是反射、反射可以做些什么
阅读目录 关闭 什么是反射,反射能干嘛? 获取类型的相关信息 获取类型本身信息(命名空间名.全名.是否是抽象.是否是类..... 获取类型成员信息(通过Tyep中的方法GetMembers ...
- 斗鱼扩展--DouyuRoom使用说明(十四)
1.从 https://pan.baidu.com/s/1yBfZFtcakbDxmyas0VCpRw 下载 DouyuRoom.zip 然后解压到一个目录,我是放在C盘根目录下的,你们随意.然后解压 ...
- Java编译及装载
Java类加载机制 JVM将类加载过程划分为三个步骤:装载.链接和初始化. 装载(Load):装载过程负责找到二进制字节码并加载至JVM中,JVM通过类的全限定名(com.bluedavy. Hell ...