mirror of
https://gitee.com/ChinaLym/shoulder-platform.git
synced 2025-12-30 11:02:26 +00:00
feat(code-gen): 代码生成器
This commit is contained in:
@@ -44,7 +44,7 @@ public class GeneratorApp {
|
|||||||
private final SysGeneratorService sysGeneratorService;
|
private final SysGeneratorService sysGeneratorService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列出数据库中所有表
|
* ===========【 列出数据库中所有表 】===========
|
||||||
* <a href="http://localhost:8080/generator/list?page=1&limit=100">列出数据库中所有表</a>
|
* <a href="http://localhost:8080/generator/list?page=1&limit=100">列出数据库中所有表</a>
|
||||||
*/
|
*/
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
@@ -54,9 +54,9 @@ public class GeneratorApp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 生成代码
|
* ===========【 生成代码 】===========
|
||||||
* web 中不需要主动关闭流
|
* web 中不需要主动关闭流
|
||||||
* <a href="http://localhost:8080/generator/code?tables=_all">所有表</a>
|
* <a href="http://localhost:8080/generator/code?tables=_all">所有表生成代码</a>
|
||||||
* @param tables 表名,逗号分隔,_all 全部
|
* @param tables 表名,逗号分隔,_all 全部
|
||||||
*/
|
*/
|
||||||
@RequestMapping("code")
|
@RequestMapping("code")
|
||||||
|
|||||||
@@ -29,7 +29,9 @@
|
|||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="queryColumns" resultType="map">
|
<select id="queryColumns" resultType="map">
|
||||||
select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra from information_schema.columns
|
select column_name columnName, data_type dataType, column_comment columnComment, column_key columnKey, extra ,
|
||||||
|
CHARACTER_MAXIMUM_LENGTH charLength, COLUMN_TYPE columnType, IS_NULLABLE isNullable
|
||||||
|
from information_schema.columns
|
||||||
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position
|
where table_name = #{tableName} and table_schema = (select database()) order by ordinal_position
|
||||||
</select>
|
</select>
|
||||||
</mapper>
|
</mapper>
|
||||||
@@ -25,4 +25,15 @@ public class TableEntity {
|
|||||||
//类名(第一个字母小写),如:sys_user => sysUser
|
//类名(第一个字母小写),如:sys_user => sysUser
|
||||||
private String lowClassName;
|
private String lowClassName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Controller 中生成校验权限注解(Spring Security)
|
||||||
|
*/
|
||||||
|
private boolean checkAuth;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 表类型:
|
||||||
|
* TREE(parent_id), CREATOR, MODIFIER, CREATOR_TIME, UPDATE_TIME, BATCH
|
||||||
|
*/
|
||||||
|
private List<String> types;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,15 +33,21 @@ public class GenUtils {
|
|||||||
*/
|
*/
|
||||||
public static List<String> getTemplates() {
|
public static List<String> getTemplates() {
|
||||||
List<String> templates = new ArrayList<String>();
|
List<String> templates = new ArrayList<String>();
|
||||||
|
templates.add("template/Controller.java.vm");
|
||||||
|
|
||||||
templates.add("template/JpaEntity.java.vm");
|
templates.add("template/JpaEntity.java.vm");
|
||||||
templates.add("template/JpaRepository.java.vm");
|
templates.add("template/JpaRepository.java.vm");
|
||||||
|
|
||||||
|
templates.add("template/Model.java.vm");
|
||||||
|
templates.add("template/DTO.java.vm");
|
||||||
templates.add("template/PO.java.vm");
|
templates.add("template/PO.java.vm");
|
||||||
templates.add("template/Mapper.java.vm");
|
templates.add("template/Converter.java.vm");
|
||||||
templates.add("template/Mapper.xml.vm");
|
|
||||||
templates.add("template/Service.java.vm");
|
templates.add("template/Service.java.vm");
|
||||||
templates.add("template/ServiceImpl.java.vm");
|
templates.add("template/ServiceImpl.java.vm");
|
||||||
templates.add("template/Controller.java.vm");
|
|
||||||
|
templates.add("template/Mapper.java.vm");
|
||||||
|
templates.add("template/Mapper.xml.vm");
|
||||||
|
|
||||||
templates.add("template/index.html.vm");
|
templates.add("template/index.html.vm");
|
||||||
|
|
||||||
@@ -131,6 +137,9 @@ public class GenUtils {
|
|||||||
columnEntity.setDataType(column.get("dataType"));
|
columnEntity.setDataType(column.get("dataType"));
|
||||||
columnEntity.setComments(column.get("columnComment"));
|
columnEntity.setComments(column.get("columnComment"));
|
||||||
columnEntity.setExtra(column.get("extra"));
|
columnEntity.setExtra(column.get("extra"));
|
||||||
|
String len = String.valueOf(column.get("charLength"));
|
||||||
|
columnEntity.setLength("null".equalsIgnoreCase(len) ? 0 : Integer.parseInt(len));
|
||||||
|
columnEntity.setNotEmpty("NO".equalsIgnoreCase(column.get("isNullable")));
|
||||||
|
|
||||||
//列名转换成Java属性名
|
//列名转换成Java属性名
|
||||||
String attrName = columnToJava(columnEntity.getColumnName());
|
String attrName = columnToJava(columnEntity.getColumnName());
|
||||||
@@ -197,10 +206,22 @@ public class GenUtils {
|
|||||||
return packagePath + "entity" + File.separator + className + "Entity.java";
|
return packagePath + "entity" + File.separator + className + "Entity.java";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (template.contains("Model.java.vm")) {
|
||||||
|
return packagePath + "model" + File.separator + className + ".java";
|
||||||
|
}
|
||||||
|
|
||||||
if (template.contains("PO.java.vm")) {
|
if (template.contains("PO.java.vm")) {
|
||||||
return packagePath + "po" + File.separator + className + "PO.java";
|
return packagePath + "po" + File.separator + className + "PO.java";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (template.contains("DTO.java.vm")) {
|
||||||
|
return packagePath + "dto" + File.separator + className + "DTO.java";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (template.contains("Converter.java.vm")) {
|
||||||
|
return packagePath + "convert" + File.separator + className + "Converter.java";
|
||||||
|
}
|
||||||
|
|
||||||
if (template.contains("Mapper.java.vm")) {
|
if (template.contains("Mapper.java.vm")) {
|
||||||
return packagePath + "dao" + File.separator + className + "Mapper.java";
|
return packagePath + "dao" + File.separator + className + "Mapper.java";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,17 +3,18 @@ package ${package}.${pkgName}.controller;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
#if(${checkAuth})
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
||||||
|
#end
|
||||||
import org.shoulder.core.dto.response.PageResult;
|
import org.shoulder.core.dto.response.PageResult;
|
||||||
import org.shoulder.core.dto.response.RestResult;
|
import org.shoulder.core.dto.response.RestResult;
|
||||||
|
|
||||||
import ${package}.${pkgName}.po.${className};
|
import ${package}.${pkgName}.convert.${className}Converter;
|
||||||
|
import ${package}.${pkgName}.dto.${className}DTO;
|
||||||
|
import ${package}.${pkgName}.model.${className};
|
||||||
import ${package}.${pkgName}.service.${className}Service;
|
import ${package}.${pkgName}.service.${className}Service;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -35,50 +36,80 @@ public class ${className}Controller {
|
|||||||
* @param condition 查询条件
|
* @param condition 查询条件
|
||||||
* @return 分页结果
|
* @return 分页结果
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/list")
|
@RequestMapping("list")
|
||||||
|
#if(${checkAuth})
|
||||||
@PreAuthorize("hasAnyAuthority('${tableName}:${pathName}:list')")
|
@PreAuthorize("hasAnyAuthority('${tableName}:${pathName}:list')")
|
||||||
|
#end
|
||||||
public PageResult list(@RequestParam Map<String, Object> condition){
|
public PageResult list(@RequestParam Map<String, Object> condition){
|
||||||
PageResult pageResult = ${lowClassName}Service.findAll(condition);
|
return ${lowClassName}Service.findAll(condition);
|
||||||
return pageResult;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存单个
|
* 保存单个,推荐使用幂等的 PUT 方法,体现接口幂等性。为了方便习惯,也开放了 POST
|
||||||
* @param ${lowClassName} 新增数据
|
*
|
||||||
|
* @param ${lowClassName}DTO 新增数据
|
||||||
* @return 保存成功
|
* @return 保存成功
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/save")
|
@RequestMapping(value = "save", method = {RequestMethod.PUT, RequestMethod.POST})
|
||||||
|
#if(${checkAuth})
|
||||||
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:save')")
|
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:save')")
|
||||||
public RestResult save(@RequestBody ${className} ${lowClassName}){
|
#end
|
||||||
${lowClassName}Service.save(${lowClassName});
|
public RestResult save(@RequestBody ${className}DTO ${lowClassName}DTO){
|
||||||
|
${className} ${lowClassName} = ${className}Converter.CONVERTER.fromDTO(${lowClassName}DTO);
|
||||||
|
${lowClassName}Service.save(${lowClassName});
|
||||||
|
|
||||||
return RestResult.succeed();
|
return RestResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 单个修改
|
* 单个修改
|
||||||
* @param ${lowClassName} 修改属性
|
*
|
||||||
|
* @param ${lowClassName}DTO 修改属性
|
||||||
* @return 修改成功
|
* @return 修改成功
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/update")
|
@PostMapping("update")
|
||||||
|
#if(${checkAuth})
|
||||||
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:update')")
|
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:update')")
|
||||||
public RestResult update(@RequestBody ${className} ${lowClassName}){
|
#end
|
||||||
|
public RestResult update(@RequestBody ${className}DTO ${lowClassName}DTO){
|
||||||
|
${className} ${lowClassName} = ${className}Converter.CONVERTER.fromDTO(${lowClassName}DTO);
|
||||||
${lowClassName}Service.update(${lowClassName});
|
${lowClassName}Service.update(${lowClassName});
|
||||||
|
|
||||||
return RestResult.succeed();
|
return RestResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据 id 删除单个
|
* 根据 id 删除单个
|
||||||
|
*
|
||||||
* @param ${pk.attributeName} id
|
* @param ${pk.attributeName} id
|
||||||
* @return 删除成功
|
* @return 删除成功
|
||||||
*/
|
*/
|
||||||
@RequestMapping("/delete/{id}")
|
@RequestMapping(value = "delete/{id}", method = {RequestMethod.DELETE, RequestMethod.POST})
|
||||||
|
#if(${checkAuth})
|
||||||
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:delete')")
|
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:delete')")
|
||||||
|
#end
|
||||||
public RestResult delete(@PathVariable Long ${pk.attributeName}){
|
public RestResult delete(@PathVariable Long ${pk.attributeName}){
|
||||||
${lowClassName}Service.delete(${pk.attributeName});
|
${lowClassName}Service.deleteById(${pk.attributeName});
|
||||||
return RestResult.succeed();
|
return RestResult.success();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 id 批量删除
|
||||||
|
*
|
||||||
|
* @param idList ids
|
||||||
|
* @return 删除成功
|
||||||
|
*/
|
||||||
|
@RequestMapping(value = "delete", method = {RequestMethod.DELETE, RequestMethod.POST})
|
||||||
|
#if(${checkAuth})
|
||||||
|
@PreAuthorize("hasAnyAuthority('resource:sysroleuser:delete')")
|
||||||
|
#end
|
||||||
|
public RestResult delete(List<Long> idList){
|
||||||
|
if(CollectionUtils.isEmpty(idList)){
|
||||||
|
return RestResult.success();
|
||||||
|
}
|
||||||
|
${lowClassName}Service.deleteByIds(idList);
|
||||||
|
return RestResult.success();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
package ${package}.${pkgName}.convert;
|
||||||
|
|
||||||
|
import ${package}.${pkgName}.model.${className};
|
||||||
|
import ${package}.${pkgName}.po.${className}PO;
|
||||||
|
import ${package}.${pkgName}.dto.${className}DTO;
|
||||||
|
import org.mapstruct.Mapper;
|
||||||
|
import org.mapstruct.factory.Mappers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ${comments} 模型转换
|
||||||
|
*
|
||||||
|
* @author ${author}
|
||||||
|
* @date ${datetime}
|
||||||
|
*/
|
||||||
|
@Mapper(componentModel = "spring") // 置为 spring 默认作为 bean 注入 Spring 容器中
|
||||||
|
public interface ${className}Converter {
|
||||||
|
|
||||||
|
${className}Converter CONVERTER = Mappers.getMapper(${className}Converter.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* DTO 转为标准模型,用于接口传入时
|
||||||
|
* @param dto 传输层对象
|
||||||
|
* @return 标准模型
|
||||||
|
*/
|
||||||
|
${className} fromDTO(${className}DTO dto);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* PO 转为标准模型,用于存储中返回查询结果后
|
||||||
|
* @param po 持久层对象
|
||||||
|
* @return 标准模型
|
||||||
|
*/
|
||||||
|
${className} fromPO(${className}PO po);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标准模型转为 DTO,用于接口返回查询结果时,转化为视图层对象
|
||||||
|
* @param model 标准模型
|
||||||
|
* @return 传输层对象
|
||||||
|
*/
|
||||||
|
${className}DTO toDTO(${className} model);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标准模型转为 PO,用于存储入库前
|
||||||
|
* @param model 标准模型
|
||||||
|
* @return 持久层对象
|
||||||
|
*/
|
||||||
|
${className}PO toPO(${className} model);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -8,6 +8,9 @@ import lombok.Builder;
|
|||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import javax.validation.constraints.NotNull;
|
||||||
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
|
||||||
#if(${hasBigDecimal})
|
#if(${hasBigDecimal})
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
#end
|
#end
|
||||||
@@ -15,12 +18,11 @@ import java.io.Serializable;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${comments}
|
* ${comments} 传输层定义 分为 param(add / update)、vo
|
||||||
*
|
*
|
||||||
* @author ${author}
|
* @author ${author}
|
||||||
* @date ${datetime}
|
* @date ${datetime}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@@ -29,18 +31,17 @@ import java.util.Date;
|
|||||||
@ApiModel(value = "${className}DTO", description = "${tableName}")
|
@ApiModel(value = "${className}DTO", description = "${tableName}")
|
||||||
public class ${className}DTO implements Serializable {
|
public class ${className}DTO implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
#foreach ($column in $columns)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $column.comments
|
* $column.comments
|
||||||
*/
|
*/
|
||||||
#if($column.notEmpty)
|
#if($column.notEmpty)
|
||||||
@NotNull(message = "$column.comments 不能为空")
|
@NotNull(message = "$column.comments 不能为空")
|
||||||
#end
|
#end
|
||||||
#if($column.length > 0)
|
#if($column.length > 0)
|
||||||
@Length(max = $column.length, message = "$column.comments 长度不能超过 $column.length")
|
@Length(max = $column.length, message = "$column.comments 长度不能超过 $column.length")
|
||||||
#end
|
#end
|
||||||
@ApiModelProperty(value = "$column.comments", notes = "$column.comments")
|
@ApiModelProperty(value = "$column.comments", notes = "$column.comments")
|
||||||
private $column.attrType $column.attributeName;
|
private $column.attrType $column.attributeName;
|
||||||
#end
|
#end
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package ${package}.${pkgName}.entity;
|
package ${package}.${pkgName}.entity;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import org.hibernate.annotations.GenericGenerator;
|
import org.hibernate.annotations.GenericGenerator;
|
||||||
@@ -20,18 +23,20 @@ import java.util.Date;
|
|||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@Builder
|
||||||
@Table// 唯一索引 (uniqueConstraints = {@UniqueConstraint(name = "uk_$pk.columnName", columnNames = {"$pk.columnName"})})
|
@Table// 唯一索引 (uniqueConstraints = {@UniqueConstraint(name = "uk_$pk.columnName", columnNames = {"$pk.columnName"})})
|
||||||
public class ${className}Entity implements Serializable {
|
public class ${className}Entity implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
#foreach ($column in $columns)
|
||||||
|
|
||||||
#if($column.columnName == $pk.columnName)
|
#if($column.columnName == $pk.columnName)
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
|
@GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
|
||||||
@GenericGenerator(name = "native", strategy = "native")
|
@GenericGenerator(name = "native", strategy = "native")
|
||||||
#end
|
#end
|
||||||
private $column.attrType $column.attributeName;
|
private $column.attrType $column.attributeName;
|
||||||
#end
|
#end
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
|||||||
import org.springframework.data.jpa.repository.Modifying;
|
import org.springframework.data.jpa.repository.Modifying;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
|
||||||
|
import ${package}.${pkgName}.entity.${className}Entity;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
package ${package}.${pkgName}.dao;
|
package ${package}.${pkgName}.dao;
|
||||||
|
|
||||||
import ${package}.${pkgName}.po.${className};
|
import ${package}.${pkgName}.po.${className}PO;
|
||||||
import org.apache.ibatis.annotations.Mapper;
|
import org.apache.ibatis.annotations.Mapper;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ${comments}
|
* ${comments}
|
||||||
*
|
*
|
||||||
@@ -14,12 +15,44 @@ import java.util.Map;
|
|||||||
@Mapper
|
@Mapper
|
||||||
public interface ${className}Mapper {
|
public interface ${className}Mapper {
|
||||||
|
|
||||||
int insert(${className}PO ${lowClassName});
|
/**
|
||||||
|
* 插入单条记录
|
||||||
|
*
|
||||||
|
* @param ${lowClassName}PO 要添加的
|
||||||
|
* @return 数据库影响行数
|
||||||
|
*/
|
||||||
|
int insert(${className}PO ${lowClassName}PO);
|
||||||
|
|
||||||
int update(${className}PO ${lowClassName});
|
/**
|
||||||
|
* 更新单个
|
||||||
|
*
|
||||||
|
* @param ${lowClassName}PO 要更新的
|
||||||
|
* @return 数据库影响行数
|
||||||
|
*/
|
||||||
|
int update(${className}PO ${lowClassName}PO);
|
||||||
|
|
||||||
int delete(Long id);
|
/**
|
||||||
|
* 根据 id 删除单个
|
||||||
|
*
|
||||||
|
* @param id 要删除的数据 id
|
||||||
|
* @return 数据库影响行数
|
||||||
|
*/
|
||||||
|
int deleteById(Long id);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据 ids 批量删除
|
||||||
|
*
|
||||||
|
* @param ids 要删除的数据 id
|
||||||
|
* @return 数据库影响行数
|
||||||
|
*/
|
||||||
|
int deleteByIds(List<Long> ids);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询
|
||||||
|
*
|
||||||
|
* @param params 查询条件
|
||||||
|
* @return 查询结果
|
||||||
|
*/
|
||||||
List<${className}PO> findAll(Map<String, Object> params);
|
List<${className}PO> findAll(Map<String, Object> params);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,42 +10,64 @@
|
|||||||
#end
|
#end
|
||||||
</resultMap>
|
</resultMap>
|
||||||
|
|
||||||
|
#if(${baseColumnList})
|
||||||
|
<!-- 通用查询结果列 -->
|
||||||
|
<sql id="Base_Column_List">
|
||||||
|
${pk}
|
||||||
|
#foreach($column in $columns)
|
||||||
|
, ${column.columnName}
|
||||||
|
#end
|
||||||
|
</sql>
|
||||||
|
#end
|
||||||
|
|
||||||
<insert id="save">
|
<insert id="save">
|
||||||
insert into ${tableName}(
|
insert into ${tableName}(
|
||||||
#foreach($column in $columns)
|
#foreach($column in $columns)
|
||||||
#if( $!{velocityCount} == $!{columns.size()})
|
#if( $!{velocityCount} == $!{columns.size()})
|
||||||
${column.columnName}
|
${column.columnName}
|
||||||
#else
|
#else
|
||||||
${column.columnName},
|
${column.columnName},
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
) values (
|
) values (
|
||||||
#foreach($column in $columns)
|
#foreach($column in $columns)
|
||||||
#if( $!{velocityCount} == $!{columns.size()})
|
#if( $!{velocityCount} == $!{columns.size()})
|
||||||
#{${column.columnName}}
|
#{${column.columnName}}
|
||||||
#else
|
#else
|
||||||
#{ ${column.columnName}},
|
#{ ${column.columnName}},
|
||||||
#end
|
#end
|
||||||
#end
|
#end
|
||||||
)
|
)
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<update id="update">
|
<update id="update">
|
||||||
update ${tableName}
|
update ${tableName}
|
||||||
<set>
|
<set>
|
||||||
#foreach($column in $columns)
|
#foreach($column in $columns)
|
||||||
<if test="${column.columnName} != null">
|
<if test="${column.columnName} != null">
|
||||||
${column.columnName} = #{${column.columnName}},
|
${column.columnName} = #{${column.columnName}},
|
||||||
</if>
|
</if>
|
||||||
#end
|
#end
|
||||||
</set>
|
</set>
|
||||||
where id = #{id}
|
where id = #{id}
|
||||||
</update>
|
</update>
|
||||||
|
|
||||||
|
<!-- 根据传入 id 删除单个 -->
|
||||||
<delete id="delete" parameterType="long" flushCache="true">
|
<delete id="delete" parameterType="long" flushCache="true">
|
||||||
delete from ${tableName} where id = #{id}
|
delete from ${tableName} where id = #{id}
|
||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
|
<!-- 根据传入 id 批量删除 -->
|
||||||
|
<delete id="deleteByIds" parameterType="java.util.List" flushCache="true">
|
||||||
|
delete from ${tableName}
|
||||||
|
<foreach collection="list" item="ids" index="index" separator="or" open="(" close=")" >
|
||||||
|
id in
|
||||||
|
<foreach collection="ids" item="id" index="index" open="(" close=")" separator="," >
|
||||||
|
#{id}
|
||||||
|
</foreach>
|
||||||
|
</foreach>
|
||||||
|
</delete>
|
||||||
|
|
||||||
<select id="findAll" resultType="${package}.${pkgName}.po.${className}PO">
|
<select id="findAll" resultType="${package}.${pkgName}.po.${className}PO">
|
||||||
select * from ${tableName} t
|
select * from ${tableName} t
|
||||||
<include refid="where"/>
|
<include refid="where"/>
|
||||||
@@ -54,11 +76,11 @@
|
|||||||
|
|
||||||
<sql id="where">
|
<sql id="where">
|
||||||
<where>
|
<where>
|
||||||
#foreach($column in $columns)
|
#foreach($column in $columns)
|
||||||
<if test="searchKey != null and searchKey != '' and searchKey=='${column.columnName}'">
|
<if test="searchKey != null and searchKey != '' and searchKey=='${column.columnName}'">
|
||||||
and t.${column.columnName} like concat('%', #{searchValue}, '%')
|
and t.${column.columnName} like concat('%', #{searchValue}, '%')
|
||||||
</if>
|
</if>
|
||||||
#end
|
#end
|
||||||
</where>
|
</where>
|
||||||
</sql>
|
</sql>
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
package ${package}.${pkgName}.model;
|
package ${package}.${pkgName}.model;
|
||||||
|
|
||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import lombok.AllArgsConstructor;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import lombok.experimental.Accessors;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@@ -18,21 +18,18 @@ import java.util.Date;
|
|||||||
* @author ${author}
|
* @author ${author}
|
||||||
* @date ${datetime}
|
* @date ${datetime}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@Builder
|
||||||
public class ${className} implements Serializable {
|
public class ${className} implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
#foreach ($column in $columns)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $column.columnName $column.comments
|
* $column.columnName $column.comments
|
||||||
*/
|
*/
|
||||||
#if($column.columnName == $pk.columnName)
|
|
||||||
@TableId(type = IdType.AUTO)
|
|
||||||
#end
|
|
||||||
@TableField("$column.columnName")
|
|
||||||
private $column.attrType $column.attributeName;
|
private $column.attrType $column.attributeName;
|
||||||
#end
|
#end
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,11 @@ package ${package}.${pkgName}.po;
|
|||||||
import com.baomidou.mybatisplus.annotation.IdType;
|
import com.baomidou.mybatisplus.annotation.IdType;
|
||||||
import com.baomidou.mybatisplus.annotation.TableField;
|
import com.baomidou.mybatisplus.annotation.TableField;
|
||||||
import com.baomidou.mybatisplus.annotation.TableId;
|
import com.baomidou.mybatisplus.annotation.TableId;
|
||||||
|
import com.baomidou.mybatisplus.annotation.TableName;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
@@ -18,20 +23,22 @@ import java.util.Date;
|
|||||||
* @author ${author}
|
* @author ${author}
|
||||||
* @date ${datetime}
|
* @date ${datetime}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
|
@Accessors(chain = true)
|
||||||
|
@Builder
|
||||||
|
@TableName("${tableName}")
|
||||||
public class ${className}PO implements Serializable {
|
public class ${className}PO implements Serializable {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
#foreach ($column in $columns)
|
#foreach ($column in $columns)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* $column.columnName $column.comments
|
* $column.columnName $column.comments
|
||||||
*/
|
*/
|
||||||
#if($column.columnName == $pk.columnName)
|
#if($column.columnName == $pk.columnName)
|
||||||
@TableId(type = IdType.AUTO)
|
@TableId(type = IdType.AUTO)
|
||||||
#end
|
#end
|
||||||
@TableField("$column.columnName")
|
@TableField("$column.columnName")
|
||||||
private $column.attrType $column.attributeName;
|
private $column.attrType $column.attributeName;
|
||||||
#end
|
#end
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package ${package}.${pkgName}.service;
|
package ${package}.${pkgName}.service;
|
||||||
|
|
||||||
import ${package}.${pkgName}.po.${className};
|
import ${package}.${pkgName}.dto.${className}DTO;
|
||||||
|
import ${package}.${pkgName}.model.${className};
|
||||||
|
import ${package}.${pkgName}.po.${className}PO;
|
||||||
import org.shoulder.core.dto.response.PageResult;
|
import org.shoulder.core.dto.response.PageResult;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,29 +15,44 @@ import java.util.Map;
|
|||||||
* @date ${datetime}
|
* @date ${datetime}
|
||||||
*/
|
*/
|
||||||
public interface ${className}Service {
|
public interface ${className}Service {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加
|
* 添加单个
|
||||||
* @param ${lowClassName}
|
*
|
||||||
|
* @param ${lowClassName} 要添加的
|
||||||
|
* @return 数据库影响行数
|
||||||
*/
|
*/
|
||||||
int save(${className} ${lowClassName});
|
int save(${className} ${lowClassName});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 修改
|
* 修改单个
|
||||||
* @param ${lowClassName}
|
*
|
||||||
|
* @param ${lowClassName} 修改
|
||||||
|
* @return 数据库影响行数
|
||||||
*/
|
*/
|
||||||
int update(${className}PO ${lowClassName});
|
int update(${className} ${lowClassName});
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除
|
* 根据 id 删除单个
|
||||||
* @param id
|
*
|
||||||
|
* @param id 要删除的数据 id
|
||||||
|
* @return 数据库影响行数
|
||||||
*/
|
*/
|
||||||
int delete(Long id);
|
int deleteById(Long id);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 列表
|
* 根据 ids 批量删除
|
||||||
* @param params
|
*
|
||||||
* @return
|
* @param idList 要删除的数据 id
|
||||||
|
* @return 数据库影响行数
|
||||||
|
*/
|
||||||
|
int deleteByIds(List<Long> idList);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 分页查询
|
||||||
|
*
|
||||||
|
* @param params 条件
|
||||||
|
* @return 查询结果
|
||||||
*/
|
*/
|
||||||
PageResult<${className}PO> findAll(Map<String, Object> params);
|
PageResult<${className}PO> findAll(Map<String, Object> params);
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package ${package}.${pkgName}.service.impl;
|
package ${package}.${pkgName}.service.impl;
|
||||||
|
|
||||||
|
import ${package}.${pkgName}.convert.${className}Converter;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.shoulder.core.dto.response.PageResult;
|
import org.shoulder.core.dto.response.PageResult;
|
||||||
@@ -8,59 +10,73 @@ import com.github.pagehelper.PageInfo;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.apache.commons.collections4.MapUtils;
|
import org.apache.commons.collections4.MapUtils;
|
||||||
|
|
||||||
import ${package}.${pkgName}.po.${className};
|
import org.shoulder.core.dto.response.PageResult;
|
||||||
|
import ${package}.${pkgName}.dto.${className}DTO;
|
||||||
import ${package}.${pkgName}.dao.${className}Mapper;
|
import ${package}.${pkgName}.dao.${className}Mapper;
|
||||||
|
import ${package}.${pkgName}.model.${className};
|
||||||
|
import ${package}.${pkgName}.po.${className}PO;
|
||||||
import ${package}.${pkgName}.service.${className}Service;
|
import ${package}.${pkgName}.service.${className}Service;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ${className} 业务层
|
||||||
|
*
|
||||||
|
* @author ${author}
|
||||||
|
* @date ${datetime}
|
||||||
|
*/
|
||||||
@Service
|
@Service
|
||||||
public class ${className}ServiceImpl implements ${className}Service {
|
public class ${className}ServiceImpl implements ${className}Service {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ${className}Mapper ${lowClassName}Mapper;
|
private ${className}Mapper ${lowClassName}Mapper;
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 添加
|
|
||||||
* @param ${lowClassName}
|
|
||||||
*/
|
|
||||||
public int save(${className} ${lowClassName}){
|
public int save(${className} ${lowClassName}){
|
||||||
return ${lowClassName}Mapper.insert(${lowClassName});
|
${className}PO ${lowClassName}PO = ${className}Converter.CONVERTER.toPO(${lowClassName});
|
||||||
|
return ${lowClassName}Mapper.insert(${lowClassName}PO);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 修改
|
|
||||||
* @param ${lowClassName}
|
|
||||||
*/
|
|
||||||
public int update(${className} ${lowClassName}){
|
public int update(${className} ${lowClassName}){
|
||||||
return ${lowClassName}Mapper.update(${lowClassName});
|
${className}PO ${lowClassName}PO = ${className}Converter.CONVERTER.toPO(${lowClassName});
|
||||||
|
return ${lowClassName}Mapper.update(${lowClassName}PO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int deleteById(Long id) {
|
||||||
|
return ${lowClassName}Mapper.deleteById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int deleteByIds(List<Long> idList) {
|
||||||
|
return ${lowClassName}Mapper.deleteByIds(idList);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
@Override
|
||||||
* 删除
|
|
||||||
* @param id 主键
|
|
||||||
*/
|
|
||||||
public int delete(Long id){
|
|
||||||
return ${lowClassName}Mapper.delete(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 列表
|
|
||||||
* @param params
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public PageResult<${className}> findAll(Map<String, Object> params){
|
public PageResult<${className}> findAll(Map<String, Object> params){
|
||||||
//设置分页信息,分别是当前页数和每页显示的总记录数
|
// 设置当前页数和每页显示的最大记录数
|
||||||
if (MapUtils.getInteger(params, "page")!=null && MapUtils.getInteger(params, "limit")!=null) {
|
if (MapUtils.getInteger(params, "page")!=null && MapUtils.getInteger(params, "limit")!=null) {
|
||||||
PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
|
PageHelper.startPage(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"), true);
|
||||||
}
|
}
|
||||||
List<${className}> list = ${lowClassName}Mapper.findAll(params);
|
List<${className}PO> poList = ${lowClassName}Mapper.findAll(params);
|
||||||
PageInfo<${className}> pageInfo = new PageInfo(list);
|
// todo 如果为空则返回空
|
||||||
|
if(CollectionUtils.isEmpty(poList)) {
|
||||||
|
throw new RuntimeException("return empty...");
|
||||||
|
//return PageResult.empty(0);
|
||||||
|
}
|
||||||
|
List<${className}> list = poList.stream()
|
||||||
|
.map(${className}Converter.CONVERTER::fromPO)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
PageInfo<${className}> pageInfo = new PageInfo<>(list);
|
||||||
|
|
||||||
return PageResult.<${className}>builder().data(pageInfo.getList()).code(0).count(pageInfo.getTotal()).build();
|
return PageResult.PageResultBuilder.aPageResult()
|
||||||
|
.list(pageInfo.getList())
|
||||||
|
.totalPageNum((int) pageInfo.getTotal())
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,32 @@
|
|||||||
<groupId>cn.itlym.platform</groupId>
|
<groupId>cn.itlym.platform</groupId>
|
||||||
<artifactId>system-api</artifactId>
|
<artifactId>system-api</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hibernate.validator</groupId>
|
||||||
|
<artifactId>hibernate-validator</artifactId>
|
||||||
|
<version>6.1.6.Final</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mapstruct</groupId>
|
||||||
|
<artifactId>mapstruct</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.baomidou</groupId>
|
||||||
|
<artifactId>mybatis-plus-annotation</artifactId>
|
||||||
|
<version>3.4.0</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.pagehelper</groupId>
|
||||||
|
<artifactId>pagehelper</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mybatis</groupId>
|
||||||
|
<artifactId>mybatis</artifactId>
|
||||||
|
<version>3.5.5</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user