完成主子表“代码生成”模块重构,支持mybatis-flex

This commit is contained in:
dataprince 2023-12-06 11:16:59 +08:00
parent 21c040b74e
commit 968e26c593
11 changed files with 138 additions and 120 deletions

View File

@ -28,7 +28,6 @@
<pagehelper.version>5.3.3</pagehelper.version> <pagehelper.version>5.3.3</pagehelper.version>
<fastjson.version>2.0.41</fastjson.version> <fastjson.version>2.0.41</fastjson.version>
<oshi.version>6.4.6</oshi.version> <oshi.version>6.4.6</oshi.version>
<!-- <commons.io.version>2.13.0</commons.io.version>-->
<commons.collections.version>3.2.2</commons.collections.version> <commons.collections.version>3.2.2</commons.collections.version>
<poi.version>5.2.3</poi.version> <poi.version>5.2.3</poi.version>
<easyexcel.version>3.3.2</easyexcel.version> <easyexcel.version>3.3.2</easyexcel.version>

View File

@ -116,8 +116,10 @@ public class VelocityUtils
context.put("subTableName", subTableName); context.put("subTableName", subTableName);
context.put("subTableFkName", subTableFkName); context.put("subTableFkName", subTableFkName);
context.put("subTableFkClassName", subTableFkClassName); context.put("subTableFkClassName", subTableFkClassName);
context.put("CapitalUnderScoreSubTableFkClassName", StringUtils.upperCase(StringUtils.toUnderScoreCase(subTableFkClassName)));//带下划线的字段大写
context.put("subTableFkclassName", StringUtils.uncapitalize(subTableFkClassName)); context.put("subTableFkclassName", StringUtils.uncapitalize(subTableFkClassName));
context.put("subClassName", subClassName); context.put("subClassName", subClassName);
context.put("CapitalUnderScoreSubClassName", StringUtils.upperCase(StringUtils.toUnderScoreCase(subClassName)));//大写的子类类名下划线SYS_USER
context.put("subclassName", StringUtils.uncapitalize(subClassName)); context.put("subclassName", StringUtils.uncapitalize(subClassName));
context.put("subImportList", getImportList(genTable.getSubTable())); context.put("subImportList", getImportList(genTable.getSubTable()));
} }
@ -152,6 +154,8 @@ public class VelocityUtils
{ {
templates.add("vm/vue/index.vue.vm"); templates.add("vm/vue/index.vue.vm");
templates.add("vm/java/sub-domain.java.vm"); templates.add("vm/java/sub-domain.java.vm");
templates.add("vm/java/sub-mapper.java.vm");
templates.add("vm/xml/sub-mapper.xml.vm");
} }
return templates; return templates;
} }
@ -176,56 +180,26 @@ public class VelocityUtils
String mybatisPath = MYBATIS_PATH + "/" + moduleName; String mybatisPath = MYBATIS_PATH + "/" + moduleName;
String vuePath = "vue"; String vuePath = "vue";
if (template.contains("domain.java.vm")) switch (template)
{ {
fileName = StringUtils.format("{}/domain/{}.java", javaPath, className); case "vm/java/domain.java.vm" -> fileName = StringUtils.format("{}/domain/{}.java", javaPath, className);
} case "vm/java/sub-domain.java.vm" -> fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName());
else if (template.contains("sub-domain.java.vm") && StringUtils.equals(GenConstants.TPL_SUB, genTable.getTplCategory())) case "vm/java/vo.java.vm" -> fileName = StringUtils.format("{}/domain/vo/{}Vo.java", javaPath, className);
{ case "vm/java/bo.java.vm" -> fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className);
fileName = StringUtils.format("{}/domain/{}.java", javaPath, genTable.getSubTable().getClassName()); case "vm/java/mapper.java.vm" -> fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className);
} case "vm/java/sub-mapper.java.vm" -> fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, genTable.getSubTable().getClassName());
else if (template.contains("vo.java.vm")) { case "vm/java/service.java.vm" -> fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
fileName = StringUtils.format("{}/domain/vo/{}Vo.java", javaPath, className); case "vm/java/serviceImpl.java.vm" -> fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
} case "vm/java/controller.java.vm" -> fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
else if (template.contains("bo.java.vm")) { case "vm/xml/mapper.xml.vm" -> fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
fileName = StringUtils.format("{}/domain/bo/{}Bo.java", javaPath, className); case "vm/xml/sub-mapper.xml.vm" -> fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, genTable.getSubTable().getClassName());
} case "vm/sql/sql.vm" -> fileName = businessName + "Menu.sql";
else if (template.contains("mapper.java.vm")) case "vm/js/api.js.vm" -> fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
{ case "vm/vue/index.vue.vm" -> fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
fileName = StringUtils.format("{}/mapper/{}Mapper.java", javaPath, className); case "vm/vue/index-tree.vue.vm" -> fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
} default -> fileName = "default.xml";
else if (template.contains("service.java.vm"))
{
fileName = StringUtils.format("{}/service/I{}Service.java", javaPath, className);
}
else if (template.contains("serviceImpl.java.vm"))
{
fileName = StringUtils.format("{}/service/impl/{}ServiceImpl.java", javaPath, className);
}
else if (template.contains("controller.java.vm"))
{
fileName = StringUtils.format("{}/controller/{}Controller.java", javaPath, className);
}
else if (template.contains("mapper.xml.vm"))
{
fileName = StringUtils.format("{}/{}Mapper.xml", mybatisPath, className);
}
else if (template.contains("sql.vm"))
{
fileName = businessName + "Menu.sql";
}
else if (template.contains("api.js.vm"))
{
fileName = StringUtils.format("{}/api/{}/{}.js", vuePath, moduleName, businessName);
}
else if (template.contains("index.vue.vm"))
{
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
}
else if (template.contains("index-tree.vue.vm"))
{
fileName = StringUtils.format("{}/views/{}/{}/index.vue", vuePath, moduleName, businessName);
} }
return fileName; return fileName;
} }

View File

@ -3,6 +3,9 @@ package ${packageName}.domain;
#foreach ($import in $importList) #foreach ($import in $importList)
import ${import}; import ${import};
#end #end
#if($table.sub)
import ${packageName}.domain.${subClassName};
#end
import com.mybatisflex.annotation.Id; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table; import com.mybatisflex.annotation.Table;
import lombok.Data; import lombok.Data;
@ -48,6 +51,6 @@ public class ${ClassName} extends ${Entity}
#if($table.sub) #if($table.sub)
/** $table.subTable.functionName信息 */ /** $table.subTable.functionName信息 */
private List<${subClassName}> ${subclassName}List; private List<${subClassName}> ${subclassName}List;
#end #end
} }

View File

@ -22,14 +22,16 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
#if($table.sub) #if($table.sub)
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import ${packageName}.domain.${subClassName}; import ${packageName}.domain.${subClassName};
import ${packageName}.mapper.${subClassName}Mapper;
import static ${packageName}.domain.table.${subClassName}TableDef.${CapitalUnderScoreSubClassName};
#end #end
import ${packageName}.mapper.${ClassName}Mapper; import ${packageName}.mapper.${ClassName}Mapper;
import ${packageName}.domain.${ClassName}; import ${packageName}.domain.${ClassName};
import ${packageName}.domain.bo.${ClassName}Bo; import ${packageName}.domain.bo.${ClassName}Bo;
import ${packageName}.domain.vo.${ClassName}Vo; import ${packageName}.domain.vo.${ClassName}Vo;
import ${packageName}.service.I${ClassName}Service; import ${packageName}.service.I${ClassName}Service;
import static ${packageName}.domain.table.${ClassName}TableDef.${CapitalUnderScoreClassName}; import static ${packageName}.domain.table.${ClassName}TableDef.${CapitalUnderScoreClassName};
/** /**
@ -43,6 +45,10 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
{ {
@Resource @Resource
private ${ClassName}Mapper ${className}Mapper; private ${ClassName}Mapper ${className}Mapper;
#if($table.sub)
@Resource
private ${subClassName}Mapper ${subclassName}Mapper;
#end
@Override @Override
public QueryWrapper query() { public QueryWrapper query() {
@ -92,7 +98,12 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
public ${ClassName}Vo selectById(${pkColumn.javaType} ${pkColumn.javaField}) public ${ClassName}Vo selectById(${pkColumn.javaType} ${pkColumn.javaField})
{ {
#set($CapitalPkColumn=${pkColumn.columnName.toUpperCase()}) #set($CapitalPkColumn=${pkColumn.columnName.toUpperCase()})
#if($table.sub)
return ${className}Mapper.selectOneWithRelationsByQueryAs(query().where(${CapitalUnderScoreClassName}.${CapitalPkColumn}.eq(${pkColumn.javaField})), ${ClassName}Vo.class);
#else
return this.getOneAs(query().where(${CapitalUnderScoreClassName}.${CapitalPkColumn}.eq(${pkColumn.javaField})), ${ClassName}Vo.class); return this.getOneAs(query().where(${CapitalUnderScoreClassName}.${CapitalPkColumn}.eq(${pkColumn.javaField})), ${ClassName}Vo.class);
#end
} }
/** /**
@ -105,7 +116,11 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
public List<${ClassName}Vo> selectList(${ClassName}Bo ${className}Bo) public List<${ClassName}Vo> selectList(${ClassName}Bo ${className}Bo)
{ {
QueryWrapper queryWrapper = buildQueryWrapper(${className}Bo); QueryWrapper queryWrapper = buildQueryWrapper(${className}Bo);
#if($table.sub)
return ${className}Mapper.selectListWithRelationsByQueryAs(queryWrapper, ${ClassName}Vo.class);
#else
return this.listAs(queryWrapper, ${ClassName}Vo.class); return this.listAs(queryWrapper, ${ClassName}Vo.class);
#end
} }
#if($table.crud || $table.sub) #if($table.crud || $table.sub)
@ -119,7 +134,11 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
public TableDataInfo<${ClassName}Vo> selectPage(${ClassName}Bo ${className}Bo) public TableDataInfo<${ClassName}Vo> selectPage(${ClassName}Bo ${className}Bo)
{ {
QueryWrapper queryWrapper = buildQueryWrapper(${className}Bo); QueryWrapper queryWrapper = buildQueryWrapper(${className}Bo);
#if($table.sub)
Page<${ClassName}Vo> page = ${className}Mapper.paginateWithRelationsAs(PageQuery.build(), queryWrapper, ${ClassName}Vo.class);
#else
Page<${ClassName}Vo> page = this.pageAs(PageQuery.build(), queryWrapper, ${ClassName}Vo.class); Page<${ClassName}Vo> page = this.pageAs(PageQuery.build(), queryWrapper, ${ClassName}Vo.class);
#end
return TableDataInfo.build(page); return TableDataInfo.build(page);
} }
#end #end
@ -140,7 +159,7 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
#if($table.sub) #if($table.sub)
boolean inserted = this.save(${className});//使用全局配置的雪花算法主键生成器生成ID值 boolean inserted = this.save(${className});//使用全局配置的雪花算法主键生成器生成ID值
if (inserted) { if (inserted && ObjectUtil.isNotNull(${className})) {
return insert${subClassName}(${className}); return insert${subClassName}(${className});
} }
return false; return false;
@ -162,15 +181,17 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
public boolean update(${ClassName}Bo ${className}Bo) public boolean update(${ClassName}Bo ${className}Bo)
{ {
${ClassName} ${className} = MapstructUtils.convert(${className}Bo, ${ClassName}.class); ${ClassName} ${className} = MapstructUtils.convert(${className}Bo, ${ClassName}.class);
if(ObjectUtil.isNotNull(${className}) && ObjectUtil.isNotNull(${className}.get${pkColumn.capJavaField}())) {
boolean updated = this.updateById(${className}); boolean updated = this.updateById(${className});
#if($table.sub) #if($table.sub)
if (updated) { if (updated) {
${className}Mapper.delete${subClassName}By${subTableFkClassName}(${className}.get${pkColumn.capJavaField}()); QueryWrapper queryWrapper = QueryWrapper.create().from(${CapitalUnderScoreSubClassName}).where(${CapitalUnderScoreSubClassName}.${CapitalUnderScoreSubTableFkClassName}.eq(${className}.get${pkColumn.capJavaField}()));
${subclassName}Mapper.deleteByQuery(queryWrapper);
return insert${subClassName}(${className}); return insert${subClassName}(${className});
} }
#end #end
return updated; }
return false;
} }
/** /**
@ -184,7 +205,8 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
public boolean deleteByIds(${pkColumn.javaType}[] ${pkColumn.javaField}s) public boolean deleteByIds(${pkColumn.javaType}[] ${pkColumn.javaField}s)
{ {
#if($table.sub) #if($table.sub)
${className}Mapper.delete${subClassName}By${subTableFkClassName}s(${pkColumn.javaField}s); QueryWrapper queryWrapper = QueryWrapper.create().from(${CapitalUnderScoreSubClassName}).where(${CapitalUnderScoreSubClassName}.${CapitalUnderScoreSubTableFkClassName}.in(Arrays.asList(${pkColumn.javaField}s)));
${subclassName}Mapper.deleteByQuery(queryWrapper);
#end #end
return this.removeByIds(Arrays.asList(${pkColumn.javaField}s)); return this.removeByIds(Arrays.asList(${pkColumn.javaField}s));
} }
@ -195,7 +217,7 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
* *
* @param ${className} ${functionName}对象 * @param ${className} ${functionName}对象
*/ */
public boolean insert${subClassName}(${ClassName} ${className}) private boolean insert${subClassName}(${ClassName} ${className})
{ {
List<${subClassName}> ${subclassName}List = ${className}.get${subClassName}List(); List<${subClassName}> ${subclassName}List = ${className}.get${subClassName}List();
${pkColumn.javaType} ${pkColumn.javaField} = ${className}.get${pkColumn.capJavaField}(); ${pkColumn.javaType} ${pkColumn.javaField} = ${className}.get${pkColumn.capJavaField}();
@ -209,7 +231,7 @@ public class ${ClassName}ServiceImpl extends BaseServiceImpl<${ClassName}Mapper,
} }
if (list.size() > 0) if (list.size() > 0)
{ {
${className}Mapper.batch${subClassName}(list); return ${subclassName}Mapper.insertBatch(list)>0;
} }
} }
return true; return true;

View File

@ -3,7 +3,17 @@ package ${packageName}.domain;
#foreach ($import in $subImportList) #foreach ($import in $subImportList)
import ${import}; import ${import};
#end #end
import com.ruoyi.common.core.core.annotation.Excel; import com.mybatisflex.annotation.Id;
import com.mybatisflex.annotation.Table;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serial;
import java.io.Serializable;
#if($table.crud || $table.sub)
import com.ruoyi.common.orm.core.domain.BaseEntity;
#elseif($table.tree)
import com.ruoyi.common.orm.core.domain.TreeEntity;
#end
/** /**
* ${subTable.functionName}对象 ${subTableName} * ${subTable.functionName}对象 ${subTableName}
@ -11,63 +21,37 @@ import com.ruoyi.common.core.core.annotation.Excel;
* @author ${author} * @author ${author}
* ${datetime} * ${datetime}
*/ */
public class ${subClassName} extends BaseEntity #if($table.crud || $table.sub)
#set($Entity="BaseEntity")
#elseif($table.tree)
#set($Entity="TreeEntity")
#end
@Data
@EqualsAndHashCode(callSuper = true)
@Table(value = "${subTableName}")
public class ${subClassName} extends ${Entity}
{ {
@Serial
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
#foreach ($column in $subTable.columns) #foreach ($column in $subTable.columns)
#if(!$table.isSuperColumn($column.javaField)) #if(!$subTable.isSuperColumn($column.javaField))
/** $column.columnComment */ /** $column.columnComment */
#if($column.list) #if($column.javaField=='delFlag')
#set($parentheseIndex=$column.columnComment.indexOf("")) @Column(isLogicDelete = true)
#if($parentheseIndex != -1)
#set($comment=$column.columnComment.substring(0, $parentheseIndex))
#else
#set($comment=$column.columnComment)
#end #end
#if($parentheseIndex != -1) #if($column.javaField=='version')
@Excel(name = "${comment}", readConverterExp = "$column.readConverterExp()") @Column(version = true)
#elseif($column.javaType == 'Date') #end
#if($column.isPk==1)
@Id
#end
#if($column.javaType == 'Date')
@JsonFormat(pattern = "yyyy-MM-dd") @JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "${comment}", width = 30, dateFormat = "yyyy-MM-dd")
#else
@Excel(name = "${comment}")
#end
#end #end
private $column.javaType $column.javaField; private $column.javaType $column.javaField;
#end #end
#end #end
#foreach ($column in $subTable.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
@Override
public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
#foreach ($column in $subTable.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
.toString();
}
} }

View File

@ -0,0 +1,21 @@
package ${packageName}.mapper;
import com.mybatisflex.core.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
#if($table.sub)
import ${packageName}.domain.${subClassName};
#else
import ${packageName}.domain.${ClassName};
#end
/**
* ${subTable.functionName}Mapper接口
*
* @author ${author}
* ${datetime}
*/
@Mapper
public interface ${subClassName}Mapper extends BaseMapper<${subClassName}>
{
}

View File

@ -11,7 +11,10 @@ import com.ruoyi.common.excel.convert.ExcelDictConvert;
import io.github.linpeilie.annotations.AutoMapper; import io.github.linpeilie.annotations.AutoMapper;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
#if($table.sub)
import com.mybatisflex.annotation.RelationOneToMany;
import ${packageName}.domain.${subClassName};
#end
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;
@ -70,4 +73,11 @@ public class ${ClassName}Vo extends ${Entity} implements Serializable
#end #end
#end #end
#end #end
#if($table.sub)
/** $table.subTable.functionName信息 */
@RelationOneToMany(selfField = "${pkColumn.javaField}", targetField = "${pkColumn.javaField}")
private List<${subClassName}> ${subclassName}List;
#end
} }

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${packageName}.mapper.${subClassName}Mapper">
</mapper>

View File

@ -130,7 +130,6 @@ public class SysDataScopeServiceImpl implements ISysDataScopeService {
} }
queryWrapper.and(bracket(queryCondition));//所有新增的插叙条件外边用圆括号包括起来 queryWrapper.and(bracket(queryCondition));//所有新增的插叙条件外边用圆括号包括起来
//System.out.println("*******************:SysDataScopeServiceImpl.filter:queryWrapper.tosql="+queryWrapper.toSQL());
return queryWrapper; return queryWrapper;
} }

View File

@ -45,7 +45,7 @@ public class SysNoticeServiceImpl extends BaseServiceImpl<SysNoticeMapper, SysNo
*/ */
@Override @Override
public SysNoticeVo selectNoticeById(Long noticeId) { public SysNoticeVo selectNoticeById(Long noticeId) {
return this.getOneAs(query().where(SYS_NOTICE.NOTICE_ID.eq(noticeId)), SysNoticeVo.class); return noticeMapper.selectOneWithRelationsByQueryAs(query().where(SYS_NOTICE.NOTICE_ID.eq(noticeId)), SysNoticeVo.class);
} }
/** /**

View File

@ -206,7 +206,6 @@ public class SysUserServiceImpl extends BaseServiceImpl<SysUserMapper, SysUser>
if (StringUtils.isNotEmpty(userBo.getPhonenumber())) { if (StringUtils.isNotEmpty(userBo.getPhonenumber())) {
queryWrapper.and(SYS_USER.PHONENUMBER.like(userBo.getPhonenumber())); queryWrapper.and(SYS_USER.PHONENUMBER.like(userBo.getPhonenumber()));
} }
//TODO:数据范围过滤
Page<SysUserVo> page = this.pageAs(PageQuery.build(), queryWrapper, SysUserVo.class); Page<SysUserVo> page = this.pageAs(PageQuery.build(), queryWrapper, SysUserVo.class);
return TableDataInfo.build(page); return TableDataInfo.build(page);