本文主要是介绍mybaties plus插入数据,自动回显 机制,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
结论:mybaties plus会将库里数据自动回显到 要插入的数据上
测试表格
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- 表结构
DROP TABLE IF EXISTS `t_stu`;
CREATE TABLE `t_stu` (`id` int NOT NULL COMMENT 'id',`name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`age` int NULL DEFAULT NULL,`sex` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,`class_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '课程名',`teacher` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '老师',`score` decimal(10, 2) NULL DEFAULT NULL COMMENT '分数',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;-- 库1数据
INSERT INTO `t_stu` VALUES (1, 'andy', 10, '男', '语文', '虚竹', 100.00);
YML
spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/sky_take_out?characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=falseusername: rootpassword: root
实体类
package com.suyan.code.test.pojo;import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;import java.io.Serializable;
import java.math.BigDecimal;
@TableName("t_stu")
public class Stu extends Model<Stu> {@TableId(value = "id",type = IdType.AUTO)private Integer id;private String name;private Integer age;private String className;private BigDecimal score;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public String getClassName() {return className;}public void setClassName(String className) {this.className = className;}public BigDecimal getScore() {return score;}public void setScore(BigDecimal score) {this.score = score;}// getter...// setter...
}
控制层(结论)
/** 核心:id自增情况下,库里id会回显到 要插入的那个实体上* 结论:在id字段存在,type = IdType.AUTO时,* 插入数据之后。数据的id是可以自动回显到 插入的实体id上的*** 备注:如删除type = IdType.AUTO* 则会导致自增的数据异常(仍然会把异常的id回显)* **/// stuMapper.insert(stu); //可以生效 id=1
// System.out.println("stu.getId() = " + stu.getId());// stuService.save(stu); //生效 id=2
// System.out.println("stu.getId()2 = " + stu.getId());// stuServiceImpl.save(stu); //生效 id=3
// System.out.println("stu.getId()3 = " + stu.getId());// stu.insert(); //生效 id=3
// System.out.println("stu.getId()4= " + stu.getId());//干掉字段上的自增(会导致自增的字段数据异常)!!!!!!!!!!!!stuMapper.insert(stu); //能回显,但是id异常 stu.getId() = -738127871System.out.println("stu.getId() = " + stu.getId());
这篇关于mybaties plus插入数据,自动回显 机制的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!