Merge pull request #172 from moshowgame/feature_template_update

Feature template update
This commit is contained in:
Moshow郑锴
2025-12-07 22:25:35 +08:00
committed by GitHub
11 changed files with 273 additions and 105 deletions

View File

@@ -244,6 +244,7 @@ ResultVo.error(message);
# Update Logs # Update Logs
| 更新日期 | 更新内容 | | 更新日期 | 更新内容 |
|:-----------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| |:-----------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 2025.12.09 | 优化Mybatis和Mybatis-Plus模板 |
| 2025.12.08 | 引入单元测试和JaCoCo测试覆盖率优化代码覆盖率 [UNIT_TEST_DOCUMENT.md](UNIT_TEST_DOCUMENT.md) | | 2025.12.08 | 引入单元测试和JaCoCo测试覆盖率优化代码覆盖率 [UNIT_TEST_DOCUMENT.md](UNIT_TEST_DOCUMENT.md) |
| 2025.12.07 | 后端重构优化![REFACTORING_DOCUMENT.md](REFACTORING_DOCUMENT.md) ;目录结构调整! | | 2025.12.07 | 后端重构优化![REFACTORING_DOCUMENT.md](REFACTORING_DOCUMENT.md) ;目录结构调整! |
| 2025.09.14 | 优化JSqlParser Engine(DDL Create SQL和Select SQL),适配更高级复杂的SQL | | 2025.09.14 | 优化JSqlParser Engine(DDL Create SQL和Select SQL),适配更高级复杂的SQL |

View File

@@ -233,11 +233,11 @@ public class SqlParserServiceImpl implements SqlParserService {
String classComment = null; String classComment = null;
//mysql是comment=,pgsql/oracle是comment on table, //mysql是comment=,pgsql/oracle是comment on table,
//2020-05-25 优化表备注的获取逻辑 //2020-05-25 优化表备注的获取逻辑
if (tableSql.contains("comment=") || tableSql.contains("comment on table")) { if (tableSql.toLowerCase().contains("comment=") || tableSql.toLowerCase().contains("comment on table")) {
int ix = tableSql.lastIndexOf("comment="); int ix = tableSql.toLowerCase().lastIndexOf("comment=");
String classCommentTmp = (ix > -1) ? String classCommentTmp = (ix > -1) ?
tableSql.substring(ix + 8).trim() : tableSql.substring(ix + 8).trim() :
tableSql.substring(tableSql.lastIndexOf("comment on table") + 17).trim(); tableSql.substring(tableSql.toLowerCase().lastIndexOf("comment on table") + 17).trim();
if (classCommentTmp.contains("`")) { if (classCommentTmp.contains("`")) {
classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("`") + 1); classCommentTmp = classCommentTmp.substring(classCommentTmp.indexOf("`") + 1);
classCommentTmp = classCommentTmp.substring(0, classCommentTmp.indexOf("`")); classCommentTmp = classCommentTmp.substring(0, classCommentTmp.indexOf("`"));
@@ -256,11 +256,11 @@ public class SqlParserServiceImpl implements SqlParserService {
List<FieldInfo> fieldList = new ArrayList<FieldInfo>(); List<FieldInfo> fieldList = new ArrayList<FieldInfo>();
// 正常( ) 内的一定是字段相关的定义。 // 正常( ) 内的一定是字段相关的定义。
String fieldListTmp = tableSql.substring(tableSql.indexOf("(") + 1, tableSql.lastIndexOf(")")); String fieldListTmp = tableSql.substring(tableSql.indexOf("(") + 1, tableSql.lastIndexOf(")")).trim();
// 匹配 comment替换备注里的小逗号, 防止不小心被当成切割符号切割 // 匹配 comment替换备注里的小逗号, 防止不小心被当成切割符号切割
String commentPattenStr1 = "comment `(.*?)\\`"; String commentPattenStr1 = "comment `(.*?)\\`";
Matcher matcher1 = Pattern.compile(commentPattenStr1).matcher(fieldListTmp); Matcher matcher1 = Pattern.compile(commentPattenStr1).matcher(fieldListTmp.toLowerCase());
while (matcher1.find()) { while (matcher1.find()) {
String commentTmp = matcher1.group(); String commentTmp = matcher1.group();
@@ -305,18 +305,20 @@ public class SqlParserServiceImpl implements SqlParserService {
// 2019-2-22 zhengkai 要在条件中使用复杂的表达式 // 2019-2-22 zhengkai 要在条件中使用复杂的表达式
// 2019-4-29 zhengkai 优化对普通和特殊storage关键字的判断感谢@AhHeadFloating的反馈 // 2019-4-29 zhengkai 优化对普通和特殊storage关键字的判断感谢@AhHeadFloating的反馈
// 2020-10-20 zhengkai 优化对fulltext/index关键字的处理感谢@WEGFan的反馈 // 2020-10-20 zhengkai 优化对fulltext/index关键字的处理感谢@WEGFan的反馈
// 2025-12-07 zhengkai 修复对primary key的处理
boolean notSpecialFlag = ( boolean notSpecialFlag = (
!columnLine.contains("key ") !columnLine.contains("key ")
&& !columnLine.contains("constraint") && !columnLine.toLowerCase().contains("constraint")
&& !columnLine.contains(" using ") && !columnLine.toLowerCase().contains(" using ")
&& !columnLine.contains("unique ") && !columnLine.toLowerCase().contains("unique ")
&& !columnLine.contains("fulltext ") && !columnLine.toLowerCase().contains("fulltext ")
&& !columnLine.contains("index ") && !columnLine.toLowerCase().contains("index ")
&& !columnLine.contains("pctincrease") && !columnLine.toLowerCase().contains("pctincrease")
&& !columnLine.contains("buffer_pool") && !columnLine.toLowerCase().contains("buffer_pool")
&& !columnLine.contains("tablespace") && !columnLine.toLowerCase().contains("tablespace")
&& !(columnLine.contains("primary ") && columnLine.indexOf("storage") + 3 > columnLine.indexOf("(")) && !(columnLine.toLowerCase().contains("primary ") && columnLine.indexOf("storage") + 3 > columnLine.indexOf("("))
&& !(columnLine.contains("primary ") && i > 3) && !(columnLine.toLowerCase().contains("primary ") && i > 3)
&& !columnLine.toLowerCase().contains("primary key")
); );
if (notSpecialFlag) { if (notSpecialFlag) {
@@ -349,7 +351,10 @@ public class SqlParserServiceImpl implements SqlParserService {
} else { } else {
fieldName = columnName; fieldName = columnName;
} }
columnLine = columnLine.substring(columnLine.indexOf("`") + 1).trim(); // 修复Oracle字段名不带引号的情况
if (columnLine.contains("`")) {
columnLine = columnLine.substring(columnLine.indexOf("`") + 1).trim();
}
//2025-03-16 修复由于类型大写导致无法转换的问题 //2025-03-16 修复由于类型大写导致无法转换的问题
String mysqlType = columnLine.split("\\s+")[1].toLowerCase(); String mysqlType = columnLine.split("\\s+")[1].toLowerCase();
if(mysqlType.contains("(")){ if(mysqlType.contains("(")){
@@ -372,13 +377,13 @@ public class SqlParserServiceImpl implements SqlParserService {
} }
// field commentMySQL的一般位于field行而pgsql和oralce多位于后面。 // field commentMySQL的一般位于field行而pgsql和oralce多位于后面。
String fieldComment = null; String fieldComment = null;
if (tableSql.contains("comment on column") && (tableSql.contains("." + columnName + " is ") || tableSql.contains(".`" + columnName + "` is"))) { if (tableSql.toLowerCase().contains("comment on column") && (tableSql.toLowerCase().contains("." + columnName + " is ") || tableSql.toLowerCase().contains(".`" + columnName + "` is"))) {
//新增对pgsql/oracle的字段备注支持 //新增对pgsql/oracle的字段备注支持
//COMMENT ON COLUMN public.check_info.check_name IS '检查者名称'; //COMMENT ON COLUMN public.check_info.check_name IS '检查者名称';
//2018-11-22 lshz0088 正则表达式的点号前面应该加上两个反斜杠,否则会认为是任意字符 //2018-11-22 lshz0088 正则表达式的点号前面应该加上两个反斜杠,否则会认为是任意字符
//2019-4-29 zhengkai 优化对oracle注释comment on column的支持@liukex //2019-4-29 zhengkai 优化对oracle注释comment on column的支持@liukex
tableSql = tableSql.replaceAll(".`" + columnName + "` is", "." + columnName + " is"); tableSql = tableSql.toLowerCase().replaceAll(".`" + columnName + "` is", "." + columnName + " is");
Matcher columnCommentMatcher = Pattern.compile("\\." + columnName + " is `").matcher(tableSql); Matcher columnCommentMatcher = Pattern.compile("\\." + columnName + " is `").matcher(tableSql.toLowerCase());
fieldComment = columnName; fieldComment = columnName;
while (columnCommentMatcher.find()) { while (columnCommentMatcher.find()) {
String columnCommentTmp = columnCommentMatcher.group(); String columnCommentTmp = columnCommentMatcher.group();
@@ -386,9 +391,9 @@ public class SqlParserServiceImpl implements SqlParserService {
fieldComment = tableSql.substring(tableSql.indexOf(columnCommentTmp) + columnCommentTmp.length()).trim(); fieldComment = tableSql.substring(tableSql.indexOf(columnCommentTmp) + columnCommentTmp.length()).trim();
fieldComment = fieldComment.substring(0, fieldComment.indexOf("`")).trim(); fieldComment = fieldComment.substring(0, fieldComment.indexOf("`")).trim();
} }
} else if (columnLine.contains(" comment")) { } else if (columnLine.toLowerCase().contains(" comment")) {
//20200518 zhengkai 修复包含comment关键字的问题 //20200518 zhengkai 修复包含comment关键字的问题
String commentTmp = columnLine.substring(columnLine.lastIndexOf("comment") + 7).trim(); String commentTmp = columnLine.toLowerCase().substring(columnLine.toLowerCase().lastIndexOf("comment") + 7).trim();
// '用户ID', // '用户ID',
if (commentTmp.contains("`") || commentTmp.indexOf("`") != commentTmp.lastIndexOf("`")) { if (commentTmp.contains("`") || commentTmp.indexOf("`") != commentTmp.lastIndexOf("`")) {
commentTmp = commentTmp.substring(commentTmp.indexOf("`") + 1, commentTmp.lastIndexOf("`")); commentTmp = commentTmp.substring(commentTmp.indexOf("`") + 1, commentTmp.lastIndexOf("`"));
@@ -398,6 +403,9 @@ public class SqlParserServiceImpl implements SqlParserService {
commentTmp = commentTmp.substring(0, commentTmp.lastIndexOf(")") + 1); commentTmp = commentTmp.substring(0, commentTmp.lastIndexOf(")") + 1);
} }
fieldComment = commentTmp; fieldComment = commentTmp;
} else if (columnLine.contains("--")) {
// 支持Oracle风格的注释--
fieldComment = columnLine.substring(columnLine.indexOf("--") + 2).trim();
} else { } else {
//修复comment不存在导致报错的问题 //修复comment不存在导致报错的问题
fieldComment = columnName; fieldComment = columnName;
@@ -416,10 +424,10 @@ public class SqlParserServiceImpl implements SqlParserService {
} }
} }
if (fieldList.size() < 1) { if (fieldList.isEmpty()) {
throw new Exception("表结构分析失败请检查语句或者提交issue给我"); throw new Exception("表结构分析失败请检查语句或者提交issue给我");
} }
//build Class Info
ClassInfo codeJavaInfo = new ClassInfo(); ClassInfo codeJavaInfo = new ClassInfo();
codeJavaInfo.setTableName(tableName); codeJavaInfo.setTableName(tableName);
codeJavaInfo.setClassName(className); codeJavaInfo.setClassName(className);

View File

@@ -27,6 +27,7 @@ public final class mysqlJavaTypeUtil {
//字符串 //字符串
mysqlJavaTypeMap.put("char","String"); mysqlJavaTypeMap.put("char","String");
mysqlJavaTypeMap.put("varchar","String"); mysqlJavaTypeMap.put("varchar","String");
mysqlJavaTypeMap.put("varchar2","String"); // Oracle类型
mysqlJavaTypeMap.put("tinytext","String"); mysqlJavaTypeMap.put("tinytext","String");
mysqlJavaTypeMap.put("text","String"); mysqlJavaTypeMap.put("text","String");
mysqlJavaTypeMap.put("mediumtext","String"); mysqlJavaTypeMap.put("mediumtext","String");
@@ -35,6 +36,8 @@ public final class mysqlJavaTypeUtil {
mysqlJavaTypeMap.put("date","Date"); mysqlJavaTypeMap.put("date","Date");
mysqlJavaTypeMap.put("datetime","Date"); mysqlJavaTypeMap.put("datetime","Date");
mysqlJavaTypeMap.put("timestamp","Date"); mysqlJavaTypeMap.put("timestamp","Date");
// 数字类型 - Oracle增强
mysqlJavaTypeMap.put("number","BigDecimal"); // Oracle的NUMBER类型默认映射为BigDecimal支持精度
mysqlSwaggerTypeMap.put("bigint","integer"); mysqlSwaggerTypeMap.put("bigint","integer");
@@ -46,7 +49,10 @@ public final class mysqlJavaTypeUtil {
mysqlSwaggerTypeMap.put("boolean","boolean"); mysqlSwaggerTypeMap.put("boolean","boolean");
mysqlSwaggerTypeMap.put("float","number"); mysqlSwaggerTypeMap.put("float","number");
mysqlSwaggerTypeMap.put("double","number"); mysqlSwaggerTypeMap.put("double","number");
mysqlSwaggerTypeMap.put("decimal","Double"); mysqlSwaggerTypeMap.put("decimal","number");
// Oracle类型
mysqlSwaggerTypeMap.put("varchar2","string");
mysqlSwaggerTypeMap.put("number","number");
} }
public static HashMap<String, String> getMysqlJavaTypeMap() { public static HashMap<String, String> getMysqlJavaTypeMap() {

View File

@@ -43,11 +43,11 @@ public class ${classInfo.className}Controller {
if(old${classInfo.className}!=null){ if(old${classInfo.className}!=null){
${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first}); ${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first});
}else{ }else{
if(${classInfo.className?uncap_first}Mapper.selectOne(new QueryWrapper<${classInfo.className}>().eq("${classInfo.className?uncap_first}_name",${classInfo.className?uncap_first}.get${classInfo.className}Name()))!=null){ if(${classInfo.className?uncap_first}Mapper.selectOne(new QueryWrapper<${classInfo.className}>().eq("${classInfo.className?uncap_first}_name",${classInfo.className?uncap_first}.get${classInfo.className}Name()))!=null){
return ${returnUtilFailure}("保存失败,名字重复"); return ${returnUtilFailure}("保存失败,名字重复");
} }
${classInfo.className?uncap_first}.setCreateTime(new Date()); ${classInfo.className?uncap_first}.setCreateTime(new Date());
${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first}); ${classInfo.className?uncap_first}Mapper.insert(${classInfo.className?uncap_first});
} }
return ${returnUtilSuccess}("保存成功"); return ${returnUtilSuccess}("保存成功");
} }
@@ -83,43 +83,51 @@ public class ${classInfo.className}Controller {
* 自动分页查询 * 自动分页查询
*/ */
@PostMapping("/list") @PostMapping("/list")
public Object list(String searchParams, public Object list(@RequestBody ${classInfo.className} ${classInfo.className?uncap_first},@RequestParam(required = false, defaultValue = "0") int page,@RequestParam(required = false, defaultValue = "10") int limit) {
@RequestParam(required = false, defaultValue = "0") int page,
@RequestParam(required = false, defaultValue = "10") int limit) {
log.info("page:"+page+"-limit:"+limit+"-json:"+ JSON.toJSONString(searchParams)); log.info("page:"+page+"-limit:"+limit+"-json:"+ JSON.toJSONString(searchParams));
//分页构造器 //分页构造器
Page<${classInfo.className}> buildPage = new Page<${classInfo.className}>(page,limit); Page<${classInfo.className}> buildPage = new Page<${classInfo.className}>(page,limit);
//条件构造器 //条件构造器
QueryWrapper<${classInfo.className}> queryWrapper = new QueryWrapper<${classInfo.className}>(); QueryWrapper<${classInfo.className}> queryWrapper = new QueryWrapper<${classInfo.className}>();
if(StringUtils.isNotEmpty(searchParams)&&JSON.isValid(searchParams)) { if(JSON.stringify(${classInfo.className?uncap_first}).length()>2) {
${classInfo.className} ${classInfo.className?uncap_first} = JSON.parseObject(searchParams, ${classInfo.className}.class); //自行删除不需要动态查询字段
queryWrapper.eq(StringUtils.isNoneEmpty(${classInfo.className?uncap_first}.get${classInfo.className}Name()), "${classInfo.className?uncap_first}_name", ${classInfo.className?uncap_first}.get${classInfo.className}Name()); queryWrapper.
<#list classInfo.fieldList as fieldItem>
eq(StringUtils.isNoneEmpty(${classInfo.className?uncap_first}.get${fieldItem.fieldName}()), "${fieldItem.fieldName}", ${classInfo.className?uncap_first}.get${fieldItem.fieldName}())
</#list>
;
} }
//执行分页 //执行分页
IPage<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Mapper.selectPage(buildPage, queryWrapper); IPage<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Mapper.selectPage(buildPage, queryWrapper);
//返回结果 //返回结果
return ${returnUtil}.PAGE(pageList.getRecords(),pageList.getTotal()); return ${returnUtil}.PAGE(pageList.getRecords(),pageList.getTotal());
} }
/** /**
* 手工分页查询(按需使用) * 动态条件手工分页查询
* 根据对象属性自动构建条件,如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
*/ */
/*@PostMapping("/list2") @PostMapping("/list")
public Object list2(String searchParams, public Object list(String searchParams, @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "10") int limit) {
@RequestParam(required = false, defaultValue = "0") int page, log.info("page:"+page+"-limit:"+limit+"-json:"+ JSON.toJSONString(searchParams));
@RequestParam(required = false, defaultValue = "10") int limit) {
log.info("searchParams:"+ JSON.toJSONString(searchParams)); // 分页参数处理
//通用模式 int offset = (page - 1) * limit;
${classInfo.className} queryParamDTO = JSON.parseObject(searchParams, ${classInfo.className}.class);
//专用DTO模式 // 查询参数处理
//QueryParamDTO queryParamDTO = JSON.parseObject(searchParams, QueryParamDTO.class); ${classInfo.className} queryParamDTO = new ${classInfo.className}();
//queryParamDTO.setPage((page - 1)* limit); if(StringUtils.isNotEmpty(searchParams) && JSON.isValid(searchParams)) {
//queryParamDTO.setLimit(limit); queryParamDTO = JSON.parseObject(searchParams, ${classInfo.className}.class);
//(page - 1) * limit, limit }
List<${classInfo.className}> itemList = ${classInfo.className?uncap_first}Mapper.pageAll(queryParamDTO,(page - 1)* limit,limit);
Integer itemCount = ${classInfo.className?uncap_first}Mapper.countAll(queryParamDTO); // 使用动态条件查询
List<${classInfo.className}> itemList = ${classInfo.className?uncap_first}Mapper.selectPageByCondition(queryParamDTO, offset, limit);
int itemCount = ${classInfo.className?uncap_first}Mapper.selectPageByConditionCount(queryParamDTO);
//返回结果 //返回结果
return ${returnUtilSuccess}.PAGE(itemList,itemCount); return ${returnUtil}.PAGE(itemList, itemCount);
}*/ }
@GetMapping("/list") @GetMapping("/list")
public ModelAndView listPage(){ public ModelAndView listPage(){
return new ModelAndView("${classInfo.className?uncap_first}-list"); return new ModelAndView("${classInfo.className?uncap_first}-list");
@@ -132,16 +140,16 @@ public class ${classInfo.className}Controller {
} }
/** /**
* 发布/暂停(如不需要请屏蔽) * 激活/停用(如不需要请屏蔽)
*/ */
@PostMapping("/publish") @PostMapping("/active")
public Object publish(int id,Integer status){ public Object active(int id,Integer status){
${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Mapper.selectOne(new QueryWrapper<${classInfo.className}>().eq("${classInfo.className?uncap_first}_id",id)); ${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Mapper.selectOne(new QueryWrapper<${classInfo.className}>().eq("${classInfo.className?uncap_first}_id",id));
if(${classInfo.className?uncap_first}!=null){ if(${classInfo.className?uncap_first}!=null){
${classInfo.className?uncap_first}.setUpdateTime(new Date()); ${classInfo.className?uncap_first}.setUpdateTime(new Date());
${classInfo.className?uncap_first}.setStatus(status); ${classInfo.className?uncap_first}.setStatus(status);
${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first}); ${classInfo.className?uncap_first}Mapper.updateById(${classInfo.className?uncap_first});
return ${returnUtilSuccess}((status==1)?"已发布":"已停"); return ${returnUtilSuccess}((status==1)?"已激活":"已停");
}else if(status.equals(${classInfo.className?uncap_first}.getStatus())){ }else if(status.equals(${classInfo.className?uncap_first}.getStatus())){
return ${returnUtilFailure}("状态不正确"); return ${returnUtilFailure}("状态不正确");
}else{ }else{
@@ -150,13 +158,13 @@ public class ${classInfo.className}Controller {
} }
/** /**
* 执行(如不需要请屏蔽) * 测试(如不需要请屏蔽)
*/ */
@PostMapping("/execute") @GetMapping("/test")
public Object execute(){ public Object test(){
return ${returnUtilSuccess}; return ${returnUtilSuccess};
} }
}
} }

View File

@@ -16,7 +16,7 @@ import io.swagger.v3.oas.annotations.media.Schema;</#if>
* @date ${.now?string('yyyy-MM-dd')} * @date ${.now?string('yyyy-MM-dd')}
*/ */
<#if isLombok?exists && isLombok==true>@Data</#if><#if isSwagger?exists && isSwagger==true> <#if isLombok?exists && isLombok==true>@Data</#if><#if isSwagger?exists && isSwagger==true>
@Schema"${classInfo.classComment}")</#if> @Schema(description = "${classInfo.classComment}")</#if>
public class ${classInfo.className} implements Serializable { public class ${classInfo.className} implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@@ -2,6 +2,7 @@
<#if isAutoImport?exists && isAutoImport==true> <#if isAutoImport?exists && isAutoImport==true>
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import ${packageName}.entity.${classInfo.className}; import ${packageName}.entity.${classInfo.className};
import java.util.List; import java.util.List;
@@ -14,25 +15,55 @@ import java.util.List;
@Mapper @Mapper
public interface ${classInfo.className}Mapper extends BaseMapper<${classInfo.className}> { public interface ${classInfo.className}Mapper extends BaseMapper<${classInfo.className}> {
@Select( /**
"<script>select t0.* from ${classInfo.tableName} t0 " + * 动态条件分页查询 - 根据对象属性自动构建条件
//add here if need left join * 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
"where 1=1" + */
<#list classInfo.fieldList as fieldItem > @Select("""
"<when test='${fieldItem.fieldName}!=null and ${fieldItem.fieldName}!=&apos;&apos; '> and t0.${fieldItem.columnName}=井{${fieldItem.fieldName}}</when> " + <script>
</#list> SELECT * FROM ${classInfo.tableName}
//add here if need page limit <where>
//" limit ¥{page},¥{limit} " + <#list classInfo.fieldList as fieldItem>
" </script>") <#if fieldItem.fieldClass?contains("String")>
List<${classInfo.className}> pageAll(${classInfo.className} queryParamDTO,int page,int limit); <if test='queryParamDTO.${fieldItem.fieldName} != null and queryParamDTO.${fieldItem.fieldName} != ""'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
<#else>
<if test='queryParamDTO.${fieldItem.fieldName} != null'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
</#if>
</#list>
</where>
ORDER BY id DESC
LIMIT 井{offset}, 井{limit}
</script>
""")
List<${classInfo.className}> selectPageByCondition(@Param("queryParamDTO") ${classInfo.className} queryParamDTO,
@Param("offset") int offset,
@Param("limit") int limit);
@Select("<script>select count(1) from ${classInfo.tableName} t0 " + /**
//add here if need left join * 动态条件分页查询总数
"where 1=1" + */
<#list classInfo.fieldList as fieldItem > @Select("""
"<when test='${fieldItem.fieldName}!=null and ${fieldItem.fieldName}!=&apos;&apos; '> and t0.${fieldItem.columnName}=井{${fieldItem.fieldName}}</when> " + <script>
</#list> SELECT COUNT(*) FROM ${classInfo.tableName}
" </script>") <where>
int countAll(${classInfo.className} queryParamDTO); <#list classInfo.fieldList as fieldItem>
<#if fieldItem.fieldClass?contains("String")>
<if test='queryParamDTO.${fieldItem.fieldName} != null and queryParamDTO.${fieldItem.fieldName} != ""'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
<#else>
<if test='queryParamDTO.${fieldItem.fieldName} != null'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
</#if>
</#list>
</where>
</script>
""")
int selectPageByConditionCount(@Param("queryParamDTO") ${classInfo.className} queryParamDTO);
} }

View File

@@ -57,9 +57,9 @@ public class ${classInfo.className}Controller {
* @author ${authorName} * @author ${authorName}
* @date ${.now?string('yyyy/MM/dd')} * @date ${.now?string('yyyy/MM/dd')}
**/ **/
@RequestMapping("/load") @RequestMapping("/find")
public Object load(int id){ public Object find(int id){
return ${classInfo.className?uncap_first}Service.load(id); return ${classInfo.className?uncap_first}Service.find(id);
} }
/** /**
@@ -73,4 +73,34 @@ public class ${classInfo.className}Controller {
return ${classInfo.className?uncap_first}Service.pageList(offset, pagesize); return ${classInfo.className?uncap_first}Service.pageList(offset, pagesize);
} }
/**
* 查询 动态条件分页查询 - 根据对象属性自动构建条件
* 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
* @author ${authorName}
* @date ${.now?string('yyyy/MM/dd')}
**/
@RequestMapping("/pageByCondition")
public Map<String, Object> pageByCondition(${classInfo.className} queryParamDTO,
@RequestParam(required = false, defaultValue = "0") int offset,
@RequestParam(required = false, defaultValue = "10") int pagesize) {
return ${classInfo.className?uncap_first}Service.pageByCondition(queryParamDTO, offset, pagesize);
}
/**
* 激活/停用
* @author ${authorName}
* @date ${.now?string('yyyy/MM/dd')}
**/
@RequestMapping("/active")
public Object active(int id, Integer status) {
${classInfo.className} ${classInfo.className?uncap_first} = ${classInfo.className?uncap_first}Service.find(id);
if(${classInfo.className?uncap_first} != null) {
${classInfo.className?uncap_first}.setStatus(status);
Object result = ${classInfo.className?uncap_first}Service.update(${classInfo.className?uncap_first});
return result;
} else {
return ${returnUtilFailure}("未找到记录");
}
}
} }

View File

@@ -40,7 +40,7 @@ public interface ${classInfo.className}Mapper {
* @author ${authorName} * @author ${authorName}
* @date ${.now?string('yyyy/MM/dd')} * @date ${.now?string('yyyy/MM/dd')}
**/ **/
${classInfo.className} load(int id); ${classInfo.className} find(int id);
/** /**
* 查询 分页查询 * 查询 分页查询

View File

@@ -13,23 +13,33 @@ import java.util.List;
@Repository @Repository
public interface ${classInfo.className}Mapper { public interface ${classInfo.className}Mapper {
@Select("select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}") @Select("""
public ${classInfo.className} getById(Integer id); select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}
""")
public ${classInfo.className} find(Integer id);
@Options(useGeneratedKeys=true,keyProperty="${classInfo.className?uncap_first}Id") @Options(useGeneratedKeys=true,keyProperty="${classInfo.className?uncap_first}Id")
@Insert("insert into ${classInfo.tableName}" + @Insert("""
" (<#list classInfo.fieldList as fieldItem >${fieldItem.columnName}<#if fieldItem_has_next>,</#if></#list>)" + insert into ${classInfo.tableName} (
" values(<#list classInfo.fieldList as fieldItem >${fieldItem.fieldName}<#if fieldItem_has_next>,<#else>)</#if></#list>") <#list classInfo.fieldList as fieldItem >${fieldItem.columnName}<#if fieldItem_has_next>,</#if></#list>
) values (
<#list classInfo.fieldList as fieldItem >井{${fieldItem.fieldName}}<#if fieldItem_has_next>,<#else>)</#if></#list>
)
""")
public Integer insert(${classInfo.className} ${classInfo.className?uncap_first}); public Integer insert(${classInfo.className} ${classInfo.className?uncap_first});
@Delete(value = "delete from ${classInfo.tableName} where ${classInfo.tableName}_id=井{${classInfo.className?uncap_first}Id}") @Delete("""
delete from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}
""")
boolean delete(Integer id); boolean delete(Integer id);
@Update(value = "update ${classInfo.tableName} set " @Update("""
<#list classInfo.fieldList as fieldItem > update ${classInfo.tableName} set
<#if fieldItem.columnName != "id">+" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next>,</#if>"</#if> <#list classInfo.fieldList as fieldItem >
</#list> <#if fieldItem.columnName != "id">${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next>,</#if></#if>
+" where ${classInfo.tableName}_id=井{${classInfo.className?uncap_first}Id} ") </#list>
where ${classInfo.tableName}_id=井{id}
""")
boolean update(${classInfo.className} ${classInfo.className?uncap_first}); boolean update(${classInfo.className} ${classInfo.className?uncap_first});
@@ -38,19 +48,73 @@ public interface ${classInfo.className}Mapper {
@Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>,</#if> @Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>,</#if>
</#list> </#list>
}) })
@Select(value = "select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{queryParam}") @Select("""
${classInfo.className} selectOne(String queryParam); select * from ${classInfo.tableName} where ${classInfo.tableName}_id=井{id}
""")
${classInfo.className} selectOne(Integer id);
@Results(value = { @Results(value = {
<#list classInfo.fieldList as fieldItem > <#list classInfo.fieldList as fieldItem >
@Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>,</#if> @Result(property = "${fieldItem.fieldName}", column = "${fieldItem.columnName}")<#if fieldItem_has_next>,</#if>
</#list> </#list>
}) })
@Select(value = "select * from ${classInfo.tableName} where " @Select("""
<#list classInfo.fieldList as fieldItem > select * from ${classInfo.tableName} where
+" ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next> or </#if>" <#list classInfo.fieldList as fieldItem >
</#list> ${fieldItem.columnName}=井{${fieldItem.fieldName}}<#if fieldItem_has_next> or </#if>
) </#list>
""")
List<${classInfo.className}> selectList(${classInfo.className} ${classInfo.className?uncap_first}); List<${classInfo.className}> selectList(${classInfo.className} ${classInfo.className?uncap_first});
/**
* 动态条件分页查询 - 根据对象属性自动构建条件
* 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
*/
@Select("""
<script>
SELECT * FROM ${classInfo.tableName}
<where>
<#list classInfo.fieldList as fieldItem>
<#if fieldItem.fieldClass?contains("String")>
<if test='queryParamDTO.${fieldItem.fieldName} != null and queryParamDTO.${fieldItem.fieldName} != ""'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
<#else>
<if test='queryParamDTO.${fieldItem.fieldName} != null'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
</#if>
</#list>
</where>
ORDER BY id DESC
LIMIT 井{offset}, 井{limit}
</script>
""")
List<${classInfo.className}> pageByCondition(@Param("queryParamDTO") ${classInfo.className} queryParamDTO,
@Param("offset") int offset,
@Param("limit") int limit);
/**
* 动态条件分页查询总数
*/
@Select("""
<script>
SELECT COUNT(*) FROM ${classInfo.tableName}
<where>
<#list classInfo.fieldList as fieldItem>
<#if fieldItem.fieldClass?contains("String")>
<if test='queryParamDTO.${fieldItem.fieldName} != null and queryParamDTO.${fieldItem.fieldName} != ""'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
<#else>
<if test='queryParamDTO.${fieldItem.fieldName} != null'>
AND ${fieldItem.columnName} = 井{queryParamDTO.${fieldItem.fieldName}}
</if>
</#if>
</#list>
</where>
</script>
""")
int pageByConditionCount(@Param("queryParamDTO") ${classInfo.className} queryParamDTO);
} }

View File

@@ -26,11 +26,17 @@ public interface ${classInfo.className}Service {
/** /**
* 根据主键 id 查询 * 根据主键 id 查询
*/ */
public ${classInfo.className} load(int id); public ${classInfo.className} find(int id);
/** /**
* 分页查询 * 分页查询
*/ */
public Map<String,Object> pageList(int offset, int pagesize); public Map<String,Object> pageList(int offset, int pagesize);
/**
* 动态条件分页查询 - 根据对象属性自动构建条件
* 如果字段有值则进行分页+指定条件查询,否则仅进行分页查询
*/
public Map<String,Object> pageByCondition(${classInfo.className} queryParamDTO, int offset, int pagesize);
} }

View File

@@ -46,8 +46,8 @@ public class ${classInfo.className}ServiceImpl implements ${classInfo.className}
@Override @Override
public ${classInfo.className} load(int id) { public ${classInfo.className} find(int id) {
return ${classInfo.className?uncap_first}Mapper.load(id); return ${classInfo.className?uncap_first}Mapper.find(id);
} }
@@ -65,4 +65,18 @@ public class ${classInfo.className}ServiceImpl implements ${classInfo.className}
return result; return result;
} }
@Override
public Map<String,Object> pageByCondition(${classInfo.className} queryParamDTO, int offset, int pagesize) {
List<${classInfo.className}> pageList = ${classInfo.className?uncap_first}Mapper.pageByCondition(queryParamDTO, offset, pagesize);
int totalCount = ${classInfo.className?uncap_first}Mapper.pageByConditionCount(queryParamDTO);
// result
Map<String, Object> result = new HashMap<String, Object>();
result.put("pageList", pageList);
result.put("totalCount", totalCount);
return result;
}
} }