1.idea先安装插件 EasyCode
2.设置模板信息
通过设置找到插件
点击添加模板具体配置看如下
Controller
##导入宏定义 $!{define.vm} $!define $!init ##设置表后缀(宏定义) #setTableSuffix("Controller") ##保存文件(宏定义) #save("/controller", "Controller.java") ##包路径(宏定义) #setPackageSuffix("controller") ##定义服务名 #set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service")) ##定义实体对象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) import $!{tableInfo.savePackageName}.domain.po.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import $!{tableInfo.savePackageName}.domain.dto.$!{tableInfo.name}Dto; import org.springframework.web.bind.annotation.*; import $!{tableInfo.savePackageName}.common.tenum.BusinessEnum; import $!{tableInfo.savePackageName}.common.utils.Response; import javax.annotation.Resource; import java.io.Serializable; import java.util.List; import java.lang.Long; ##表注释(宏定义) #tableComment("表控制层") @RestController @RequestMapping("$!tool.firstLowerCase($!tableInfo.name)") public class $!{tableName} { /** * 服务对象 */ @Resource private $!{tableInfo.name}Service $!{serviceName}; /** * * @param $!{entityName}dto 请求参数封装 * @author $!author * @description //TODO 分页查询所有数据 * @date $!time.currTime() * @return 实例对象 */ @PostMapping("/find$!{tableInfo.name}SelectList") public Response find$!{tableInfo.name}SelectList(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto ) { return Response.ok(BusinessEnum.SUCCESS,$!{serviceName}.find$!{tableInfo.name}SelectList($!{entityName}dto)); } /** * * @param id 主键 * @author $!author * @description //TODO 通过主键查询单条数据 * @date $!time.currTime() * @return 单条数据 */ @GetMapping("/select$!{tableInfo.name}ById/{id}") public Response select$!{tableInfo.name}ById(@PathVariable Long id) { return Response.ok(BusinessEnum.SUCCESS,$!{serviceName}.select$!{tableInfo.name}ById(id)); } /** * * @param $!{entityName}dto 实体对象 * @author $!author * @description //TODO 新增数据 * @date $!time.currTime() * @return 新增结果 */ @PostMapping public Response insert(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto) { $!{serviceName}.insert($!{entityName}dto); return Response.ok(BusinessEnum.SUCCESS); } /** * * @param $!{entityName}dto 实体对象 * @author $!author * @description //TODO 修改数据 * @date $!time.currTime() * @return 修改结果 */ @PutMapping public Response update(@RequestBody $!{tableInfo.name}Dto $!{entityName}dto) { return Response.ok(BusinessEnum.SUCCESS,$!{serviceName}.update($!{entityName}dto)); } /** * * @param idList 主键集合 * @author $!author * @description //TODO 删除数据 * @date $!time.currTime() * @return 删除结果 */ @DeleteMapping public Response delete(@RequestParam("idList") ListidList) { $!{serviceName}.deleteById(idList); return Response.ok(BusinessEnum.SUCCESS); } }
Service
##导入宏定义 $!{define.vm} $!define $!init ##设置表后缀(宏定义) #setTableSuffix("Service") ##保存文件(宏定义) #save("/service", "Service.java") ##包路径(宏定义) #setPackageSuffix("service") import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao; import $!{tableInfo.savePackageName}.domain.dto.$!{tableInfo.name}Dto; import $!{tableInfo.savePackageName}.domain.po.$!{tableInfo.name}; import java.util.List; ##表注释(宏定义) #tableComment("表服务接口") public interface $!{tableName} extends IService<$!tableInfo.name> { /** * * @param id 主键 * @author $!author * @description //TODO 通过ID查询单条数据 * @date $!time.currTime() * @return 实例对象 */ public $!{tableInfo.name} select$!{tableInfo.name}ById (Long id); /** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象 * @author $!author * @description //TODO 分页查询数据 * @date $!time.currTime() * @return 实例对象 */ public IPage find$!{tableInfo.name}SelectList ($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto); /** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象 * @author $!author * @description //TODO 新增数据 * @date $!time.currTime() * @return 实例对象 */ public void insert($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto); /** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象 * @author $!author * @description //TODO 修改数据并返回 * @date $!time.currTime() * @return 实例对象 */ public $!{tableInfo.name} update($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto); /** * * @param idList 主键 * @author $!author * @description //TODO 通过主键删除数据 * @date $!time.currTime() * @return */ public void deleteById(ListidList); }
ServiceImpl
##导入宏定义 $!{define.vm} $!define $!init ##设置表后缀(宏定义) #setTableSuffix("ServiceImpl") ##保存文件(宏定义) #save("/service/impl", "ServiceImpl.java") ##包路径(宏定义) #setPackageSuffix("service.impl") import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Dao; import $!{tableInfo.savePackageName}.domain.dto.$!{tableInfo.name}Dto; import $!{tableInfo.savePackageName}.domain.po.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import $!{tableInfo.savePackageName}.common.utils.copy.ModelUtil; import $!{tableInfo.savePackageName}.common.tenum.ModelIsDelEnum; import org.springframework.stereotype.Service; import java.lang.Long; import java.util.List; import java.time.LocalDateTime; ##表注释(宏定义) #tableComment("表服务实现类") @Service("$!tool.firstLowerCase($tableInfo.name)Service") public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Dao, $!{tableInfo.name}> implements $!{tableInfo.name}Service { /** * * @param id 主键 * @author $!author * @description //TODO 通过ID查询单条数据 * @date $!time.currTime() * @return 实例对象 */ @Override public $!{tableInfo.name} select$!{tableInfo.name}ById (Long id) { LambdaQueryWrapper<$!{tableInfo.name}> $!{tool.firstLowerCase($!{tableInfo.name})}lam = new LambdaQueryWrapper<$!{tableInfo.name}>(); $!{tool.firstLowerCase($!{tableInfo.name})}lam.eq($!{tableInfo.name} ::getId ,id); return baseMapper.selectOne($!{tool.firstLowerCase($!{tableInfo.name})}lam); } /** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象 * @author $!author * @description //TODO 分页查询数据 * @date $!time.currTime() * @return 实例对象 */ @Override public IPage find$!{tableInfo.name}SelectList ($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto) { IPage<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})Page =new Page<>($!{tool.firstLowerCase($!{tableInfo.name})}dto.getPageNum(), $!{tool.firstLowerCase($!{tableInfo.name})}dto.getPageSize()); LambdaQueryWrapper<$!{tableInfo.name}> $!tool.firstLowerCase($!{tableInfo.name})Wrapper = new LambdaQueryWrapper<>(); return baseMapper.selectPage($!tool.firstLowerCase($!{tableInfo.name})Page, $!tool.firstLowerCase($!{tableInfo.name})Wrapper); } /** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象 * @author $!author * @description //TODO 新增数据 * @date $!time.currTime() * @return 实例对象 */ @Override public void insert($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto) { $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}) = ModelUtil.copyModel($!tool.firstLowerCase($!{tableInfo.name})dto, $!{tableInfo.name}.class); $!{tool.firstLowerCase($!{tableInfo.name})}.setIsDelete(ModelIsDelEnum.EFFECTIVE.getCode()); $!{tool.firstLowerCase($!{tableInfo.name})}.setCreatedTime(LocalDateTime.now()); baseMapper.insert($!tool.firstLowerCase($!{tableInfo.name})); } /** * * @param $!tool.firstLowerCase($!{tableInfo.name})dto 实例对象 * @author $!author * @description //TODO 修改数据并返回 * @date $!time.currTime() * @return 实例对象 */ @Override public $!{tableInfo.name} update($!{tableInfo.name}Dto $!tool.firstLowerCase($!{tableInfo.name})dto) { $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name}) = ModelUtil.copyModel($!tool.firstLowerCase($!{tableInfo.name})dto, $!{tableInfo.name}.class); $!{tool.firstLowerCase($!{tableInfo.name})}.setUpdatedTime(LocalDateTime.now()); baseMapper.updateById($!{tool.firstLowerCase($!{tableInfo.name})}); return baseMapper.selectById($!{tool.firstLowerCase($!{tableInfo.name})}.getId()); } /** * * @param idList 主键 * @author $!author * @description //TODO 通过主键删除数据 * @date $!time.currTime() * @return */ @Override public void deleteById(ListidList) { if(idList!=null && idList.size()>0){ for(int i = 0;i < idList.size(); i++ ){ baseMapper.deleteById(idList.get(i)); } } } }
Dao
##导入宏定义 $!{define.vm} $!define $!init ##设置表后缀(宏定义) #setTableSuffix("Dao") ##保存文件(宏定义) #save("/dao", "Dao.java") ##包路径(宏定义) #setPackageSuffix("dao") import com.baomidou.mybatisplus.core.mapper.BaseMapper; import $!{tableInfo.savePackageName}.domain.po.$!tableInfo.name; import org.apache.ibatis.annotations.Mapper; ##表注释(宏定义) #tableComment("表数据库访问层") @Mapper public interface $!{tableName} extends BaseMapper<$!tableInfo.name> { }
Dto
##引入宏定义 $!{define.vm} $!define $!init ##设置表后缀(宏定义) #setTableSuffix("dto") ##保存文件(宏定义) #save("/domain/dto", "Dto.java") ##包路径(宏定义) #setPackageSuffix("domain.dto") ##使用全局变量实现默认包导入 $!autoImport import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import lombok.EqualsAndHashCode; ##使用宏定义实现类注释信息 #tableComment("参数封装类") @Data() @EqualsAndHashCode() public class $!{tableInfo.name}Dto { #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end /** * 页 */ private Integer pageNum; /** * 条 */ private Integer pageSize; /** * 开始时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime startTime; /** * 结束时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime endTime; public Integer getPageNum() { return pageNum == null ? 1 : pageNum; } public Integer getPageSize() { return pageSize == null ? 10 : pageSize; } }
PO
##导入宏定义 $!{define.vm} $!define $!init ##保存文件(宏定义) #save("/domain/po", ".java") ##包路径(宏定义) #setPackageSuffix("domain.po") ##自动导入包(全局变量) $!autoImport import com.baomidou.mybatisplus.extension.activerecord.Model; import java.io.Serializable; import lombok.Data; import lombok.EqualsAndHashCode; ##表注释(宏定义) #tableComment("表实体类") @EqualsAndHashCode(callSuper = true) @SuppressWarnings("serial") @Data() public class $!{tableInfo.name} extends Model<$!{tableInfo.name}> implements Serializable { private static final long serialVersionUID = $!tool.serial(); #foreach($column in $tableInfo.fullColumn) #if(${column.comment})/** * ${column.comment} */#end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end #foreach($column in $tableInfo.pkColumn) /** * 获取主键值 * * @return 主键值 */ @Override protected Serializable pkVal() { return this.$!column.name; } #break #end }
mapper.xml
##引入mybatis支持 $!mybatisSupport ##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Dao.xml")) $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end#foreach($column in $tableInfo.fullColumn) #end
模板配置成功idea连接数据库
输入数据库的账号密码
第一次用可能要去下载对应连接的jar包
如果出现市区问题,去Advanced修改这个字段为GMT
点击测试连接,连接成功
选择对应数据表
选择包路径
我这里是全选了,有需要可以自己来选择
生成成功
ModelUtil
import org.springframework.beans.BeanUtils; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; /** * * @author 洛无异 * @description //TODO model copy 工具类 * @date 2021/4/19 11:13 */ public class ModelUtil { public staticV copyModel(K k, Class vClass){ try { return copyModel(k, vClass.newInstance()); }catch (Exception e){ throw new BusinessException(BusinessEnum.SYSTEM_HANDLE_ERROR); } } public static V copyModel(K k, Class vClass, ModelUtilsProcessor processor){ try { return copyModel(k, vClass.newInstance(), processor); }catch (Exception e){ throw new BusinessException(BusinessEnum.SYSTEM_HANDLE_ERROR); } } public static V copyModel(K k, Class vClass, ModelUtilsFilter filter) { try { return copyModel(k, vClass.newInstance(), filter); }catch (Exception e){ throw new BusinessException(BusinessEnum.SYSTEM_HANDLE_ERROR); } } /** * * 复制kList的每一项到list中 * @param kList source * @param classV 生成的list中每一项的类型 * @author 洛无异 * @description //TODO 创建包含kList.size()个ClassV实例的list,并调用org.springframework.beans.BeanUtils, * @date 2021/4/19 11:14 * @return java.util.List */ public static List copyModelList(Iterable kList, Class classV) { List vList = new ArrayList<>(); try { for (K k : kList) vList.add(copyModel(k, classV.newInstance())); }catch (Exception e){ throw new BusinessException(BusinessEnum.SYSTEM_HANDLE_ERROR); } return vList; } /** * * @param kList source * @param classV 生成的list中每一项的类型 * @param processor 每次复制时对BeanUtils.copyProperties(source, target)中的source,target调用方法processor.process(source, target) * @author 洛无异 * @description //TODO 创建包含kList.size()个ClassV实例的vlist,并调用org.springframework.beans.BeanUtils.copyProperties(), 复制kList的每一项到vlist中,之后再对vlist的每一项执行processor.process(k, v) * @date 2021/4/19 11:15 * @return java.util.List */ public static List copyModelList(Iterable kList, Class classV, ModelUtilsProcessor processor) throws Exception { List vList = new ArrayList<>(); try { for (K k : kList) if (processor.filter(k)) vList.add(copyModel(k, classV.newInstance(), processor)); }catch (Exception e){ throw new BusinessException(BusinessEnum.SYSTEM_HANDLE_ERROR); } return vList; } /** * * @param kList * @param classV * @param filter * @author 洛无异 * @description //TODO 同上,但是接口换成了默认实现process()的ModelUtilsFilter,便于使用lambda表达式 * @date 2021/4/19 11:15 * @return java.util.List */ public static List copyModelList(Iterable kList, Class classV, ModelUtilsFilter filter) throws Exception { List vList = new ArrayList<>(); try { for (K k : kList) if (filter.filter(k)) vList.add(copyModel(k, classV.newInstance(), filter)); }catch (Exception e){ throw new BusinessException(BusinessEnum.SYSTEM_HANDLE_ERROR); } return vList; } /** * * @param kList modelList * @param classK modelList的类型 * @author 洛无异 * @description //TODO 获取kList中的所有id * @date 2021/4/19 11:16 * @return java.util.List * @throws Exception 如果classV.getId()返回值类型不是string类型的,会抛出异常 */ public static List getModelIdList(Iterable kList, Class classK) throws Exception { List idList = new ArrayList<>(); for (K k : kList) { Method getId = classK.getMethod("getId"); Object idobj = getId.invoke(k); if (!(idobj instanceof String)) throw new Exception("The return value of " + classK.toString() + ".getId() is not a String type."); String id = (String) idobj; idList.add(id); } return idList; } private static V copyModel(K k, V v) { BeanUtils.copyProperties(k, v); return v; } private static V copyModel(K k, V v, ModelUtilsProcessor processor) throws Exception { BeanUtils.copyProperties(k, v); processor.process(k, v); return v; } private static V copyModel(K k, V v, ModelUtilsFilter filter) throws Exception { BeanUtils.copyProperties(k, v); filter.process(k, v); return v; } }
ModelUtilsFilter
/** * * @author 洛无异 * @description //TODO * @date 2021/4/19 11:19 */ public interface ModelUtilsFilter{ default void process(K k, V v) throws Exception { } boolean filter(K k) throws Exception; }
ModelUtilsProcessor
public interface ModelUtilsProcessor{ void process(K k, V v) throws Exception; default boolean filter(K k) { return true; } }
BusinessException 异常封装类
import lombok.Data; /** * * @author 洛无异 * @description //TODO 自定义异常处理类 * @date 2021/4/19 11:19 */ @Data public class BusinessException extends RuntimeException{ private static final long serialVersionUID = 8204335493386165578L; /** * 状态码 */ private Integer code; /** * 信息 */ private String msg; /** * 传入BusinessEnum类 * @param businessEnum */ public BusinessException(BusinessEnum businessEnum){ this.code = businessEnum.getCode(); this.msg = businessEnum.getMsg(); } public BusinessException(int code ,String msg){ this.code = code; this.msg = msg; } public BusinessException(String msg){ super(msg); this.msg = msg; }
BusinessEnum
import lombok.Getter; import lombok.Setter; /** * @author 洛无异 * @description //TODO 自定义异常处理类 * @date 2021/4/19 11:18 */ public enum BusinessEnum { SUCCESS(0, "成功"), PARAMETER_ERROR(1, "参数错误"), ERROR(2, "处理失败"); @Setter @Getter private Integer code; @Setter @Getter private String msg; BusinessEnum(Integer code, String msg) { this.code = code; this.msg = msg; } }
Response
import com.yzchn.admin.common.enums.ResCodeEnum; import com.yzchn.admin.util.BusinessEnum; import lombok.Data; import java.io.Serializable; /** * @description: 接口返回对象 * @author: 洛无异 * @create: 2020-04-23 14:32 **/ @Data public class Responseimplements Serializable { private static final long serialVersionUID = 1L; private String code; private String message; private T data; private boolean success; private Response() { } public Response(String code, String message, T data) { this.code = code; this.message = message; this.data = data; } public static Response getInstance(ResCodeEnum res) { Response instance = new Response(); instance.setCode(res.getCode()); instance.setMessage(res.getResMsg()); return instance; } public static Response getInstance(ResCodeEnum res, String message) { Response instance = new Response(); instance.setCode(res.getCode()); instance.setMessage(message); return instance; } public static Response ok(BusinessEnum res, T data) { Response instance = new Response(); instance.setCode(res.getCode().toString()); instance.setMessage(res.getMsg()); instance.setSuccess(true); instance.setData(data); return instance; } public static Response ok(BusinessEnum res) { Response instance = new Response(); instance.setCode(res.getCode().toString()); instance.setMessage(res.getMsg()); instance.setSuccess(true); return instance; } public static Response error(BusinessEnum res, T data) { Response instance = new Response(); instance.setCode(res.getCode().toString()); instance.setMessage(res.getMsg()); instance.setSuccess(false); return instance; } public static Response error(BusinessEnum res) { Response instance = new Response(); instance.setCode(res.getCode().toString()); instance.setMessage(res.getMsg()); instance.setSuccess(false); return instance; } public static Response error(BusinessEnum res, String message) { Response instance = new Response(); instance.setCode(res.getCode().toString()); instance.setMessage(message); instance.setSuccess(false); return instance; } }
记录一下,反正也没人看