基本上参考官帮助文档就可以http://doc.ruoyi.vip/ruoyi-vue/document/cjjc.html#%E9%9B%86%E6%88%90mybatisplus%E5%AE%9E%E7%8E%B0mybatis%E5%A2%9E%E5%BC%BA
1.ruoyi-common\pom.xml
模块添加整合依赖添加依赖。打开mybatisplus官网,用最新的MyBatis-Plus
com.baomidou
mybatis-plus-boot-starter
3.5.1
2.ruoyi-admin
文件application.yml
,修改mybatis配置为mybatis-plus。将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.ruoyi-framework/config/添加Mybatis Plus
配置MybatisPlusConfig.java
。 原来的MyBatisConfig.java
需要删除掉。
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();
}
}
4.以上就集成完了,下面改domain.加上
@TableName(value="sysUser")
在ID字段上加 @TableId(type=IdType.AUTO)
//这里
@TableName(value = "sys_student")
public class SysStudent implements Serializable
{
//这里
@TableField(exist = false)
private static final long serialVersionUID = 1L;
/** 编号 */
//这里
@TableId(type = IdType.AUTO)
5.修改服务层I...Service.java,加上extends IService<>
public interface ISysStudentService extends IService
6.修改mapper,加上extends BaseMapper<实体类名称>
public interface SysStudentMapper extends BaseMapper
7.修改服务实现层:....ServiceImpl.java , 加上extends ServiceImpl< , >
public class SysStudentServiceImpl extends ServiceImpl implements ISysStudentService
{
8.domina 中还 继承有一个BaseEntity,打开它修改,给每一个不存在的字段都加上@TableField(exist = false)
**
* Entity基类
*
* @author ruoyi
*/
public class BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
/** 搜索值 */
@TableField(exist = false)
private String searchValue;
/** 创建者 */
@TableField(exist = false)
private String createBy;
/**创建者ID*/
@TableField(exist = false)
private Integer idCreatBy;
/** 创建时间 */
@TableField(exist = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/** 更新者 */
@TableField(exist = false)
private String updateBy;
/** 更新时间 */
@TableField(exist = false)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/** 备注 */
@TableField(exist = false)
private String remark;
/** 请求参数 */
@TableField(exist = false)
private Map params;
9.修改代码生成器。domain.java.vm
@TableName("${tableName}") //添加
#if($table.crud || $table.sub)
#set($Entity="BaseEntity")
#elseif($table.tree)
#set($Entity="TreeEntity")
#end
public class ${ClassName} extends ${Entity} implements Serializable //修改
{
............
#else
@Excel(name = "${comment}")
#end
#end
//这里
#if($column.isPk==1)
@TableId
#end
private $column.javaType $column.javaField;
#end
.............
10.修改mapper.java.vm
package ${packageName}.mapper;
import java.util.List;
import ${packageName}.domain.${ClassName};
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
#if($table.sub)
import ${packageName}.domain.${subClassName};
#end
/**
* ${functionName}Mapper接口
*
* @author ${author}
* @date ${datetime}
*/
public interface ${ClassName}Mapper extends BaseMapper<${ClassName}>
...........
11.修改serviceImpl.java.vm
package ${packageName}.service.impl;
import java.util.List;
#foreach ($column in $columns)
#if($column.javaField == 'createTime' || $column.javaField == 'updateTime')
import com.ruoyi.common.utils.DateUtils;
#break
#end
#end
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
#if($table.sub)
import java.util.ArrayList;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;//导包
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import ${packageName}.domain.${subClassName};
#end
import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.domain.${ClassName};
import ${packageName}.service.I${ClassName}Service;
/**
* ${functionName}Service业务层处理
*
* @author ${author}
* @date ${datetime}
*/
@Service
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper, ${ClassName}> implements I${ClassName}Service //修改
12.修改service.java.vm
package ${packageName}.service;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import ${packageName}.domain.${ClassName};
/**
* ${functionName}Service接口
*
* @author ${author}
* @date ${datetime}
*/
public interface I${ClassName}Service extends IService<${ClassName}>
{