代码生成-模板处理

This commit is contained in:
TinyAnts 2022-06-15 17:14:20 +08:00
parent cd273a75d1
commit 5ec1e82d8b
6 changed files with 113 additions and 216 deletions

View File

@ -40,18 +40,14 @@ public class VelocityUtil {
velocityContext.put("genTpl", table.getGenTpl());
velocityContext.put("tableName", table.getTableName());
velocityContext.put("authorName", table.getAuthorName());
velocityContext.put("entityName", table.getEntityName());
velocityContext.put("EntityName", table.getEntityName());
velocityContext.put("entityName", StringUtil.uncapitalize(table.getEntityName()));
velocityContext.put("moduleName", table.getModuleName());
velocityContext.put("packageName", table.getPackageName());
velocityContext.put("businessName", StringUtil.capitalize(table.getBusinessName()));
velocityContext.put("functionName", StringUtil.isNotEmpty(table.getFunctionName()) ? table.getFunctionName() : "【请填写功能名称】");
velocityContext.put("table", table);
velocityContext.put("columns", columns);
System.out.println(table.getTableName());
System.out.println(table.getAuthorName());
System.out.println(table.getEntityName());
System.out.println(table.getModuleName());
System.out.println(table.getPackageName());
return velocityContext;
}
@ -64,10 +60,10 @@ public class VelocityUtil {
public static List<String> getTemplateList(String genTpl) {
List<String> templates = new LinkedList<>();
// templates.add("java/controller.java.vm");
templates.add("java/entity.java.vm");
// templates.add("java/entity.java.vm");
// templates.add("java/mapper.java.vm");
// templates.add("java/service.java.vm");
// templates.add("java/serviceImpl.java.vm");
templates.add("java/serviceImpl.java.vm");
// templates.add("java/validate.java.vm");
return templates;
}

View File

@ -2,11 +2,10 @@ package ${packageName}.controller;
import com.hxkj.admin.LikeAdminThreadLocal;
import com.hxkj.admin.config.aop.Log;
import com.hxkj.admin.service.ISystemAdminService;
import com.hxkj.admin.service.I${EntityName}Service;
import com.hxkj.admin.validate.PageParam;
import com.hxkj.admin.validate.system.SystemAdminParam;
import com.hxkj.admin.vo.system.SystemAdminVo;
import com.hxkj.admin.vo.system.SystemSelfVo;
import com.hxkj.admin.validate.system.${EntityName}Param;
import com.hxkj.admin.vo.system.${EntityName}Vo;
import com.hxkj.common.core.AjaxResult;
import com.hxkj.common.core.PageResult;
import com.hxkj.common.validator.annotation.IDMust;
@ -24,7 +23,7 @@ import java.util.Map;
public class ${ClassName}Controller {
@Resource
ISystemAdminService iSystemAdminService;
I${EntityName}Service i${EntityName}AdminService;
/**
* ${functionName}列表
@ -35,7 +34,7 @@ public class ${ClassName}Controller {
@GetMapping("/list")
public Object list(@Validated PageParam pageParam,
@RequestParam Map<String, String> params) {
PageResult<SystemAdminVo> list = iSystemAdminService.list(pageParam, params);
PageResult<${EntityName}Vo> list = i${EntityName}Service.list(pageParam, params);
return AjaxResult.success(list);
}
@ -48,7 +47,7 @@ public class ${ClassName}Controller {
*/
@GetMapping("/detail")
public Object detail(@Validated @IDMust() @RequestParam("id") Integer id) {
SystemAdminVo vo = iSystemAdminService.detail(id);
${EntityName}Vo vo = i${EntityName}Service.detail(id);
return AjaxResult.success(vo);
}
@ -56,13 +55,13 @@ public class ${ClassName}Controller {
* ${functionName}新增
*
* @author ${authorName}
* @param systemAdminParam 参数
* @param ${entityName}Param 参数
* @return Object
*/
@Log(title = "${functionName}新增")
@PostMapping("/add")
public Object add(@Validated(value = SystemAdminParam.create.class) @RequestBody SystemAdminParam systemAdminParam) {
iSystemAdminService.add(systemAdminParam);
public Object add(@Validated(value = ${EntityName}Param.create.class) @RequestBody ${EntityName}Param ${entityName}Param) {
i${EntityName}Service.add(systemAdminParam);
return AjaxResult.success();
}
@ -70,13 +69,13 @@ public class ${ClassName}Controller {
* ${functionName}编辑
*
* @author ${authorName}
* @param systemAdminParam 参数
* @param ${entityName}Param 参数
* @return Object
*/
@Log(title = "${functionName}编辑")
@PostMapping("/edit")
public Object edit(@Validated(value = SystemAdminParam.update.class) @RequestBody SystemAdminParam systemAdminParam) {
iSystemAdminService.edit(systemAdminParam);
public Object edit(@Validated(value = ${EntityName}Param.update.class}) @RequestBody ${EntityName}Param} ${entityName}Param}) {
i${EntityName}Service.edit(${entityName}Param});
return AjaxResult.success();
}
@ -88,8 +87,8 @@ public class ${ClassName}Controller {
*/
@Log(title = "${functionName}删除")
@PostMapping("/del")
public Object del(@Validated(value = SystemAdminParam.delete.class) @RequestBody SystemAdminParam systemAdminParam) {
iSystemAdminService.del(systemAdminParam.getId());
public Object del(@Validated(value = ${EntityName}Param.delete.class) @RequestBody ${EntityName}Param ${entityName}Param) {
i${EntityName}Service.del(${entityName}Param.getId());
return AjaxResult.success();
}

View File

@ -5,7 +5,7 @@ import com.hxkj.common.entity.${entityName};
import org.apache.ibatis.annotations.Mapper;
/**
* 系统管理员
* ${functionName}
*/
@Mapper
public interface ${entityName}Mapper extends IBaseMapper<${entityName}> {

View File

@ -1,97 +1,58 @@
package com.hxkj.admin.service;
import com.hxkj.admin.validate.PageParam;
import com.hxkj.admin.validate.system.SystemAdminParam;
import com.hxkj.admin.vo.system.SystemAdminVo;
import com.hxkj.admin.vo.system.SystemSelfVo;
import com.hxkj.admin.validate.${EntityName}Param;
import com.hxkj.admin.vo.system.${EntityName}Vo;
import com.hxkj.common.core.PageResult;
import com.hxkj.common.entity.system.SystemAdmin;
import com.hxkj.common.entity.${EntityName};
import java.util.Map;
/**
* 系统管理员服务接口类
* ${functionName}服务接口类
*/
public interface ISystemAdminService {
/**
* 根据账号查找管理员
* ${functionName}列表
*
* @author fzr
* @param username 主键ID
* @return SysAdmin
*/
SystemAdmin findByUsername(String username);
/**
* 管理员列表
*
* @author fzr
* @author ${authorName}
* @param pageParam 分页参数
* @return PageResult<SysAdminListVo>
* @return PageResult<${EntityName}Vo}>
*/
PageResult<SystemAdminVo> list(PageParam pageParam, Map<String, String> params);
PageResult<${EntityName}Vo> list(PageParam pageParam, Map<String, String> params);
/**
* 当前管理员
* ${functionName}详情
*
* @author fzr
* @return SystemSelfVo
*/
SystemSelfVo self(Integer adminId);
/**
* 管理员详情
*
* @author fzr
* @author ${authorName}
* @param id 主键参数
* @return SysAdmin
* @return ${EntityName}Vo
*/
SystemAdminVo detail(Integer id);
/**
* 管理员新增
* ${functionName}新增
*
* @author fzr
* @param systemAdminParam 参数
* @author ${authorName}
* @param ${entityName}Param 参数
*/
void add(SystemAdminParam systemAdminParam);
void add(${EntityName}Param ${entityName}Param);
/**
* 管理员编辑
* ${functionName}编辑
*
* @author fzr
* @param systemAdminParam 参数
* @author ${authorName}
* @param ${entityName}Param 参数
*/
void edit(SystemAdminParam systemAdminParam);
void edit(${EntityName}Param ${entityName}Param);
/**
* 当前管理员更新
* ${functionName}删除
*
* @author fzr
* @param systemAdminParam 参数
*/
void upInfo(SystemAdminParam systemAdminParam, Integer adminId);
/**
* 管理员删除
*
* @author fzr
* @author ${authorName}
* @param id 主键参数
*/
void del(Integer id);
/**
* 管理员状态切换
*
* @author fzr
* @param id 主键参数
*/
void disable(Integer id);
/**
* 缓存管理员
*/
void cacheAdminUserByUid(Integer id);
}

View File

@ -27,65 +27,41 @@ import javax.annotation.Resource;
import java.util.*;
/**
* 系统管理员实现类
* ${functionName}实现类
*/
@Service
public class SystemAdminServiceImpl implements ISystemAdminService {
public class ${EntityName}ServiceImpl implements I${EntityName}Service {
@Resource
SystemAdminMapper systemAdminMapper;
@Resource
SystemMenuMapper systemMenuMapper;
@Resource
ISystemRoleService iSystemRoleService;
@Resource
ISystemRoleMenuService iSystemRoleMenuService;
${EntityName}Mapper ${entityName}Mapper;
/**
* 管理员列表
* ${functionName}列表
*
* @author fzr
* @author ${authorName}
* @param pageParam 分页参数
* @return PageResult<SysAdminListVo>
*/
@Override
public PageResult<SystemAdminVo> list(PageParam pageParam, Map<String, String> params) {
public PageResult<${EntityName}Vo> list(PageParam pageParam, Map<String, String> params) {
Integer page = pageParam.getPageNo();
Integer limit = pageParam.getPageSize();
MPJQueryWrapper<SystemAdmin> mpjQueryWrapper = new MPJQueryWrapper<>();
mpjQueryWrapper.select("t.id,t.dept_id,t.post_id,t.username,t.nickname,t.avatar," +
"sd.name as dept,sr.name as role,t.is_multipoint,t.is_disable," +
"t.last_login_ip,t.last_login_time,t.create_time,t.update_time")
.eq("t.is_delete", 0)
.leftJoin("ls_system_role sr ON sr.id=t.role")
.leftJoin("ls_system_dept sd ON sd.id=t.dept_id")
.orderByDesc(Arrays.asList("t.id", "t.sort"));
QueryWrapper<SystemAdmin> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("is_delete", 0);
queryWrapper.orderByDesc(Arrays.asList("id", "sort"));
systemAdminMapper.setSearch(mpjQueryWrapper, params, new String[]{
#if($column.isPk)
systemAdminMapper.setSearch(queryWrapper, params, new String[]{
"like:username:str",
"like:nickname:str",
"=:role:int"
});
#end
IPage<SystemAdminVo> iPage = systemAdminMapper.selectJoinPage(
new Page<>(page, limit),
SystemAdminVo.class,
mpjQueryWrapper);
IPage<SystemAdminVo> iPage = systemAdminMapper.selectPage(new Page<>(page, limit), queryWrapper);
for (SystemAdminVo vo : iPage.getRecords()) {
if (vo.getId() == 1) {
vo.setRole("系统管理员");
}
if (vo.getDept() == null) {
vo.setDept("");
}
vo.setAvatar(UrlUtil.toAbsoluteUrl(vo.getAvatar()));
vo.setCreateTime(TimeUtil.timestampToDate(vo.getCreateTime()));
vo.setUpdateTime(TimeUtil.timestampToDate(vo.getUpdateTime()));
vo.setLastLoginTime(TimeUtil.timestampToDate(vo.getLastLoginTime()));
@ -95,20 +71,20 @@ public class SystemAdminServiceImpl implements ISystemAdminService {
}
/**
* 管理员详细
* ${functionName}详细
*
* @author fzr
* @author ${authorName}
* @param id 主键
* @return SysAdmin
*/
@Override
public SystemAdminVo detail(Integer id) {
public ${EntityName}Vo detail(Integer id) {
SystemAdmin sysAdmin = systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(SystemAdmin.class, info->
!info.getColumn().equals("salt") &&
!info.getColumn().equals("password") &&
!info.getColumn().equals("is_delete") &&
!info.getColumn().equals("delete_time"))
!info.getColumn().equals("salt") &&
!info.getColumn().equals("password") &&
!info.getColumn().equals("is_delete") &&
!info.getColumn().equals("delete_time"))
.eq("id", id)
.eq("is_delete", 0)
.last("limit 1"));
@ -129,82 +105,28 @@ public class SystemAdminServiceImpl implements ISystemAdminService {
}
/**
* 管理员新增
* ${functionName}新增
*
* @author fzr
* @param systemAdminParam 参数
* @author ${authorName}
* @param ${entityName}Param 参数
*/
@Override
public void add(SystemAdminParam systemAdminParam) {
String[] field = {"id", "username", "nickname"};
Assert.isNull(systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(field)
.eq("is_delete", 0)
.eq("username", systemAdminParam.getUsername())
.last("limit 1")), "账号已存在换一个吧!");
Assert.isNull(systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(field)
.eq("is_delete", 0)
.eq("nickname", systemAdminParam.getNickname())
.last("limit 1")), "昵称已存在换一个吧!");
SystemRoleVo roleVo = iSystemRoleService.detail(systemAdminParam.getRole());
Assert.notNull(roleVo, "角色不存在!");
Assert.isTrue(roleVo.getIsDisable() <= 0, "当前角色已被禁用!");
String salt = ToolsUtil.randomString(5);
String pwd = ToolsUtil.makeMd5(systemAdminParam.getPassword().trim() + salt);
String avatar = UrlUtil.toRelativeUrl(systemAdminParam.getAvatar());
public void add(${EntityName}Param ${entityName}Param) {
SystemAdmin model = new SystemAdmin();
model.setDeptId(systemAdminParam.getDeptId());
model.setPostId(systemAdminParam.getPostId());
model.setUsername(systemAdminParam.getUsername());
model.setNickname(systemAdminParam.getNickname());
model.setRole(systemAdminParam.getRole());
model.setAvatar(avatar);
model.setPassword(pwd);
model.setSalt(salt);
model.setSort(systemAdminParam.getSort());
model.setIsMultipoint(systemAdminParam.getIsMultipoint());
model.setIsDisable(systemAdminParam.getIsDisable());
model.setCreateTime(System.currentTimeMillis() / 1000);
model.setUpdateTime(System.currentTimeMillis() / 1000);
systemAdminMapper.insert(model);
#foreach ($column in $columns)
model.set${column.javaField}(${entityName}Param.get${column.javaField.toUpperCase()}());
#end
${entityName}Mapper.insert(model);
}
/**
* 管理员更新
* ${functionName}更新
*
* @author fzr
* @param systemAdminParam 参数
* @author ${authorName}
* @param ${entityName}Param 参数
*/
@Override
public void edit(SystemAdminParam systemAdminParam) {
String[] field = {"id", "username", "nickname"};
Assert.notNull(systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(field)
.eq("id", systemAdminParam.getId())
.eq("is_delete", 0)
.last("limit 1")), "账号不存在了!");
Assert.isNull(systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(field)
.eq("is_delete", 0)
.eq("username", systemAdminParam.getUsername())
.ne("id", systemAdminParam.getId())
.last("limit 1")), "账号已存在换一个吧!");
Assert.isNull(systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(field)
.eq("is_delete", 0)
.eq("nickname", systemAdminParam.getNickname())
.ne("id", systemAdminParam.getId())
.last("limit 1")), "昵称已存在换一个吧!");
Assert.notNull(iSystemRoleService.detail(systemAdminParam.getRole()), "角色不存在!");
public void edit(${EntityName}Param ${entityName}Param) {
SystemAdmin model = new SystemAdmin();
model.setId(systemAdminParam.getId());
model.setDeptId(systemAdminParam.getDeptId());
@ -218,43 +140,23 @@ public class SystemAdminServiceImpl implements ISystemAdminService {
model.setIsDisable(systemAdminParam.getIsDisable());
model.setUpdateTime(System.currentTimeMillis() / 1000);
if (systemAdminParam.getPassword() != null) {
String salt = ToolsUtil.randomString(5);
String pwd = ToolsUtil.makeMd5( systemAdminParam.getPassword().trim() + salt);
model.setPassword(pwd);
model.setSalt(salt);
}
systemAdminMapper.updateById(model);
this.cacheAdminUserByUid(systemAdminParam.getId());
}
/**
* 管理员删除
* ${functionName}删除
*
* @author fzr
* @author ${authorName}
* @param id 主键
*/
@Override
public void del(Integer id) {
String[] field = {"id", "username", "nickname"};
Assert.notNull(systemAdminMapper.selectOne(new QueryWrapper<SystemAdmin>()
.select(field)
.eq("id", id)
.eq("is_delete", 0)
.last("limit 1")), "账号已不存在!");
Assert.isFalse(id == 1, "系统管理员不允许删除");
int adminId = Integer.parseInt(LikeAdminThreadLocal.getAdminId().toString());
Assert.isFalse(id == adminId, "不能删除自己");
SystemAdmin model = new SystemAdmin();
model.setId(id);
model.setIsDelete(1);
model.setDeleteTime(System.currentTimeMillis() / 1000);
systemAdminMapper.updateById(model);
this.cacheAdminUserByUid(id);
}
}

View File

@ -0,0 +1,39 @@
package com.hxkj.admin.validate.system;
import com.hxkj.common.validator.annotation.IDMust;
import com.hxkj.common.validator.annotation.IntegerContains;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.hibernate.validator.constraints.Length;
import lombok.Data;
import javax.validation.constraints.*;
import java.io.Serializable;
/**
* ${functionName}参数
*/
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class ${EntityName}Param implements Serializable {
private static final long serialVersionUID = 1L;
public interface create{}
public interface update{}
public interface delete{}
#foreach ($column in $columns)
#if($column.isInsert || $column.isEdit)
#if($column.isPk)
@IDMust(message = "${column.javaField}参数必传且需大于0", groups = {update.class, delete.class})
#end
#if(!$column.isPk && $column.isRequired)
@NotNull(message = "${column.javaField}参数缺失", groups = {create.class, update.class})
#end
private ${column.javaType} ${column.javaField};
#end
#end
}