若依分离版Oracle分离版集成mybatis-plus实现mybatis增强

一、前言

给项目加新模块写sql写烦了,于是就想改成mybatis-plus,原模块不变,因为Mybatis-Plus是在Mybatis的基础上进行扩展,只做增强不做改变,可以兼容Mybatis原生的特性,所以我们原模块的代码不用动,只需更改些配置即可。

二、配置

1、pom.xml模块添加整合依赖

<!-- mybatis-plus 增强CRUD -->
<dependency>
	<groupId>com.baomidou</groupId>
	<artifactId>mybatis-plus-boot-starter</artifactId>
	<version>3.5.1</version>
</dependency>

2、ruoyi-admin文件application.yml,修改mybatis配置为mybatis-plus

# MyBatis Plus配置
mybatis-plus:
  # 搜索指定包别名
  typeAliasesPackage: com.ruoyi.**.domain  //这里注意根据自己项目的实际目录配置
  # 配置mapper的扫描,找到所有的mapper.xml映射文件
  mapperLocations: classpath*:mapper/**/*Mapper.xml
  # 加载全局的配置文件
  configLocation: classpath:mybatis/mybatis-config.xml

3、添加Mybatis Plus配置MybatisPlusConfig.java。 原来MyBatisConfig.java需要去除掉

/**
 * 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();
    }
}

4、配置domain、service、mapper

/**
 * 学生信息对象 sys_student
 * 
 * @author ruoyi
 */
@TableName(value = "sys_student")
public class SysStudent implements Serializable
/**
 * 学生信息Service接口
 * 
 * @author ruoyi
 */
public interface ISysStudentService extends IService<SysStudent>
{

}
/**
 * 学生信息Service业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysStudentServiceImpl extends ServiceImpl<SysStudentMapper, SysStudent> implements ISysStudentService
{

}

PS: 最后说一下如果你们也遇到如下报错,可以去检查一下自己的application.yml配置,不要像我一样无脑复制粘贴然后找了一下午问题,最后。。。

  • 9
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MyBatis-Plus 支持 Oracle 数据库,可以通过在 `application.properties` 或 `application.yml` 文件中配置相关属性来使用。下面是一个示例 `application.properties` 文件: ```properties # 数据源配置 spring.datasource.url=jdbc:oracle:thin:@localhost:1521:ORCL spring.datasource.username=your_username spring.datasource.password=your_password spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver # MyBatis-Plus 配置 mybatis-plus.mapper-locations=classpath*:mapper/*.xml mybatis-plus.global-config.db-config.id-type=auto mybatis-plus.global-config.db-config.logic-delete-value=1 mybatis-plus.global-config.db-config.logic-not-delete-value=0 ``` 在上面的配置中,我们指定了 Oracle 数据库的连接 URL、用户名、密码和驱动程序的类名。我们还指定了 MyBatis-Plus 的配置,包括映射文件的位置、主键生成策略、逻辑删除值和未删除值。 需要注意的是,如果你使用的是 `application.yml` 文件,那么配置内容应该如下所示: ```yaml # 数据源配置 spring: datasource: url: jdbc:oracle:thin:@localhost:1521:ORCL username: your_username password: your_password driver-class-name: oracle.jdbc.driver.OracleDriver # MyBatis-Plus 配置 mybatis-plus: mapper-locations: classpath*:mapper/*.xml global-config: db-config: id-type: auto logic-delete-value: '1' logic-not-delete-value: '0' ``` 无论你使用哪种方式,都应该把 Oracle 驱动程序和 MyBatis-Plus 添加到你的项目依赖中,例如: ```xml <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc8</artifactId> <version>19.3.0.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.3.2</version> </dependency> ``` 这样就完成了 MyBatis-PlusOracle 数据库的配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值