目录
- 一、新建业务模块
- 二、集成MybatisPlus
- 三、测试
一、新建业务模块
- 新建Maven模块,并建立如下目录
- 新模块的pom.xml添加如下内容
<description>业务模块</description><dependencies><!-- Mysql驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 通用工具--><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-common</artifactId></dependency><!-- 验证码 --><dependency><groupId>pro.fessional</groupId><artifactId>kaptcha</artifactId></dependency><!-- 系统用户--><dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-system</artifactId></dependency></dependencies>
-
ruoyi文件下pom.xml加入新增的业务模块
-
admin模块引入业务模块,在pom.xml文件中添加下面依赖
<dependency><groupId>com.ruoyi</groupId><artifactId>ruoyi-wk</artifactId><version>4.7.8</version></dependency>
- 修改代码生成器配置文件
二、集成MybatisPlus
- common模块引入mybatis-plus依赖
<!-- mybatisPlus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.5.1</version></dependency>
- 修改BaseEntity.java,搜索值和请求参数添加注解
@TableField(exist = false)
- 修改admin配置文件,
application.yml
,注释mybatis配置,添加mybatis-plus配置
# MyBatis
#mybatis:
# # 搜索指定包别名
# typeAliasesPackage: com.syt.**.domain
# # 配置mapper的扫描,找到所有的mapper.xml映射文件
# mapperLocations: classpath*:mapper/**/*Mapper.xml
# # 加载全局的配置文件
# configLocation: classpath:mybatis/mybatis-config.xml#mybatis-plus
mybatis-plus:mapper-locations: classpath*:mapper/**/*.xml# config-location: classpath:mybatis/mybatis-config.xml#实体扫描,多个package用逗号或者分号分隔typeAliasesPackage: com.ruoyi.**.domainglobal-config:#数据库相关配置db-config:#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";id-type: AUTOlogic-delete-value: -1logic-not-delete-value: 0banner: false#原生配置configuration:map-underscore-to-camel-case: truecache-enabled: falsecall-setters-on-nulls: truejdbc-type-for-null: 'null'
- 注释Mybatis配置文件,在framework模块下(MybatisConfig.java)
/*** Mybatis支持*匹配扫描包** @author */
@Configuration
public class MyBatisConfig
{@Autowiredprivate Environment env;static final String DEFAULT_RESOURCE_PATTERN = "**/*.class";public static String setTypeAliasesPackage(String typeAliasesPackage){ResourcePatternResolver resolver = (ResourcePatternResolver) new PathMatchingResourcePatternResolver();MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);List<String> allResult = new ArrayList<String>();try{for (String aliasesPackage : typeAliasesPackage.split(",")){List<String> result = new ArrayList<String>();aliasesPackage = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX+ ClassUtils.convertClassNameToResourcePath(aliasesPackage.trim()) + "/" + DEFAULT_RESOURCE_PATTERN;Resource[] resources = resolver.getResources(aliasesPackage);if (resources != null && resources.length > 0){MetadataReader metadataReader = null;for (Resource resource : resources){if (resource.isReadable()){metadataReader = metadataReaderFactory.getMetadataReader(resource);try{result.add(Class.forName(metadataReader.getClassMetadata().getClassName()).getPackage().getName());}catch (ClassNotFoundException e){e.printStackTrace();}}}}if (result.size() > 0){HashSet<String> hashResult = new HashSet<String>(result);allResult.addAll(hashResult);}}if (allResult.size() > 0){typeAliasesPackage = String.join(",", (String[]) allResult.toArray(new String[0]));}else{throw new RuntimeException("mybatis typeAliasesPackage 路径扫描错误,参数typeAliasesPackage:" + typeAliasesPackage + "未找到任何包");}}catch (IOException e){e.printStackTrace();}return typeAliasesPackage;}public Resource[] resolveMapperLocations(String[] mapperLocations){ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();List<Resource> resources = new ArrayList<Resource>();if (mapperLocations != null){for (String mapperLocation : mapperLocations){try{Resource[] mappers = resourceResolver.getResources(mapperLocation);resources.addAll(Arrays.asList(mappers));}catch (IOException e){// ignore}}}return resources.toArray(new Resource[resources.size()]);}// @Bean
// public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception
// {
// String typeAliasesPackage = env.getProperty("mybatis.typeAliasesPackage");
// String mapperLocations = env.getProperty("mybatis.mapperLocations");
// String configLocation = env.getProperty("mybatis.configLocation");
// typeAliasesPackage = setTypeAliasesPackage(typeAliasesPackage);
// VFS.addImplClass(SpringBootVFS.class);
//
// final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
// sessionFactory.setDataSource(dataSource);
// sessionFactory.setTypeAliasesPackage(typeAliasesPackage);
// sessionFactory.setMapperLocations(resolveMapperLocations(StringUtils.split(mapperLocations, ",")));
// sessionFactory.setConfigLocation(new DefaultResourceLoader().getResource(configLocation));
// return sessionFactory.getObject();
// }
}
- 修改代码生成器
domain.java.vm
import com.baomidou.mybatisplus.annotation.TableName;
@TableName("${tableName}")
mapper.java.vm
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface ${ClassName}Mapper extends BaseMapper<${ClassName}>
service.java.vm
import com.baomidou.mybatisplus.extension.service.IService;
public interface I${ClassName}Service extends IService<${ClassName}>
serviceImpl.java.vm
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
public class ${ClassName}ServiceImpl extends ServiceImpl<${ClassName}Mapper,${ClassName}> implements I${ClassName}Service
generator.yml,自动去除表前缀改为true
。
tablePrefix
表前缀,生成类名时省略表前缀。这里前缀有sys_
,wk_
packageName
表示生成代码的路径,这里默认是com.ruoyi.wk
- MybatisPlus分页插件初始化配置,在framework模块下新增
MybatisPlusConfig.java
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
@MapperScan("com.*.*.mapper") //指定扫描目录
public class MybatisPlusConfig {@Beanpublic MybatisPlusInterceptor mybatisPlusInterceptor() {MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //注意使用哪种数据库return interceptor;}
}
- 按照MybatisPlus规则处理
SysConfig
,不处理会出现错误提示:
1.)SysConfig.java
2.)SysConfigMapper.java
3.)SysConfigService.java
4.)SysConfigServiceImpl.java
三、测试
-
启动项目
-
新建表,这里我定的表名前缀是wk_。表名前缀在代码生成模块有配置文件设置,在上面第5步有介绍
generator.yml
文件 -
用代码生成工具生成代码,注意检查下面几个是否符合自己设定的规则。菜单需要配置
-
将代码粘入项目,并执行生成包中的sql文件
-
重新启动项目
如果出现模版不存在异常,重新编译一下项目,在重新启动项目可以解决。