1. Accroître la dépendance
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${
mybatisplus.version}</version>
</dependency>
2. Modifiez application.yml pour ajouter la configuration mybatis-plus
# MyBatis
#mybatis:
# 搜索指定包别名
#typeAliasesPackage: com.ruoyi.**.domain
# 配置mapper的扫描,找到所有的mapper.xml映射文件
#mapperLocations: classpath*:mapper/**/*Mapper.xml
# 加载全局的配置文件
#configLocation: classpath:mybatis/mybatis-config.xml
#mybatis
mybatis-plus:
mapper-locations: classpath*:mapper/**/*Mapper.xml
# config-location: classpath:mybatis/mybatis-config.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.ruoyi.**.domain
global-config:
#数据库相关配置
db-config:
#主键类型 AUTO:"数据库ID自增", INPUT:"用户输入ID", ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: AUTO
logic-delete-value: -1
logic-not-delete-value: 0
banner: false
#原生配置
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
call-setters-on-nulls: true
jdbc-type-for-null: 'null'
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3. Annotez le code dans MyBatisConfig
package com.ruoyi.framework.config;
import com.ruoyi.common.utils.StringUtils;
import org.apache.ibatis.io.VFS;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;
import javax.sql.DataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
/**
* Mybatis支持*匹配扫描包
*
* @author ruoyi
*/
@Configuration
public class MyBatisConfig
{
@Autowired
private 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();
}*/
}
4. Modifier le modèle de génération de code
domaine.java.vm
package ${
packageName}.domain;
#foreach ($import in $importList)
import ${
import};
#end
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.annotation.Excel;
#if($table.crud || $table.sub)
import com.ruoyi.common.core.domain.BaseEntity;
#elseif($table.tree)
import com.ruoyi.common.core.domain.TreeEntity;
#end
/**
* ${functionName}对象 ${tableName}
*
* @author ${author}
* @date ${datetime}
*/
@TableName("${tableName}")
#if($table.crud || $table.sub)
#set($Entity="BaseEntity")
#elseif($table.tree)
#set($Entity="TreeEntity")
#end
public class ${
ClassName} extends ${
Entity}
{
private static final long serialVersionUID = 1L;
#foreach ($column in $columns)
#if(!$table.isSuperColumn($column.javaField))
/** $column.columnComment */
#if($column.list)
#set($parentheseIndex=$column.columnComment.indexOf("("))
#if($parentheseIndex != -1)
#set($comment=$column.columnComment.substring(0, $parentheseIndex))
#else
#set($comment=$column.columnComment)
#end
#if($parentheseIndex != -1)
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()")
#elseif($column.javaType == 'Date')
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd")
#else
@Excel(name = "${comment}")
#end
#end
#if($column.isPk==1)
@TableId(type = IdType.AUTO)
#end
private $column.javaType $column.javaField;
#end
#end
#if($table.sub)
/** $table.subTable.functionName信息 */
private List<${
subClassName}> ${
subclassName}List;
#end
#foreach ($column in $columns)
#if(!$table.isSuperColumn($column.javaField))
#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))
#set($AttrName=$column.javaField)
#else
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${
column.javaField.substring(1)})
#end
public void set${
AttrName}($column.javaType $column.javaField)
{
this.$column.javaField = $column.javaField;
}
public $column.javaType get${
AttrName}()
{
return $column.javaField;
}
#end
#end
#if($table.sub)
public List<${
subClassName}> get${
subClassName}List()
{
return ${
subclassName}List;
}
public void set${
subClassName}List(List<${
subClassName}> ${
subclassName}List)
{
this.${
subclassName}List = ${
subclassName}List;
}
#end
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
#foreach ($column in $columns)
#if($column.javaField.length() > 2 && $column.javaField.substring(1,2).matches("[A-Z]"))
#set($AttrName=$column.javaField)
#else
#set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${
column.javaField.substring(1)})
#end
.append("${column.javaField}", get${
AttrName}())
#end
#if($table.sub)
.append("${subclassName}List", get${
subClassName}List())
#end
.toString();
}
}
mappeur.java.vm
package ${
packageName}.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import java.util.List;
import ${
packageName}.domain.${
ClassName};
#if($table.sub)
import ${
packageName}.domain.${
subClassName};
#end
/**
* ${functionName}Mapper接口
*
* @author ${author}
* @date ${datetime}
*/
public interface ${
ClassName}Mapper extends BaseMapper<${
ClassName}>
{
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
public ${
ClassName} select${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaType} ${
pkColumn.javaField});
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}集合
*/
public List<${
ClassName}> select${
ClassName}List(${
ClassName} ${
className});
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int insert${
ClassName}(${
ClassName} ${
className});
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int update${
ClassName}(${
ClassName} ${
className});
/**
* 删除${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
public int delete${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaType} ${
pkColumn.javaField});
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的数据主键集合
* @return 结果
*/
public int delete${
ClassName}By${
pkColumn.capJavaField}s(String[] ${
pkColumn.javaField}s);
#if($table.sub)
/**
* 批量删除${subTable.functionName}
*
* @param ${pkColumn.javaField}s 需要删除的数据主键集合
* @return 结果
*/
public int delete${
subClassName}By${
subTableFkClassName}s(String[] ${
pkColumn.javaField}s);
/**
* 批量新增${subTable.functionName}
*
* @param ${subclassName}List ${subTable.functionName}列表
* @return 结果
*/
public int batch${
subClassName}(List<${
subClassName}> ${
subclassName}List);
/**
* 通过${functionName}主键删除${subTable.functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}ID
* @return 结果
*/
public int delete${
subClassName}By${
subTableFkClassName}(${
pkColumn.javaType} ${
pkColumn.javaField});
#end
}
service.java.vm
package ${
packageName}.service;
import java.util.List;
import ${
packageName}.domain.${
ClassName};
import com.baomidou.mybatisplus.extension.service.IService;
#if($table.tree)
import com.ruoyi.common.core.domain.Ztree;
#end
/**
* ${functionName}Service接口
*
* @author ${author}
* @date ${datetime}
*/
public interface I${
ClassName}Service extends IService<${
ClassName}>
{
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
public ${
ClassName} select${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaType} ${
pkColumn.javaField});
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}集合
*/
public List<${
ClassName}> select${
ClassName}List(${
ClassName} ${
className});
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int insert${
ClassName}(${
ClassName} ${
className});
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
public int update${
ClassName}(${
ClassName} ${
className});
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的${functionName}主键集合
* @return 结果
*/
public int delete${
ClassName}By${
pkColumn.capJavaField}s(String ${
pkColumn.javaField}s);
/**
* 删除${functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
public int delete${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaType} ${
pkColumn.javaField});
#if($table.tree)
/**
* 查询${functionName}树列表
*
* @return 所有${functionName}信息
*/
public List<Ztree> select${
ClassName}Tree();
#end
}
serviceImpl.java.vm
package ${
packageName}.service.impl;
import java.util.List;
#if($table.tree)
import java.util.ArrayList;
import com.ruoyi.common.core.domain.Ztree;
#end
#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;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;//导包
#if($table.sub)
import java.util.ArrayList;
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;
import com.ruoyi.common.core.text.Convert;
/**
* ${functionName}Service业务层处理
*
* @author ${author}
* @date ${datetime}
*/
@Service
public class ${
ClassName}ServiceImpl extends ServiceImpl<${
ClassName}Mapper, ${
ClassName}> implements I${
ClassName}Service
{
@Autowired
private ${
ClassName}Mapper ${
className}Mapper;
/**
* 查询${functionName}
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return ${functionName}
*/
@Override
public ${
ClassName} select${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaType} ${
pkColumn.javaField})
{
return ${
className}Mapper.select${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaField});
}
/**
* 查询${functionName}列表
*
* @param ${className} ${functionName}
* @return ${functionName}
*/
@Override
public List<${
ClassName}> select${
ClassName}List(${
ClassName} ${
className})
{
return ${
className}Mapper.select${
ClassName}List(${
className});
}
/**
* 新增${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int insert${
ClassName}(${
ClassName} ${
className})
{
#foreach ($column in $columns)
#if($column.javaField == 'createTime')
${
className}.setCreateTime(DateUtils.getNowDate());
#end
#end
#if($table.sub)
int rows = ${
className}Mapper.insert${
ClassName}(${
className});
insert${
subClassName}(${
className});
return rows;
#else
return ${
className}Mapper.insert${
ClassName}(${
className});
#end
}
/**
* 修改${functionName}
*
* @param ${className} ${functionName}
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int update${
ClassName}(${
ClassName} ${
className})
{
#foreach ($column in $columns)
#if($column.javaField == 'updateTime')
${
className}.setUpdateTime(DateUtils.getNowDate());
#end
#end
#if($table.sub)
${
className}Mapper.delete${
subClassName}By${
subTableFkClassName}(${
className}.get${
pkColumn.capJavaField}());
insert${
subClassName}(${
className});
#end
return ${
className}Mapper.update${
ClassName}(${
className});
}
/**
* 批量删除${functionName}
*
* @param ${pkColumn.javaField}s 需要删除的${functionName}主键
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int delete${
ClassName}By${
pkColumn.capJavaField}s(String ${
pkColumn.javaField}s)
{
#if($table.sub)
${
className}Mapper.delete${
subClassName}By${
subTableFkClassName}s(Convert.toStrArray(${
pkColumn.javaField}s));
#end
return ${
className}Mapper.delete${
ClassName}By${
pkColumn.capJavaField}s(Convert.toStrArray(${
pkColumn.javaField}s));
}
/**
* 删除${functionName}信息
*
* @param ${pkColumn.javaField} ${functionName}主键
* @return 结果
*/
#if($table.sub)
@Transactional
#end
@Override
public int delete${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaType} ${
pkColumn.javaField})
{
#if($table.sub)
${
className}Mapper.delete${
subClassName}By${
subTableFkClassName}(${
pkColumn.javaField});
#end
return ${
className}Mapper.delete${
ClassName}By${
pkColumn.capJavaField}(${
pkColumn.javaField});
}
#if($table.tree)
/**
* 查询${functionName}树列表
*
* @return 所有${functionName}信息
*/
@Override
public List<Ztree> select${
ClassName}Tree()
{
List<${
ClassName}> ${
className}List = ${
className}Mapper.select${
ClassName}List(new ${
ClassName}());
List<Ztree> ztrees = new ArrayList<Ztree>();
for (${
ClassName} ${
className} : ${
className}List)
{
Ztree ztree = new Ztree();
#if($treeCode.length() > 2 && $treeCode.substring(1,2).matches("[A-Z]"))
#set($TreeCode=$treeCode)
#else
#set($TreeCode=$treeCode.substring(0,1).toUpperCase() + ${
treeCode.substring(1)})
#end
#if($treeParentCode.length() > 2 && $treeParentCode.substring(1,2).matches("[A-Z]"))
#set($TreeParentCode=$treeParentCode)
#else
#set($TreeParentCode=$treeParentCode.substring(0,1).toUpperCase() + ${
treeParentCode.substring(1)})
#end
#if($treeName.length() > 2 && $treeName.substring(1,2).matches("[A-Z]"))
#set($TreeName=$treeName)
#else
#set($TreeName=$treeName.substring(0,1).toUpperCase() + ${
treeName.substring(1)})
#end
ztree.setId(${
className}.get${
TreeCode}());
ztree.setpId(${
className}.get${
TreeParentCode}());
ztree.setName(${
className}.get${
TreeName}());
ztree.setTitle(${
className}.get${
TreeName}());
ztrees.add(ztree);
}
return ztrees;
}
#end
#if($table.sub)
/**
* 新增${subTable.functionName}信息
*
* @param ${className} ${functionName}对象
*/
public void insert${
subClassName}(${
ClassName} ${
className})
{
List<${
subClassName}> ${
subclassName}List = ${
className}.get${
subClassName}List();
${
pkColumn.javaType} ${
pkColumn.javaField} = ${
className}.get${
pkColumn.capJavaField}();
if (StringUtils.isNotNull(${
subclassName}List))
{
List<${
subClassName}> list = new ArrayList<${
subClassName}>();
for (${
subClassName} ${
subclassName} : ${
subclassName}List)
{
${
subclassName}.set${
subTableFkClassName}(${
pkColumn.javaField});
list.add(${
subclassName});
}
if (list.size() > 0)
{
${
className}Mapper.batch${
subClassName}(list);
}
}
}
#end
}