我们在进行数据插入时候,很多情况需要返回id,进而进行之后的处理,比如:传回前端之后进行更改,或者作为外键对其他表写入数据。
一般情况下都可以将需要插入表的内容提前处理到一个实体类中,但是可以偷懒直接传入在在xml中通过判断处理。下面记录几种插入数据返回id的情况(新标签中打开图片可以看得更清楚哦)
1. 首先我们先创建一个测试使用的表。只是为了举常见的学生 - 成绩例子,正常情况,学生个人信息和成绩是要分开两张表的,多提一句现在已经很少使用建外键了。
图1 学生表
2. 单个实体类插入数据返回id。--传入对象,在xml中指明自增和映射主键,,在执行完之后,在我们注入的那个类中直接取就可以取到id,这里是将整个对象返回到前端。
图2 单个实体类接收返回id
图3 测试接口和返回内容
附件:各个文件的源码:

1 package com.example.demo.controller; 2 3 import com.example.demo.domain.Result; 4 import com.example.demo.entity.Student; 5 import com.example.demo.service.StudentService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.RequestBody; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import java.math.BigDecimal; 12 import java.util.Map; 13 14 /** 15 * @author Codorld 16 * @date 2021/05/05 14:08 17 */ 18 19 @RestController 20 public class StudentController { 21 22 @Autowired 23 StudentService studentService; 24 25 // 将学生的的信息存入,并且将学生的id传回 26 @RequestMapping("inertStu") 27 public Result insertStudent(@RequestBody Map params) { 28 int yuwen_score = Integer.parseInt(params.get("yuwen_score").toString()); 29 int shuxue_score = Integer.parseInt(params.get("shuxue_score").toString()); 30 BigDecimal avg_score = BigDecimal.valueOf((double) (yuwen_score+shuxue_score)/2).setScale(2, BigDecimal.ROUND_HALF_UP); 31 Student student = new Student(params.get("stu_name").toString(), Integer.parseInt(params.get("stu_gender").toString()), 32 yuwen_score, shuxue_score, avg_score); 33 34 studentService.insertStudent(student); 35 return new Result("200", "插入学生信息成功", student); 36 } 37 }

1 package com.example.demo.entity; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 import java.math.BigDecimal; 8 9 /** 10 * @author Codorld 11 * @date 2021/05/05 14:01 12 */ 13 14 @NoArgsConstructor 15 @AllArgsConstructor 16 @Data 17 public class Student { 18 19 // 学生id - 学号 20 private int stu_id; 21 22 // 学生姓名 23 private String stu_name; 24 25 // 学生性别 1.男 2.女 26 private int stu_gender; 27 28 // 语文成绩 29 private int yuwen_socre; 30 31 // 数学成绩 32 private int shuxue_socre; 33 34 // 平均分(保留小数点两位) 35 private BigDecimal avg_score; 36 37 // 只姓名、性别、语文、数学和平均成绩的构造方法 38 public Student(String stu_name, int stu_gender, int yuwen_socre, int shuxue_socre, BigDecimal avg_score) { 39 this.stu_name = stu_name; 40 this.stu_gender = stu_gender; 41 this.yuwen_socre = yuwen_socre; 42 this.shuxue_socre = shuxue_socre; 43 this.avg_score = avg_score; 44 } 45 46 }

1 package com.example.demo.mapper; 2 3 import com.example.demo.entity.Student; 4 import org.apache.ibatis.annotations.Mapper; 5 6 /** 7 * @author Codorld 8 * @date 2021/05/05 14:18 9 */ 10 11 @Mapper 12 public interface StudentMapper { 13 14 // 插入一条学生数据返回学生id 15 void insertStudent(Student student); 16 17 }

1 package com.example.demo.service; 2 3 import com.example.demo.entity.Student; 4 5 /** 6 * @author Codorld 7 * @date 2021/05/05 14:11 8 */ 9 10 public interface StudentService { 11 12 // 插入学生数据返回id 13 void insertStudent(Student student); 14 15 }

1 package com.example.demo.service.impl; 2 3 import com.example.demo.entity.Student; 4 import com.example.demo.mapper.StudentMapper; 5 import com.example.demo.service.StudentService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 /** 10 * @author Codorld 11 * @date 2021/05/05 14:13 12 */ 13 14 @Service("studentService") 15 public class StudentServiceImpl implements StudentService { 16 17 @Autowired 18 StudentMapper studentMapper; 19 20 @Override 21 public void insertStudent(Student student) { 22 studentMapper.insertStudent(student); 23 } 24 }


1 package com.example.demo; 2 3 import org.mybatis.spring.annotation.MapperScan; 4 import org.springframework.boot.SpringApplication; 5 import org.springframework.boot.autoconfigure.SpringBootApplication; 6 7 @SpringBootApplication 8 @MapperScan("com.example.demo.mapper") 9 public class DemoApplication { 10 public static void main(String[] args) { 11 SpringApplication.run(DemoApplication.class, args); 12 13 } 14 15 }

1 # Web Server 配置 2 server: 3 port: 9091 4 servlet: 5 context-path: /demoBlog 6 session: 7 timeout: 60m 8 9 # MyBatis配置 10 Spring: 11 datasource: 12 url: jdbc:mysql://localhost:3306/demoblog?allowMultiQueries=true 13 username: root 14 password: 123456 15 driver-class-name: com.mysql.cj.jdbc.Driver 16 17 mybatis: 18 #mapper 19 mapper-locations: classpath:mapper/*.xml 20 type-aliases-package: com.example.demo.entity 21 22 #sql - 打印sql语句,推荐安装一个插件:mybatis log 23 logging: 24 level: 25 com: 26 example: 27 demo: 28 mapper: debug

1 package com.example.demo.domain; 2 3 import lombok.AllArgsConstructor; 4 import lombok.Data; 5 import lombok.NoArgsConstructor; 6 7 /** 8 * @author Codorld 9 * @date 2021/05/05 14:15 10 */ 11 12 @AllArgsConstructor 13 @NoArgsConstructor 14 @Data 15 public class Result { 16 private String code; 17 private String msg; 18 private Object data; 19 20 public Result(String code, String msg) { 21 this.code = code; 22 this.msg = msg; 23 } 24 }
3. 多参数情况:只是在xml中取值有一点不同,同时不指定参数类型即可。
对上面内容进行修改:
①mapper和service中加入平均值的参数
②student中添加出去平均值的构造函数
③在controller中构建student时候使用②中的构造函数
④xml中使用多参数取值方法,即使用#{student.x}取值,其他参数还是直接使用#{avg_score}
图4 多参数返回id时候的情况
图5 测试结果
注:从返回结果中可以看到只会映射id,不是表中所有内容都映射到类中。
附件:

1 package com.example.demo.controller; 2 3 import com.example.demo.domain.Result; 4 import com.example.demo.entity.Student; 5 import com.example.demo.service.StudentService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.RequestBody; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import java.math.BigDecimal; 12 import java.util.Map; 13 14 /** 15 * @author Codorld 16 * @date 2021/05/05 14:08 17 */ 18 19 @RestController 20 public class StudentController { 21 22 @Autowired 23 StudentService studentService; 24 25 // 将学生的的信息存入,并且将学生的id传回 26 @RequestMapping("inertStu") 27 public Result insertStudent(@RequestBody Map params) { 28 int yuwen_score = Integer.parseInt(params.get("yuwen_score").toString()); 29 int shuxue_score = Integer.parseInt(params.get("shuxue_score").toString()); 30 BigDecimal avg_score = BigDecimal.valueOf((double) (yuwen_score+shuxue_score)/2).setScale(2, BigDecimal.ROUND_HALF_UP); 31 Student student = new Student(params.get("stu_name").toString(), Integer.parseInt(params.get("stu_gender").toString()), 32 yuwen_score, shuxue_score); 33 34 studentService.insertStudent(student, avg_score); 35 return new Result("200", "插入学生信息成功", student); 36 } 37 }

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 <!--@author Codorld--> 4 <!--投标结果的读取的xml--> 5 <mapper namespace="com.example.demo.mapper.StudentMapper"> 6 7 <!--同学对象注入的方式来获取插入学生自长的id--> 8 <insert id="insertStudent" keyProperty="student.stu_id" useGeneratedKeys="true"> 9 10 insert into student(stu_name, stu_gender, yuwen_score, shuxue_socre, avg_score) 11 values (#{student.stu_name}, #{student.stu_gender}, #{student.yuwen_socre}, #{student.shuxue_socre}, 12 #{avg_score}); 13 14 </insert> 15 16 </mapper>
4. 内部类返回id + 多参数:此中情况只需要新建一个内部类,改变controller中需要导入的对象,和mapper与service中的参数类型,xml中不改变具体看代码,测试结果与上图相同。

1 package com.example.demo.controller; 2 3 import com.example.demo.domain.Result; 4 import com.example.demo.entity.OutStudent; 5 import com.example.demo.service.StudentService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.web.bind.annotation.RequestBody; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import java.math.BigDecimal; 12 import java.util.Map; 13 14 /** 15 * @author Codorld 16 * @date 2021/05/05 14:08 17 */ 18 19 @RestController 20 public class StudentController { 21 22 @Autowired 23 StudentService studentService; 24 25 // 将学生的的信息存入,并且将学生的id传回 26 @RequestMapping("inertStu") 27 public Result insertStudent(@RequestBody Map params) { 28 int yuwen_score = Integer.parseInt(params.get("yuwen_score").toString()); 29 int shuxue_score = Integer.parseInt(params.get("shuxue_score").toString()); 30 BigDecimal avg_score = BigDecimal.valueOf((double) (yuwen_score+shuxue_score)/2).setScale(2, BigDecimal.ROUND_HALF_UP); 31 32 OutStudent.Student student = new OutStudent.Student(); 33 34 student.setStu_name(params.get("stu_name").toString()); 35 student.setStu_gender(Integer.parseInt(params.get("stu_gender").toString())); 36 student.setYuwen_socre(yuwen_score); 37 student.setShuxue_socre(shuxue_score); 38 //student.setAvg_score(avg_score); 39 40 studentService.insertStudent(student, avg_score); 41 return new Result("200", "插入学生信息成功", student); 42 } 43 }

1 package com.example.demo.entity; 2 3 import lombok.Data; 4 5 import java.math.BigDecimal; 6 7 /** 8 * @author wangXiaoMing 9 * @date 2021/05/05 17:51 10 */ 11 12 @Data 13 public class OutStudent { 14 15 private int any_one; 16 17 private Student student; 18 19 // 执行人(小组成员) 20 @Data 21 public static class Student { 22 23 // 学生id - 学号 24 private int stu_id; 25 26 // 学生姓名 27 private String stu_name; 28 29 // 学生性别 1.男 2.女 30 private int stu_gender; 31 32 // 语文成绩 33 private int yuwen_socre; 34 35 // 数学成绩 36 private int shuxue_socre; 37 38 // 平均分(保留小数点两位) 39 private BigDecimal avg_score; 40 41 } 42 }
以下修改参数类型:

1 package com.example.demo.service; 2 3 import com.example.demo.entity.OutStudent; 4 5 import java.math.BigDecimal; 6 7 /** 8 * @author Codorld 9 * @date 2021/05/05 14:11 10 */ 11 12 public interface StudentService { 13 14 // 插入学生数据返回id 15 void insertStudent(OutStudent.Student student, BigDecimal avg_score); 16 17 }

1 package com.example.demo.service.impl; 2 3 import com.example.demo.entity.OutStudent; 4 import com.example.demo.mapper.StudentMapper; 5 import com.example.demo.service.StudentService; 6 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.stereotype.Service; 8 9 import java.math.BigDecimal; 10 11 /** 12 * @author Codorld 13 * @date 2021/05/05 14:13 14 */ 15 16 @Service("studentService") 17 public class StudentServiceImpl implements StudentService { 18 19 @Autowired 20 StudentMapper studentMapper; 21 22 @Override 23 public void insertStudent(OutStudent.Student student, BigDecimal avg_score) { 24 studentMapper.insertStudent(student, avg_score); 25 } 26 }

1 package com.example.demo.mapper; 2 3 import com.example.demo.entity.OutStudent; 4 import org.apache.ibatis.annotations.Mapper; 5 6 import java.math.BigDecimal; 7 8 /** 9 * @author Codorld 10 * @date 2021/05/05 14:18 11 */ 12 13 @Mapper 14 public interface StudentMapper { 15 16 // 插入一条学生数据返回学生id 17 void insertStudent(OutStudent.Student student, BigDecimal avg_score); 18 19 }
以下未改变:

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 <!--@author Codorld--> 4 <!--投标结果的读取的xml--> 5 <mapper namespace="com.example.demo.mapper.StudentMapper"> 6 7 <!--同学对象注入的方式来获取插入学生自长的id--> 8 <insert id="insertStudent" keyProperty="student.stu_id" useGeneratedKeys="true"> 9 10 insert into student(stu_name, stu_gender, yuwen_score, shuxue_socre, avg_score) 11 values (#{student.stu_name}, #{student.stu_gender}, #{student.yuwen_socre}, #{student.shuxue_socre}, 12 #{avg_score}); 13 14 </insert> 15 16 </mapper>
思考:那么什么情况才使用得到内部类这种东西,其实原理跟多参数一样的,只是当我们创建类时候不可能每张表都要去创建一个员工表,比如我们可以通过创建一个整体的类,但是又需要添加数据时候那个id,我们就可以使用内部类的情况,一般在list一些数据时候都需要传给前端id以便之后的处理,所以这个内部类不仅仅可以在插入时候返回id使用,也可以作为结果集使用。对于内部类在结果集的使用在,另外一篇博客 巴拉巴拉巴拉 今天没时间了,之后写上补上超链接。
记得把总的链接也补上。
标题:微软雅黑
内容:楷体
斜体:仿宋
贴代码时候添加了行号,会不方便复制,但是内容不是很难,还是简易手敲一遍。
对待代码的态度,会影响到自己。