EasyCode-MybatisCodeHelper代码生成器
安装
方法一:打开idea,一次到开File–Settings–Plugins搜索插件“EasyCode-MybatisCodeHelper"。
在idea中配置数据源
分别如图所示展开需要生成代码的数据表,右击表图标
配置对应的路径
也可以使用自己配置的模板
这样就可以去配置自己常用的写法和接口啦! 以下是作者用的模板
entity
##引入宏定义 $!define ##使用宏定义设置回调(保存位置与文件后缀) #save("/entity", ".java") ##使用宏定义设置包后缀 #setPackageSuffix("entity") ##使用全局变量实现默认包导入 $!autoImport import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import io.swagger.annotations.ApiModel; import lombok.experimental.Accessors; import java.io.Serializable; import lombok.Data; import io.swagger.annotations.ApiModelProperty; ##使用宏定义实现类注释信息 #tableComment("实体类") @Data @Accessors(chain = true) @ApiModel(value = "$!{tableInfo.comment}") public class $!{tableInfo.name} implements Serializable { private static final long serialVersionUID = 1L; #if(!$tableInfo.pkColumn.isEmpty())@TableId(type = IdType.AUTO) #end #foreach($column in $tableInfo.fullColumn) #if(${column.comment})@ApiModelProperty("$column.comment") #end private $!{tool.getClsNameByFullName($column.type)} $!{column.name}; #end }
dao
##导入宏定义 $!define ##设置表后缀(宏定义) #setTableSuffix("Mapper") ##保存文件(宏定义) #save("/dao", "Mapper.java") ##包路径(宏定义) #setPackageSuffix("dao") import com.baomidou.mybatisplus.core.mapper.BaseMapper; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; import org.apache.ibatis.annotations.Mapper; ##表注释(宏定义) #tableComment("表数据库访问层") @Mapper public interface $!{tableName} extends BaseMapper<$!tableInfo.name> { }
service
##定义初始变量 #set($tableName = $tool.append($tableInfo.name, "Service")) ##设置回调 $!callback.setFileName($tool.append($tableName, ".java")) $!callback.setSavePath($tool.append($tableInfo.savePath, "/service")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end ##if($tableInfo.savePackageName)package $!{tableInfo.savePackageName}.#{end}service; import com.baomidou.mybatisplus.extension.service.IService; import com.github.pagehelper.PageInfo; import com.lb.farm.common.response.Page; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import java.util.List; /** * $!{tableInfo.comment}($!{tableInfo.name})表服务接口 * * @author $!author * @since $!time.currTime() */ public interface $!{tableName} extends IService<$!{tableInfo.name}>{ /** * 分页查询 * * @param page 查询参数 * @return 对象列表 */ PageInfo<$!{tableInfo.name}> query(Page page); /** * 选择框查询 * @return */ List<$!{tableInfo.name}> queryAll(); /** * 保存数据 * * @param $!tool.firstLowerCase($!{tableInfo.name}) 实例对象 * @return 实例对象 */ $!{tableInfo.name} saveEntity($!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})); /** * 通过主键删除数据 * * @param ids 主键 * @return 是否成功 */ Boolean deleteBatchId(String ids); /** * 通过ID查询单条数据 * * @param $!pk.name 主键 * @return 实例对象 */ $!{tableInfo.name} queryById($!pk.shortType $!pk.name); }
serviceImpl
###导入宏定义 $!define ##设置表后缀(宏定义) #setTableSuffix("ServiceImpl") ##保存文件(宏定义) #save("/service/impl", "ServiceImpl.java") ##包路径(宏定义) #setPackageSuffix("service.impl") ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.lb.farm.common.response.Page; import $!{tableInfo.savePackageName}.dao.$!{tableInfo.name}Mapper; import $!{tableInfo.savePackageName}.entity.$!{tableInfo.name}; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.Arrays; import java.util.List; /** * $!{tableInfo.comment}($!{tableInfo.name})表服务实现类 * * @author $!author * @since $!time.currTime() */ @Slf4j @Service public class $!{tableName} extends ServiceImpl<$!{tableInfo.name}Mapper, $!{tableInfo.name}> implements $!{tableInfo.name}Service { @Override public PageInfo<$!{tableInfo.name}> query(Page page) { PageHelper.startPage(page.getPage(), page.getLimit()); List<$!{tableInfo.name}> list = getBaseMapper().selectList(null); return new PageInfo<>(list); } @Override public List<$!{tableInfo.name}> queryAll() { return getBaseMapper().selectList(null); } @Override public $!{tableInfo.name} saveEntity($!{tableInfo.name} entity) { if (entity.getId() == null) { getBaseMapper().insert(entity); } else { getBaseMapper().updateById(entity); } return entity; } @Override public Boolean deleteBatchId(String ids) { return getBaseMapper().deleteBatchIds(Arrays.asList(ids.split(","))) > 0 ? true : false; } @Override public $!{tableInfo.name} queryById($!pk.shortType $!pk.name) { return getBaseMapper().selectById($!pk.name); } }
controller
##导入宏定义 $!define ##设置表后缀(宏定义) #setTableSuffix("Controller") ##保存文件(宏定义) #save("/controller", "Controller.java") ##包路径(宏定义) #setPackageSuffix("controller") ##定义服务名 #set($serviceName = $!tool.append($!tool.firstLowerCase($!tableInfo.name), "Service")) ##定义实体对象名 #set($entityName = $!tool.firstLowerCase($!tableInfo.name)) import com.lb.farm.common.response.JsonResult; import com.lb.farm.common.response.Page; import $!{tableInfo.savePackageName}.entity.$!tableInfo.name; import $!{tableInfo.savePackageName}.service.$!{tableInfo.name}Service; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; ##表注释(宏定义) #tableComment("表控制层") @Api(description = "$tableInfo.comment相关接口") @RestController @RequestMapping("/trace/$!tool.firstLowerCase($!tableInfo.name)") public class $!{tableName} { @Resource private $!{tableInfo.name}Service $!{serviceName}; @ApiOperation(value = "分页查询", response = $!{tableInfo.name}.class) @GetMapping("/query") public JsonResult query(Page page) { return new JsonResult($!{serviceName}.query(page)); } @ApiOperation("选择框查询") @GetMapping("/queryAll") public JsonResult queryAll() { return new JsonResult($!{serviceName}.queryAll()); } @ApiOperation("保存数据") @PostMapping("/saveEntity") public JsonResult saveEntity(@RequestBody $!{tableInfo.name} $!tool.firstLowerCase($!{tableInfo.name})) { return new JsonResult($!{serviceName}.saveEntity($!tool.firstLowerCase($!{tableInfo.name}))); } @ApiOperation("删除数据") @DeleteMapping("/{ids}") public JsonResult deleteBatchId(@ApiParam(value = "主键ids 1,2,3...") @PathVariable String ids) { return new JsonResult($!{serviceName}.deleteBatchId(ids)); } @ApiOperation(value = "主键查询", response = $!{tableInfo.name}.class) @GetMapping("/{id}") public JsonResult getById(@ApiParam(value = "主键id") @PathVariable Integer id) { return new JsonResult($!{serviceName}.queryById(id)); } }
xml
##引入mybatis支持 $!mybatisSupport ##设置保存名称与保存位置 $!callback.setFileName($tool.append($!{tableInfo.name}, "Mapper.xml")) $!callback.setSavePath($tool.append($modulePath, "/src/main/resources/mapper")) ##拿到主键 #if(!$tableInfo.pkColumn.isEmpty()) #set($pk = $tableInfo.pkColumn.get(0)) #end <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="$!{tableInfo.savePackageName}.mapper.$!{tableInfo.name}Mapper"> <resultMap type="$!{tableInfo.savePackageName}.entity.$!{tableInfo.name}" id="$!{tableInfo.name}BaseResultMap"> #foreach($column in $tableInfo.fullColumn) <result property="$!column.name" column="$!column.obj.name"/> #end </resultMap> </mapper>