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);
}
希望上述讲解可以帮助到你