MyBatisPlus部分知识学习(自用)

目录

MB整合Boot依赖

yml配置

Mapper被扫描

添加lombok依赖以后

分页查询

映射问题

1.表字段与编码属性设计不同步@TableField

2.编码中添加了数据库中未定义的属性@TableField

3.采用默认查询开放了更多的字段查看权限@TableField

@TableField

4.表名与编码开发设计不同步@TableName

id生成策略控制@TableId

逻辑删除@TableLogic

@TableLogic 

乐观锁@Version


MB整合Boot依赖

<!--起步依赖-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
<!--druid数据源-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>





<!--来个全的-->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.1.16</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

yml配置

spring:
    datasource:
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/********?serverTimezone=UTC
        username: *******
        password: *******
mybatis-plus:
    configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印SQL日志到控制台
    
#只能用空格缩进不能用Tab
#属性值前必须加空格
#可以配置多环境开发

Mapper被扫描

Dao接口要想被容器扫描到,有两种解决方案:

方案一:在Dao接口上添加@Mapper注解,并且确保Dao处在引导类所在包或其子包中 该方案的缺点是需要在每一Dao接口中添加注解

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

方案二:在引导类上添加@MapperScan注解,其属性为所要扫描的Dao所在包 该方案的好处是只需要写一次,则指定包下的所有Dao接口都能被扫描到,@Mapper就可以不 写。

@SpringBootApplication
@MapperScan("com.ycx.dao")
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

添加lombok依赖以后

Lombok,一个Java类库,提供了一组注解,简化POJO实体类开发。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String name;
    private String password;
    private Integer age;
    private String tel;
}//什么set get ToString全都不用写  一个@Data解决

Lombok常见的注解有:

@Setter:为模型类的属性提供setter方法

@Getter:为模型类的属性提供getter方法

@ToString:为模型类的属性提供toString方法

@EqualsAndHashCode:为模型类的属性提供equals和hashcode方法

@Data:是个组合注解,包含上面的注解的功能

@NoArgsConstructor:提供一个无参构造函数

@AllArgsConstructor:提供一个包含所有参数的构造函数

分页查询

@SpringBootTest
class Mybatisplus01QuickstartApplicationTests {
    @Autowired
    private UserDao userDao;
    //分页查询
    @Test
    void testSelectPage(){
        //1 创建IPage分页对象,设置分页参数,1为当前页码,3为每页显示的记录数
        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());
    }
}

设置分页拦截器

@Configuration
public class MybatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        //1 创建MybatisPlusInterceptor拦截器对象
        MybatisPlusInterceptor mpInterceptor=new MybatisPlusInterceptor();
        //2 添加分页拦截器
        mpInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mpInterceptor;
    }
}//后边乐观锁还用得到

映射问题

 1.表字段与编码属性设计不同步@TableField

 在属性上添加@TableField(value = "字段名")即可

2.编码中添加了数据库中未定义的属性@TableField

  在属性上添加@TableField(exist = false)即可

3.采用默认查询开放了更多的字段查看权限@TableField

   在属性上添加@TableField(select = false)即可 设置本字段不参与查询

@TableField

 4.表名与编码开发设计不同步@TableName

 在类上添加注解@TableName("数据库中表名")即可

 id生成策略控制@TableId

 

@Data
@TableName("tbl_user")
public class User {
                                //NONE: 不设置id生成策略
                                //INPUT:用户手工输入id
                                //ASSIGN_ID:雪花算法生成id(可兼容数值型与字符串型)
                                //ASSIGN_UUID:以UUID生成算法作为id生成策略 
    @TableId(type = IdType.AUTO)//设置为自动递增,数据库中表也要有自动递增选项                           
    private Long id;
    private String name;
    @TableField(value="pwd",select=false)
    private String password;
    private Integer age;
    private String tel;
    @TableField(exist=false)
    private Integer online;
}

为了简化配置 减少书写@TableId 在yml中添加

mybatis-plus:
    global-config:
        db-config:
            id-type: AUTO

逻辑删除@TableLogic

在表中添加逻辑删除字段deleted

 

实体类添加属性

@Data
@TableName("tbl_user") //可以不写是因为配置了全局配置
public class User {
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;
    private String name;
    @TableField(value="pwd",select=false)
    private String password;
    private Integer age;
    private String tel;
    @TableField(exist=false)
    private Integer online;
    @TableLogic(value="0",delval="1")
    //value为正常数据的值,delval为删除数据的值
    private Integer deleted;
}

 优化书写 在yml中添加配置

mybatis-plus:
    global-config:
        db-config:
            # 逻辑删除字段名
            logic-delete-field: deleted
            # 逻辑删除字面值:未删除为0
            logic-not-delete-value: 0
            # 逻辑删除字面值:删除为1
            logic-delete-value: 1

@TableLogic 

乐观锁@Version

在表中添加乐观锁字段version

 在模型类中添加对应的属性

@Data
@TableName("tbl_user") //可以不写是因为配置了全局配置
public class User {
    @TableId(type = IdType.ASSIGN_UUID)
    private String id;
    private String name;
    @TableField(value="pwd",select=false)
    private String password;
    private Integer age;
    private String tel;
    @TableField(exist=false)
    private Integer online;
    private Integer deleted;
    @Version      //乐观锁注释
    private Integer version;
}

添加乐观锁的拦截器

@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mpInterceptor() {
        //1.定义Mp拦截器
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
        //2.添加乐观锁拦截器
        mpInterceptor.addInnerInterceptor(new
        OptimisticLockerInnerInterceptor());
        return mpInterceptor;
    }
}//前边说过还要用到类

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值