MyBatisPlus_快速入门_笔记

news2024/9/21 2:33:18

MyBatisPlus_快速入门_笔记

文章目录

  • MyBatisPlus_快速入门_笔记
    • 学习目标
    • 一、MyBatisPlus简介
      • 1. 入门案例
        • 问题导入
        • 1.1 SpringBoot整合MyBatisPlus入门程序
          • ①:创建新模块,选择Spring初始化,并配置模块相关基础信息
          • ③:手动添加MyBatisPlus起步依赖
          • ④:制作实体类与表结构
          • ⑤:设置Jdbc参数(**application.yml**)
          • ⑥:定义数据接口,继承**BaseMapper**
          • ⑦:测试类中注入dao接口,测试功能
      • 2. MyBatisPlus概述
        • 问题导入
        • 2.1 MyBatis介绍
        • 2.2 MyBatisPlus特性
    • 二、标准数据层开发
      • 1. MyBatisPlus的CRUD操作
      • 2. Lombok插件介绍
        • 问题导入
      • 3. MyBatisPlus分页功能
        • 问题导入
        • 3.1 分页功能接口
        • 3.2 MyBatisPlus分页使用
        • 3.3 开启MyBatisPlus日志
        • 3.4 解决日志打印过多问题
          • 3.4.1 取消初始化spring日志打印
          • 3.4.2 取消SpringBoot启动banner图标
          • 3.4.3 取消MybatisPlus启动banner图标
    • 三、DQL编程控制
      • 1. 条件查询方式
        • 1.1 条件查询
          • 1.1.1 方式一:按条件查询
          • 1.1.2 方式二:lambda格式按条件查询
          • 1.1.3 方式三:lambda格式按条件查询(推荐)
        • 1.2 组合条件
          • 1.2.1 并且关系(and)
          • 1.2.2 或者关系(or)
        • 1.3 NULL值处理
          • 问题导入
          • 1.3.1 if语句控制条件追加
          • 1.3.2 条件参数控制
          • 1.3.3 条件参数控制(链式编程)
      • 2. 查询投影-设置【查询字段、分组、分页】
      • 2.1 查询结果包含模型类中部分属性
        • 2.2 查询结果包含模型类中未定义的属性
      • 3. 查询条件设定
        • 问题导入
        • 3.1 查询条件
        • 3.2 查询API
        • 3.3 练习:MyBatisPlus练习
      • 4. 字段映射与表名映射
        • 问题导入
        • 4.1 问题一:表字段与编码属性设计不同步
        • 4.2 问题二:编码中添加了数据库中未定义的属性
        • 4.3 问题三:采用默认查询开放了更多的字段查看权限
        • 4.4 问题四:表名与编码开发设计不同步
    • 四、DML编程控制
      • 1. id生成策略控制(Insert)
        • 问题导入
        • 1.1 id生成策略控制(@TableId注解)
        • 1.2 全局策略配置
          • id生成策略全局配置
          • 表名前缀全局配置
      • 2. 多记录操作(批量Delete/Select)
        • 问题导入
        • 2.1 按照主键删除多条记录
        • 2.2 根据主键查询多条记录
      • 3. 逻辑删除(Delete/Update)
        • 问题导入
        • 3.1 逻辑删除案例
          • ①:数据库表中添加逻辑删除标记字段
          • ②:实体类中添加对应字段,并设定当前字段为逻辑删除标记字段
          • ③:配置逻辑删除字面值
      • 4. 乐观锁(Update)
        • 问题导入
        • 4.1 乐观锁案例
          • ①:数据库表中添加锁标记字段
          • ②:实体类中添加对应字段,并设定当前字段为逻辑删除标记字段
          • ③:配置乐观锁拦截器实现锁机制对应的动态SQL语句拼装
          • ④:使用乐观锁机制在修改前必须先获取到对应数据的verion方可正常进行
    • 五、快速开发-代码生成器
      • 问题导入
      • 1. MyBatisPlus提供模板
      • 2. 工程搭建和基本代码编写
      • 3. 开发者自定义配置

学习目标

  • 能够基于MyBatisPlus完成标准Dao开发
  • 能够掌握MyBatisPlus的条件查询
  • 能够掌握MyBatisPlus的字段映射与表名映射
  • 能够掌握id生成策略控制
  • 能够理解代码生成器的相关配置

一、MyBatisPlus简介

1. 入门案例

问题导入

MyBatisPlus环境搭建的步骤?

1.1 SpringBoot整合MyBatisPlus入门程序

①:创建新模块,选择Spring初始化,并配置模块相关基础信息

#####在这里插入图片描述

②:选择当前模块需要使用的技术集(仅保留JDBC)

在这里插入图片描述

③:手动添加MyBatisPlus起步依赖
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>

注意事项1:由于mp并未被收录到idea的系统内置配置,无法直接选择加入

注意事项2:如果使用Druid数据源,需要导入对应坐标

④:制作实体类与表结构

(类名与表名对应,属性名与字段名对应)

create database if not exists mybatisplus_db character set utf8;
use mybatisplus_db;
CREATE TABLE user (
            id bigint(20) primary key auto_increment,
            name varchar(32) not null,
            password  varchar(32) not null,
            age int(3) not null ,
            tel varchar(32) not null
);
insert into user values(null,'tom','123456',12,'12345678910');
insert into user values(null,'jack','123456',8,'12345678910');
insert into user values(null,'jerry','123456',15,'12345678910');
insert into user values(null,'tom','123456',9,'12345678910');
insert into user values(null,'snake','123456',28,'12345678910');
insert into user values(null,'张益达','123456',22,'12345678910');
insert into user values(null,'张大炮','123456',16,'12345678910');
public class User {
    private Long id;
    private String name;
    private String password;
    private Integer age;
    private String tel;
    //自行添加getter、setter、toString()等方法
}
⑤:设置Jdbc参数(application.yml
spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC
    username: root
    password: root
⑥:定义数据接口,继承BaseMapper
package com.itheima.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserDao extends BaseMapper<User> {
}

⑦:测试类中注入dao接口,测试功能
package com.itheima;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class Mybatisplus01QuickstartApplicationTests {

    @Autowired
    private UserDao userDao;

    @Test
    void testGetAll() {
        List<User> userList = userDao.selectList(null);
        System.out.println(userList);
    }
}

2. MyBatisPlus概述

问题导入

通过入门案例制作,MyBatisPlus的优点有哪些?

2.1 MyBatis介绍

  • MyBatisPlus(简称MP)是基于MyBatis框架基础上开发的增强型工具,旨在简化开发、提高效率

  • 官网:https😕/mybatis.plus/ https://mp.baomidou.com/

2.2 MyBatisPlus特性

  • 无侵入:只做增强不做改变,不会对现有工程产生影响
  • 强大的 CRUD 操作:内置通用 Mapper,少量配置即可实现单表CRUD 操作
  • 支持 Lambda:编写查询条件无需担心字段写错
  • 支持主键自动生成
  • 内置分页插件
  • ……

二、标准数据层开发

1. MyBatisPlus的CRUD操作

在这里插入图片描述

package com.itheima;

import com.itheima.dao.UserDao;
import com.itheima.domain.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {

    @Autowired
    private UserDao userDao;

    @Test
    void testSave() {
        User user = new User();
        user.setName("黑马程序员");
        user.setPassword("itheima");
        user.setAge(12);
        user.setTel("4006184000");
        userDao.insert(user);
    }

    @Test
    void testDelete() {
        userDao.deleteById(1401856123725713409L);
    }

    @Test
    void testUpdate() {
        User user = new User();
        user.setId(1L);
        user.setName("Tom888");
        user.setPassword("tom888");
        userDao.updateById(user);
    }

    @Test
    void testGetById() {
        User user = userDao.selectById(2L);
        System.out.println(user);
    }


    @Test
    void testGetAll() {
        List<User> userList = userDao.selectList(null);
        System.out.println(userList);
    }
}

2. Lombok插件介绍

问题导入

有什么简单的办法可以自动生成实体类的GET、SET方法?

  • Lombok,一个Java类库,提供了一组注解,简化POJO实体类开发。
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.12</version>
</dependency>
  • 常用注解:@Data,为当前实体类在编译期设置对应的get/set方法,无参/无参构造方法,toString方法,hashCode方法,equals方法等
package com.itheima.domain;

import lombok.*;
/*
    1 生成getter和setter方法:@Getter、@Setter
      生成toString方法:@ToString
      生成equals和hashcode方法:@EqualsAndHashCode

    2 统一成以上所有:@Data

    3 生成空参构造: @NoArgsConstructor
      生成全参构造: @AllArgsConstructor

    4 lombok还给我们提供了builder的方式创建对象,好处就是可以链式编程。 @Builder【扩展】
 */
@Data
public class User {
    private Long id;
    private String name;
    private String password;
    private Integer age;
    private String tel;
}

3. MyBatisPlus分页功能

问题导入

思考一下Mybatis分页插件是如何用的?

3.1 分页功能接口

在这里插入图片描述

3.2 MyBatisPlus分页使用

①:设置分页拦截器作为Spring管理的bean

package com.itheima.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MybatisPlusConfig {
    
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1 创建MybatisPlusInterceptor拦截器对象
        MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor();
        //2 添加分页拦截器
        mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mpInterceptor;
    }
}

②:执行分页查询

//分页查询
@Test
void testSelectPage(){
    //1 创建IPage分页对象,设置分页参数
    IPage<User> page=new Page<>(1,3);
    //2 执行分页查询
    userDao.selectPage(page,null);
    //3 获取分页结果
    System.out.println("当前页码值:"+page.getCurrent());
    System.out.println("每页显示数:"+page.getSize());
    System.out.println("总页数:"+page.getPages());
    System.out.println("总条数:"+page.getTotal());
    System.out.println("当前页数据:"+page.getRecords());
}

3.3 开启MyBatisPlus日志

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC
    username: root
    password: root
# 开启mp的日志(输出到控制台)
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

3.4 解决日志打印过多问题

3.4.1 取消初始化spring日志打印

在这里插入图片描述

做法:在resources下新建一个logback.xml文件,名称固定,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

</configuration>

关于logback参考播客:https://www.jianshu.com/p/75f9d11ae011

3.4.2 取消SpringBoot启动banner图标

在这里插入图片描述

spring:
  main:
    banner-mode: off # 关闭SpringBoot启动图标(banner)
3.4.3 取消MybatisPlus启动banner图标

在这里插入图片描述

# mybatis-plus日志控制台输出
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    banner: off # 关闭mybatisplus启动图标

三、DQL编程控制

1. 条件查询方式

  • MyBatisPlus将书写复杂的SQL查询条件进行了封装,使用编程的形式完成查询条件的组合

在这里插入图片描述

1.1 条件查询

1.1.1 方式一:按条件查询
//方式一:按条件查询
QueryWrapper<User> qw=new QueryWrapper<>();
qw.lt("age", 18);
List<User> userList = userDao.selectList(qw);
System.out.println(userList);
1.1.2 方式二:lambda格式按条件查询
//方式二:lambda格式按条件查询
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.lambda().lt(User::getAge, 10);
List<User> userList = userDao.selectList(qw);
System.out.println(userList);
1.1.3 方式三:lambda格式按条件查询(推荐)
//方式三:lambda格式按条件查询
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.lt(User::getAge, 10);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);

1.2 组合条件

1.2.1 并且关系(and)
//并且关系
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//并且关系:10到30岁之间
lqw.lt(User::getAge, 30).gt(User::getAge, 10);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
1.2.2 或者关系(or)
//或者关系
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//或者关系:小于10岁或者大于30岁
lqw.lt(User::getAge, 10).or().gt(User::getAge, 30);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);

1.3 NULL值处理

问题导入

如下搜索场景,在多条件查询中,有条件的值为空应该怎么解决?

在这里插入图片描述

1.3.1 if语句控制条件追加
Integer minAge=10;  //将来有用户传递进来,此处简化成直接定义变量了
Integer maxAge=null;  //将来有用户传递进来,此处简化成直接定义变量了
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
if(minAge!=null){
    lqw.gt(User::getAge, minAge);
}
if(maxAge!=null){
    lqw.lt(User::getAge, maxAge);
}
List<User> userList = userDao.selectList(lqw);
userList.forEach(System.out::println);
1.3.2 条件参数控制
Integer minAge=10;  //将来有用户传递进来,此处简化成直接定义变量了
Integer maxAge=null;  //将来有用户传递进来,此处简化成直接定义变量了
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//参数1:如果表达式为true,那么查询才使用该条件
lqw.gt(minAge!=null,User::getAge, minAge);
lqw.lt(maxAge!=null,User::getAge, maxAge);
List<User> userList = userDao.selectList(lqw);
userList.forEach(System.out::println);
1.3.3 条件参数控制(链式编程)
Integer minAge=10;  //将来有用户传递进来,此处简化成直接定义变量了
Integer maxAge=null;  //将来有用户传递进来,此处简化成直接定义变量了
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//参数1:如果表达式为true,那么查询才使用该条件
lqw.gt(minAge!=null,User::getAge, minAge)
   .lt(maxAge!=null,User::getAge, maxAge);
List<User> userList = userDao.selectList(lqw);
userList.forEach(System.out::println);

2. 查询投影-设置【查询字段、分组、分页】

2.1 查询结果包含模型类中部分属性

/*LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
lqw.select(User::getId, User::getName, User::getAge);*/
//或者
QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("id", "name", "age", "tel");
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);

2.2 查询结果包含模型类中未定义的属性

QueryWrapper<User> lqw = new QueryWrapper<User>();
lqw.select("count(*) as count, tel");
lqw.groupBy("tel");
List<Map<String, Object>> userList = userDao.selectMaps(lqw);
System.out.println(userList);

3. 查询条件设定

问题导入

多条件查询有哪些组合?

  • 范围匹配(> 、 = 、between)
  • 模糊匹配(like)
  • 空判定(null)
  • 包含性匹配(in)
  • 分组(group)
  • 排序(order)
  • ……

3.1 查询条件

  • 用户登录(eq匹配)
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//等同于=
lqw.eq(User::getName, "Jerry").eq(User::getPassword, "jerry");
User loginUser = userDao.selectOne(lqw);
System.out.println(loginUser);
  • 购物设定价格区间、户籍设定年龄区间(le ge匹配 或 between匹配)
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//范围查询 lt le gt ge eq between
lqw.between(User::getAge, 10, 30);
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
  • 查信息,搜索新闻(非全文检索版:like匹配)
LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<User>();
//模糊匹配 like
lqw.likeLeft(User::getName, "J");
List<User> userList = userDao.selectList(lqw);
System.out.println(userList);
  • 统计报表(分组查询聚合函数)
QueryWrapper<User> qw = new QueryWrapper<User>();
qw.select("gender","count(*) as nums");
qw.groupBy("gender");
List<Map<String, Object>> maps = userDao.selectMaps(qw);
System.out.println(maps);

3.2 查询API

  • 更多查询条件设置参看 https://mybatis.plus/guide/wrapper.html#abstractwrapper

3.3 练习:MyBatisPlus练习

题目:基于MyBatisPlus_Ex1模块,完成Top5功能的开发。

  • 说明:

    ①:Top5指根据销售量排序(提示:对销售量进行降序排序)

    ②:Top5是仅获取前5条数据(提示:使用分页功能控制数据显示数量)

4. 字段映射与表名映射

问题导入

思考表的字段和实体类的属性不对应,查询会怎么样?

4.1 问题一:表字段与编码属性设计不同步

  • 在模型类属性上方,使用**@TableField**属性注解,通过==value==属性,设置当前属性对应的数据库表中的字段关系。

在这里插入图片描述

4.2 问题二:编码中添加了数据库中未定义的属性

  • 在模型类属性上方,使用**@TableField注解,通过exist**属性,设置属性在数据库表字段中是否存在,默认为true。此属性无法与value合并使用。

在这里插入图片描述

4.3 问题三:采用默认查询开放了更多的字段查看权限

  • 在模型类属性上方,使用**@TableField注解,通过select**属性:设置该属性是否参与查询。此属性与select()映射配置不冲突。

在这里插入图片描述

4.4 问题四:表名与编码开发设计不同步

  • 模型类上方,使用**@TableName注解,通过value**属性,设置当前类对应的数据库表名称。

在这里插入图片描述

@Data
@TableName("tbl_user")
public class User {
    /*
        id为Long类型,因为数据库中id为bigint类型,
        并且mybatis有自己的一套id生成方案,生成出来的id必须是Long类型
     */
    private Long id;
    private String name;
    @TableField(value = "pwd",select = false)
    private String password;
    private Integer age;
    private String tel;
    @TableField(exist = false) //表示online字段不参与CRUD操作
    private Boolean online;
}

四、DML编程控制

1. id生成策略控制(Insert)

问题导入

主键生成的策略有哪几种方式?

不同的表应用不同的id生成策略

  • 日志:自增(1,2,3,4,……)
  • 购物订单:特殊规则(FQ23948AK3843)
  • 外卖单:关联地区日期等信息(10 04 20200314 34 91)
  • 关系表:可省略id
  • ……

1.1 id生成策略控制(@TableId注解)

  • 名称:@TableId

  • 类型:属性注解

  • 位置:模型类中用于表示主键的属性定义上方

  • 作用:设置当前类中主键属性的生成策略

  • 相关属性

    type:设置主键属性的生成策略,值参照IdType枚举值

在这里插入图片描述

1.2 全局策略配置

mybatis-plus:
  global-config:
    db-config:
      id-type: assign_id
      table-prefix: tbl_
id生成策略全局配置

在这里插入图片描述

表名前缀全局配置

在这里插入图片描述

2. 多记录操作(批量Delete/Select)

问题导入

MyBatisPlus是否支持批量操作?

在这里插入图片描述

2.1 按照主键删除多条记录

//删除指定多条数据
List<Long> list = new ArrayList<>();
list.add(1402551342481838081L);
list.add(1402553134049501186L);
list.add(1402553619611430913L);

userDao.deleteBatchIds(list);

2.2 根据主键查询多条记录

//查询指定多条数据
List<Long> list = new ArrayList<>();
list.add(1L);
list.add(3L);
list.add(4L);
userDao.selectBatchIds(list);

3. 逻辑删除(Delete/Update)

问题导入

在实际环境中,如果想删除一条数据,是否会真的从数据库中删除该条数据?

  • 删除操作业务问题:业务数据从数据库中丢弃

  • 逻辑删除:为数据设置是否可用状态字段,删除时设置状态字段为不可用状态,数据保留在数据库中

在这里插入图片描述

3.1 逻辑删除案例

①:数据库表中添加逻辑删除标记字段

在这里插入图片描述

②:实体类中添加对应字段,并设定当前字段为逻辑删除标记字段
package com.itheima.domain;

import com.baomidou.mybatisplus.annotation.*;

import lombok.Data;

@Data
public class User {

    private Long id;
    
    //逻辑删除字段,标记当前记录是否被删除
    @TableLogic
    private Integer deleted;
    
}
③:配置逻辑删除字面值
mybatis-plus:
  global-config:
    db-config:
      table-prefix: tbl_
      # 逻辑删除字段名
      logic-delete-field: deleted
      # 逻辑删除字面值:未删除为0
      logic-not-delete-value: 0
      # 逻辑删除字面值:删除为1
      logic-delete-value: 1

逻辑删除本质:逻辑删除的本质其实是修改操作。如果加了逻辑删除字段,查询数据时也会自动带上逻辑删除字段。

在这里插入图片描述

4. 乐观锁(Update)

问题导入

乐观锁主张的思想是什么?

  • 业务并发现象带来的问题:秒杀

在这里插入图片描述

4.1 乐观锁案例

①:数据库表中添加锁标记字段

在这里插入图片描述

②:实体类中添加对应字段,并设定当前字段为逻辑删除标记字段
package com.itheima.domain;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;

@Data
public class User {

	private Long id;
	
    @Version
    private Integer version;
}

③:配置乐观锁拦截器实现锁机制对应的动态SQL语句拼装
package com.itheima.config;

import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mpInterceptor() {
        //1.定义Mp拦截器
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();

        //2.添加乐观锁拦截器
        mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        
        return mpInterceptor;
    }
}

④:使用乐观锁机制在修改前必须先获取到对应数据的verion方可正常进行
@Test
public void testUpdate() {
    /*User user = new User();
    user.setId(3L);
    user.setName("Jock666");
    user.setVersion(1);
    userDao.updateById(user);*/
    
    //1.先通过要修改的数据id将当前数据查询出来
    //User user = userDao.selectById(3L);
    //2.将要修改的属性逐一设置进去
    //user.setName("Jock888");
    //userDao.updateById(user);
    
    //1.先通过要修改的数据id将当前数据查询出来
    User user = userDao.selectById(3L);     //version=3
    User user2 = userDao.selectById(3L);    //version=3
    user2.setName("Jock aaa");
    userDao.updateById(user2);              //version=>4
    user.setName("Jock bbb");
    userDao.updateById(user);               //verion=3?条件还成立吗?
}

在这里插入图片描述

五、快速开发-代码生成器

问题导入

如果只给一张表的字段信息,能够推演出Domain、Dao层的代码?

1. MyBatisPlus提供模板

  • Mapper接口模板

在这里插入图片描述

  • 实体对象类模板

在这里插入图片描述

2. 工程搭建和基本代码编写

  • 第一步:创建SpringBoot工程,添加代码生成器相关依赖,其他依赖自行添加
<!--代码生成器-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-generator</artifactId>
    <version>3.4.1</version>
</dependency>

<!--velocity模板引擎-->
<dependency>
    <groupId>org.apache.velocity</groupId>
    <artifactId>velocity-engine-core</artifactId>
    <version>2.3</version>
</dependency>
  • 第二步:编写代码生成器类
package com.itheima;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;

public class Generator {
    public static void main(String[] args) {
        //1. 创建代码生成器对象,执行生成代码操作
        AutoGenerator autoGenerator = new AutoGenerator();

        //2. 数据源相关配置:读取数据库中的信息,根据数据库表结构生成代码
        DataSourceConfig dataSource = new DataSourceConfig();
        dataSource.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/mybatisplus_db?serverTimezone=UTC");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        autoGenerator.setDataSource(dataSource);

         //3. 执行生成操作
        autoGenerator.execute();
    }
}

3. 开发者自定义配置

  • 设置全局配置
//设置全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setOutputDir(System.getProperty("user.dir")+"/mybatisplus_04_generator/src/main/java");    //设置代码生成位置
globalConfig.setOpen(false);    //设置生成完毕后是否打开生成代码所在的目录
globalConfig.setAuthor("黑马程序员");    //设置作者
globalConfig.setFileOverride(true);     //设置是否覆盖原始生成的文件
globalConfig.setMapperName("%sDao");    //设置数据层接口名,%s为占位符,指代模块名称
globalConfig.setIdType(IdType.ASSIGN_ID);   //设置Id生成策略
autoGenerator.setGlobalConfig(globalConfig);
  • 设置包名相关配置
//设置包名相关配置
PackageConfig packageInfo = new PackageConfig();
packageInfo.setParent("com.aaa");   //设置生成的包名,与代码所在位置不冲突,二者叠加组成完整路径
packageInfo.setEntity("domain");    //设置实体类包名
packageInfo.setMapper("dao");   //设置数据层包名
autoGenerator.setPackageInfo(packageInfo);
  • 策略设置
//策略设置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setInclude("tbl_user");  //设置当前参与生成的表名,参数为可变参数
strategyConfig.setTablePrefix("tbl_");  //设置数据库表的前缀名称,模块名 = 数据库表名 - 前缀名  例如: User = tbl_user - tbl_
strategyConfig.setRestControllerStyle(true);    //设置是否启用Rest风格
strategyConfig.setVersionFieldName("version");  //设置乐观锁字段名
strategyConfig.setLogicDeleteFieldName("deleted");  //设置逻辑删除字段名
strategyConfig.setEntityLombokModel(true);  //设置是否启用lombok
autoGenerator.setStrategy(strategyConfig);

说明:在资料中也提供了CodeGenerator代码生成器类,根据实际情况修改后可以直接使用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/64628.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

规避跑道安全事故,如何进行飞机跑道入侵检测

机场跑道入侵因为危险源综合而复杂&#xff0c;涉及方面很多&#xff0c;管制员、飞行员、环境等众多因素的影响导致&#xff0c;一直难以彻底消除&#xff0c;是全球民航面临的重要核心问题。本文围绕北斗RTK技术&#xff0c;探讨如何防止车辆入侵跑道。 机场跑道面积辽阔&am…

# Navicat报错:1045-Access denied for user root@localhost(using password:YES)怎么解决

文章目录1.删除mysql服务2.新建my.ini配置文件3.重新生成data文件4.重新安装mysql服务&#xff0c;同时绑定my.ini配置文件5.重新设置密码6.修改root用户密码1.删除mysql服务 以管理员身份运行cmd&#xff0c;进入安装目录下的mysql的bin文件下&#xff0c;运行命令&#xff1…

Executor框架

文章目录前言Executor框架简介ScheduledThreadPoolExecutor和ThreadPoolExecutor对比总结前言 在一些场景&#xff0c;我们常常会用到多线程&#xff0c;但是线程的管理又比较麻烦。所以我们通常使用线程池来进行线程的管理&#xff0c;而今天我们将介绍一个java中常常 用来创…

如何通过编码器信号计算输送线/输送带线速度(飞剪、追剪算法基础)

不同品牌PLC如何采集编码器的频率(速度)信号,专栏有系列文章和详细讲解,这里不再赘述,链接地址如下: PLC通过编码器反馈值计算速度的推荐做法(算法解析+ST代码)_RXXW_Dor的博客-CSDN博客_plc运算速度PLC如何测量采集编码器的位置数据,不清楚的可以参看我的另一篇博文:…

【docker】配置深度学习环境

目录基本环境搭建问题与解决容器启动后添加端口映射安装完虚拟环境后 CUDA调用不了opencv的使用问题自定义软件包的使用基本环境搭建 容器基本操作&#xff1a; # 按照服务器配置拉取对应的镜像 docker pull pytorch/pytorch:1.9.0-cuda10.2-cudnn7-devel# 查看主机端口 没有信…

网站部署SSL证书是否会影响网站流量?

给网站部署SSL证书的重要性想必很多站长用户都已知悉&#xff0c;SSL证书是数字证书的一种&#xff0c;由受信任的数字证书颁发机构CA&#xff0c;在验证服务器身份后颁发&#xff0c;具有服务器身份验证和数据传输加密功能。由于SSL证书是部署在服务器上&#xff0c;那么SSL证…

【第二阶段:java基础】第11章:Exception异常(P442-P459)

本系列博客是韩顺平老师java基础课的课程笔记&#xff0c;B站&#xff1a;课程链接&#xff0c;吐血推荐的一套全网最细java教程&#xff0c;获益匪浅&#xff01; 韩顺平P442-P4591. 异常的概念2. 常见的异常运行时异常编译异常3. 异常处理1️⃣try-catch-finally2️⃣throws4…

Java项目:SSM在线化妆品网站

作者主页&#xff1a;源码空间站2022 简介&#xff1a;Java领域优质创作者、Java项目、学习资料、技术互助 文末获取源码 项目介绍 本项目为前后台项目&#xff0c;前台为普通用户登录&#xff0c;后台为管理员登录&#xff1b; 管理员角色包含以下功能&#xff1a; 管理员登…

Java入门笔记

目录Java入门笔记快捷键基础准备环境准备Java编译运行流程TODO自定义模板基础语法变量原理标识符标识符命名规则数据类型整数型浮点型类型转换引用数据类型面向对象类和对象判断一个对象是不是某个类方法传参可变参数基本数据类型传参字符串数据类型传参引用数据类型传参静态属…

MySQL索引事务——小记

文章目录索引概念page索引类型B树vsB树主键索引非聚簇索引覆盖索引复合索引/联合索引优化基于B树的索引hash索引事务事务的特性ACID问题隔离级别索引 概念 使用一定的数据结构&#xff0c;来保存索引字段&#xff08;一列或多列&#xff09;对应的数据。以后根据索引字段来检…

低代码常见场景【上】|如何解决业务问题

全文 2105 字 阅读时间约 7 分钟 首发于码匠技术博客 目录 低代码用例 用于解决业务问题的低代码用例 内部系统开发所面临的困境 低代码如何解决上述困境 关于码匠 低代码平台通过在开发和部署应用程序时最大限度地减少编程来减轻 IT 团队的压力&#xff0c;不仅可以帮助…

[附源码]Python计算机毕业设计Django农村人居环境治理监管系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

Java+MySQL 基于Springboot的垃圾分类网站-计算机毕业设计

项目介绍 当前全球都在提倡环境保护&#xff0c;现在社会高速发展&#xff0c;我们每个人每天都会产生很多的垃圾&#xff0c;尤其是工业发展到一定阶段之后这些垃圾的种类也越来越多&#xff0c;如果随意丢弃很可能会造成环境污染&#xff0c;尤其是一些电池等重污染垃圾&…

前端模块化开发

1.模块化产生的背景 Javascript模块化编程&#xff0c;已经成为一个迫切的需求。理想情况下&#xff0c;开发者只需要实现核心的业务逻辑&#xff0c;其他都可以加载别人已经写好的模块。 但是&#xff0c;Javascript不是一种模块化编程语言&#xff0c;它不支持"类&quo…

持续的敏捷转型--我们的经验

作者&#xff1a;Shalini Joshi、 Syed Tamkeen Banu 在这篇文章中&#xff0c;我们将分享自己的经验和教训&#xff0c;说明组织可能在哪些方面会失败&#xff0c;以及可以采取哪些积极措施。 一、简介 如今&#xff0c;大多数组织为了获取更快的交付、更高的商业价值和更好的…

Markdown使用模板

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题&#xff0c;有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注…

智能仓储机器人设计

目录 第一章 绪论 1 1.1 研究的背景与意义[1] 1 1.2 机器人的研究现状[4] 2 1.3机器人的发展趋势 4 1.4本文的主要研究内容 5 第二章 机器人总体方案设计 7 2.1 小型仓储机器人的功能 7 2.2 传感器系统[13] 7 2.3 移动载体 7 2.4 自由度与机器人的运作 8 2.5 控制方式的选择 9 …

Git使用及配置

一、下载安装 网上很多教程我就不出了&#xff0c;hh 二、配置 1.设置用户信息 //设置用户信息 git config --global user.name"xxx" git config --global user.email"xxxxx.xxx" 三、获取本地仓库 1.在电脑上创建一个空目录作为本地Git仓库 2.进入这…

【Redis】初识Redis

1.初识Redis1.1安装1.2数据结构1.2.1 通用命令1.2.2 String类型key的结构1.2.3 Hash类型2.Jedis2.1 添加依赖及配置2.2 配置StringRedisTemplte1.初识Redis Redis是一个基于内存的键值型NoSQL数据库。其具有性能优越、持久化的特点&#xff0c;能够支持每秒十几万次的读写操作…

前端面试题之【HTTP/HTML/浏览器】

1.说一下http与https http&#xff1a;超文本传输协议&#xff0c;https&#xff1a;超文本传输安全协议 区别&#xff1a; http传输数据是未加密的&#xff0c;是明文传输&#xff1b;https使用ssl协议对数据进行加密处理&#xff1b;https协议需要ca证书&#xff0c;费用高…