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 ...
随机推荐
- 多线程编程_控制并发线程数的Semaphore
简介 Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源.很多年以来,我都觉得从字面上很难理解Semaphore所表达的含义,只能把它比作是 ...
- hdu 2222 ac自动机更新模板 for onSite contest
http://acm.split.hdu.edu.cn/showproblem.php?pid=2222 #include <cstdio> #include <cstdlib> ...
- Linux下的NFS快速配置教程与安全策略
[51CTO专稿]在Linux下实现文件共享有多种方式,NFS就是其中之一.网络文件系统(NFS)协议是由Sun MicroSystem在20世纪80年代为了提供对共享文件的远程访问而设计和实现的.该 ...
- web.xml文件配置详解以及实例说明
1.web.xml学名叫部署描述符文件,是在Servlet规范中定义的,是web应用的配置文件. 2.部署描述符文件就像所有XML文件一样,必须以一个XML头开始.这个头声明可以使用的XML版本并给出 ...
- golang中并发的相关知识
golang中done channel理解:https://segmentfault.com/a/1190000006261218 golang并发模型之使用Context:https://segme ...
- php一种面向对象的语言,那么什么是面向对象呢?
php一种面向对象的语言,那么什么是面向对象呢? 传统的面向过程的编程思想: 相信很多人第一次接触编程都是c语言,c语言就是非常典型的面向过程的编程语言,将要实现的功能描述为一个从开始到结束的连续的“ ...
- 让你的sharepoint2013具有EMS快递查询的功能
<iframe name="kuaidi100" src="http://www.kuaidi100.com/frame/app/index2.html" ...
- C++ Knowledge series 3
Programming language evolves always along with Compiler's evolvement The Semantics of Data The size ...
- C#对bat脚本文件的操作示例
实现C#操作bat脚本文件 using System;using System.Collections.Generic;using System.ComponentModel;using System ...
- Spring Boot项目Circular view path问题解决
使用Spring Boot创建Spring MVC项目,访问url请求出现问题:Circular view path 1.问题描述 控制台打印: javax.servlet.ServletExcept ...