MyBatis-Plus学习笔记

目录

从MyBatis升级到MyBatis-Plus

分页查询的配置

yml配置

MP开启乐观锁拦截器

代码生成插件

 自定义sql


从MyBatis升级到MyBatis-Plus

首先先学习如何从MyBatis改到MyBatis-Plus

很多人首先会说先把Mybatis依赖删掉,再加上MyBatis-Plus的依赖

<!--        <dependency>-->
<!--            <groupId>org.mybatis.spring.boot</groupId>-->
<!--            <artifactId>mybatis-spring-boot-starter</artifactId>-->
<!--        </dependency>-->
    <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>最新版本</version>
    </dependency>

但是还有一个依赖需要删掉,那就是PageHelper分页插件,里面的Mybatis版本也会冲突

所以需要把PageHelper分页插件也去掉

        <!--        pageHelper分页插件的依赖-->
<!--        <dependency>-->
<!--            <groupId>com.github.pagehelper</groupId>-->
<!--            <artifactId>pagehelper-spring-boot-starter</artifactId>-->
<!--            <version>${pagehelper}</version>-->
<!--        </dependency>-->

在yml配置文件把Mybatis的配置修改成MP的

mybatis-plus:
  #mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.dhl.gd_blog_backend.entity.pojo
  configuration:
    #开启驼峰命名
    map-underscore-to-camel-case: true

然后应该就可以开始使用了

分页查询的配置

开始分页查询时,要配置MP的配置类

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

yml配置

开启MP日志需要在yml配置

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #打印SQL日志到控制台

全局主键id生成策略

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

全局表名前缀

mybatis-plus:
  global-config:
    db-config:
    	table-prefix: tbl_

逻辑删除操作全局配置

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

MP开启乐观锁拦截器

@Configuration
public class MpConfig {
    @Bean
    public MybatisPlusInterceptor mpInterceptor() {
        //1.定义Mp拦截器
        MybatisPlusInterceptor mpInterceptor = new MybatisPlusInterceptor();
        //2.添加乐观锁拦截器
        mpInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mpInterceptor;
    }
}

代码生成插件

介绍一个十分方便的代码生成插件:

一下子就把三层的代码板子给你生成好了,这点比MybatisX好用

注意:如果你的启动类没有加@MapperScan的话,需要在每个mapper文件里加上@Mapper注解,此插件是默认你的启动类是有@MapperScan的

@SpringBootApplication
@MapperScan("com.baomidou.mybatisplus.samples.quickstart.mapper")
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

 自定义sql

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值