若依cloud升级mybaits-plus方法

1、在主pom文件中引入依赖

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

2、在ruoyi-common/ruoyi-common-core的pom中文件进行引用

<dependency>
   <groupId>com.baomidou</groupId>
   <artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>

3、进行mapper配置,新建配置文件包放在ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/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;

@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、加载配置

5、在nacos中ruoyi-system修改的扫描配置,mybaits修改为mybaits-plus

6、使用mybaits-plus,需要对domain/server/serverImpl/mapper进行改造

domain中对在表中不存在的字段要进行屏蔽,注解@TableField(exist = false)

server定义需要继承IService,如下:

public interface ITMeasurePointService extends IService<TMeasurePoint>
ServiceImpl需要继承ServiceImpl如下:
public class TMeasurePointServiceImpl extends ServiceImpl<TMeasurePointMapper, TMeasurePoint> implements ITMeasurePointService

mapper需要继承BaseMapper如下:

public interface TMeasurePointMapper extends BaseMapper<TMeasurePoint>

7、例子

public int insertTMeasurePoint(TMeasurePoint tMeasurePoint)
    {
        //判断编码是否重复
        QueryWrapper<TMeasurePoint> wrapper = new QueryWrapper<>();
        wrapper.eq("point_code",tMeasurePoint.getPointCode());
        List<TMeasurePoint> tMeasurePoints = tMeasurePointMapper.selectList(wrapper);
        if(!tMeasurePoints.isEmpty()){
            return AjaxConstants.CODE_REPET;
        }

        tMeasurePoint.setCreateUser(SecurityUtils.getUserId());
        tMeasurePoint.setCreateDate(new Date());
        return tMeasurePointMapper.insertTMeasurePoint(tMeasurePoint);
    }

希望上述讲解可以帮助到你

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

开心编码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值