mybaties plus插入数据,自动回显 机制

2023-12-13 21:07:37

结论: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.Driver
    url: jdbc:mysql://127.0.0.1:3306/sky_take_out?characterEncoding=utf-8&allowMultiQueries=true&zeroDateTimeBehavior=convertToNull&useSSL=false
    username: root
    password: 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() = -738127871
        System.out.println("stu.getId() = " + stu.getId());

文章来源:https://blog.csdn.net/weixin_50914566/article/details/134877543
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。