由于我之前开发的一些模块使用了mybatis-plus,为了方便灵活地使用,尝试引入MP。

引入MP可能会和原来的mybatis冲突,所以折腾一番才成功,记录一下步骤。

参考资料主要是这篇:

我的步骤主要是:

1、在根POM添加依赖:

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
MyBatisConfig类注释掉(屏蔽掉)

同个目录加入

MybatisPlusConfig
package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
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;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/**
 * Mybatis Plus 配置
 *
 * @author ruoyi
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig
{
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor()
    {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        // 阻断插件
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
        return interceptor;
    }

    /**
     * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
     */
    public PaginationInnerInterceptor paginationInnerInterceptor()
    {
        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
        // 设置数据库类型为mysql
        paginationInnerInterceptor.setDbType(DbType.MYSQL);
        // 设置最大单页限制数量,默认 500 条,-1 不受限制
        paginationInnerInterceptor.setMaxLimit(-1L);
        return paginationInnerInterceptor;
    }

    /**
     * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
    {
        return new OptimisticLockerInnerInterceptor();
    }

    /**
     * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
     */
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
    {
        return new BlockAttackInnerInterceptor();
    }
}
//        版权声明:本文为CSDN博主「onejson」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
//        原文链接:https://blog.csdn.net/wangxinxinsj/article/details/129280292

修改yml,把mybatis的注释掉,加入

mybatis-plus:
  # 指定实体类所在包的路径,MyBatis-Plus 会自动扫描该路径下的实体类
  typeAliasesPackage: com.ruoyi.**.domain
  # 指定 Mapper 接口所在包的路径,MyBatis-Plus 会自动扫描该路径下的 Mapper 接口
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 指定 MyBatis 全局配置文件的位置
  #  configLocation: classpath:mybatis/mybatis-config.xml
  configuration:
    #在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
    map-underscore-to-camel-case: true
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      id-type: ASSIGN_ID

common和framework的pom文件也添加mp的依赖,但是不用写版本号了

最后我写了一个MP的自定义SQL做测试,遇到一点麻烦:

//要放在mapper,否则默认不会扫描到
@Mapper
public interface CommonQuery {

    @Select("select 1+3 aa ")
    public int testMP();

    @Select("select 1+#{p} aa ")
    public int testMP2(int p);
}

如上所示,类应该放在mapper包,不是放在service包,mp扫描的是mapper。

Logo

快速构建 Web 应用程序

更多推荐