├── src ├── main │ ├── resources │ │ ├── application.yml │ │ ├── application-dev.yml │ │ ├── templates │ │ │ └── mp │ │ │ │ ├── mapper.java.ftl │ │ │ │ ├── service.java.ftl │ │ │ │ ├── controller.java.ftl │ │ │ │ └── serviceImpl.java.ftl │ │ └── log4j2-dev.xml │ └── java │ │ └── cn │ │ └── hellochaos │ │ └── generator │ │ ├── SpringbootMybatisPlusGeneratorApplication.java │ │ ├── exception │ │ ├── bizException │ │ │ ├── BizExceptionCode.java │ │ │ ├── BizException.java │ │ │ └── BizExceptionCodeEnum.java │ │ └── NoSuchAddressException.java │ │ ├── config │ │ ├── MybatisPlusConfig.java │ │ └── LocalDateTimeSerializerConfig.java │ │ ├── entity │ │ └── dto │ │ │ └── ResultBean.java │ │ └── CodeGenerator.java └── test │ └── java │ └── cn │ └── hellochaos │ └── generator │ └── SpringbootMybatisPlusGeneratorApplicationTests.java ├── .gitignore ├── pom.xml └── README.md /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | profiles: 3 | active: dev 4 | mvc: 5 | throw-exception-if-no-handler-found: true 6 | # 单个文件的最大值 7 | servlet: 8 | multipart: 9 | max-file-size: 50MB 10 | 11 | -------------------------------------------------------------------------------- /src/test/java/cn/hellochaos/generator/SpringbootMybatisPlusGeneratorApplicationTests.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.boot.test.context.SpringBootTest; 5 | 6 | @SpringBootTest 7 | class SpringbootMybatisPlusGeneratorApplicationTests { 8 | 9 | @Test 10 | void contextLoads() { 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/** 5 | !**/src/test/** 6 | 7 | *.properties 8 | ### STS ### 9 | .apt_generated 10 | .classpath 11 | .factorypath 12 | .project 13 | .settings 14 | .springBeans 15 | .sts4-cache 16 | 17 | ### IntelliJ IDEA ### 18 | .idea 19 | *.iws 20 | *.iml 21 | *.ipr 22 | 23 | ### NetBeans ### 24 | /nbproject/private/ 25 | /nbbuild/ 26 | /dist/ 27 | /nbdist/ 28 | /.nb-gradle/ 29 | build/ 30 | 31 | ### VS Code ### 32 | .vscode/ 33 | -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/SpringbootMybatisPlusGeneratorApplication.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator; 2 | 3 | import org.springframework.boot.SpringApplication; 4 | import org.springframework.boot.autoconfigure.SpringBootApplication; 5 | 6 | @SpringBootApplication 7 | public class SpringbootMybatisPlusGeneratorApplication { 8 | 9 | public static void main(String[] args) { 10 | SpringApplication.run(SpringbootMybatisPlusGeneratorApplication.class, args); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/application-dev.yml: -------------------------------------------------------------------------------- 1 | 2 | 3 | logging: 4 | config: classpath:log4j2-dev.xml 5 | spring: 6 | jackson: 7 | date-format: yyyy-MM-dd HH:mm:ss 8 | time-zone: GMT+8 9 | 10 | datasource: 11 | driver-class-name: com.mysql.jdbc.Driver 12 | username: island 13 | password: root 14 | type: com.alibaba.druid.pool.DruidDataSource 15 | url: jdbc:mysql://47.102.210.203/island?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT 16 | server: 17 | port: 8080 -------------------------------------------------------------------------------- /src/main/resources/templates/mp/mapper.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.Mapper}; 2 | 3 | import ${package.Entity}.${entity}; 4 | import ${superMapperClassPackage}; 5 | import org.apache.ibatis.annotations.Mapper; 6 | import org.springframework.stereotype.Repository; 7 | 8 | 9 | /** 10 | *
11 | * ${table.comment!} Mapper 接口 12 | *
13 | * 14 | * @author ${author} 15 | * @since ${date} 16 | */ 17 | @Mapper 18 | @Repository 19 | <#if kotlin> 20 | interface ${table.mapperName} : ${superMapperClass}<${entity}> 21 | <#else> 22 | public interface ${table.mapperName} extends ${superMapperClass}<${entity}> { 23 | 24 | } 25 | #if> 26 | -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/exception/bizException/BizExceptionCode.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.exception.bizException; 2 | 3 | 4 | /** 5 | * @Description : 业务异常的错误代码接口 6 | * @Param : 7 | * @Return : 8 | * @Author : SheldonPeng 9 | * @Date : 2019-10-11 10 | */ 11 | 12 | public interface BizExceptionCode { 13 | 14 | /** 15 | * @Description : 获取错误代码 16 | * @Param : [] 17 | * @Return : java.lang.String 18 | * @Author : SheldonPeng 19 | * @Date : 2019-10-11 20 | */ 21 | String getCode(); 22 | 23 | /** 24 | * @Description : 获取错误信息 25 | * @Param : [] 26 | * @Return : java.lang.String 27 | * @Author : SheldonPeng 28 | * @Date : 2019-10-11 29 | */ 30 | String getMessage(); 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/config/MybatisPlusConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.config; 2 | 3 | import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor; 4 | import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.transaction.annotation.EnableTransactionManagement; 9 | 10 | @EnableTransactionManagement 11 | @Configuration 12 | public class MybatisPlusConfig { 13 | 14 | @Bean 15 | public PaginationInterceptor paginationInterceptor() { 16 | PaginationInterceptor paginationInterceptor = new PaginationInterceptor(); 17 | // 设置请求的页面大于最大页后操作, true调回到首页,false 继续请求 默认false 18 | // paginationInterceptor.setOverflow(false); 19 | // 设置最大单页限制数量,默认 500 条,-1 不受限制 20 | // paginationInterceptor.setLimit(500); 21 | // 开启 count 的 join 优化,只针对部分 left join 22 | paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true)); 23 | return paginationInterceptor; 24 | } 25 | } -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/exception/bizException/BizException.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.exception.bizException; 2 | 3 | /** 4 | * @Description : 运行时业务中出现的异常 5 | * @Param : 6 | * @Return : 7 | * @Author : SheldonPeng 8 | * @Date : 2019-10-11 9 | */ 10 | public class BizException extends RuntimeException{ 11 | 12 | private static final long serialVersionUID = -7864604160297181941L; 13 | 14 | private final String code; 15 | 16 | /** 17 | * @Description : 指定枚举类中的错误类 18 | * @Param : [errorCode] 19 | * @Return : 20 | * @Author : SheldonPeng 21 | * @Date : 2019-10-11 22 | */ 23 | public BizException(final BizExceptionCode exceptionCode) { 24 | super(exceptionCode.getMessage()); 25 | this.code = exceptionCode.getCode(); 26 | } 27 | /** 28 | * @Description : 指定具体业务错误的信息 29 | * @Param : [detailedMessage] 30 | * @Return : 31 | * @Author : SheldonPeng 32 | * @Date : 2019-10-11 33 | */ 34 | public BizException(final String message) { 35 | super(message); 36 | this.code = BizExceptionCodeEnum.SPECIFIED.getCode(); 37 | } 38 | 39 | public String getCode() { 40 | return code; 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/exception/NoSuchAddressException.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.exception; 2 | 3 | 4 | import cn.hellochaos.generator.exception.bizException.BizExceptionCode; 5 | import cn.hellochaos.generator.exception.bizException.BizExceptionCodeEnum; 6 | 7 | /** 8 | * @Description : 运行时业务中出现的异常 9 | * @Param : 10 | * @Return : 11 | * @Author : SheldonPeng 12 | * @Date : 2019-10-11 13 | */ 14 | public class NoSuchAddressException extends RuntimeException{ 15 | 16 | private static final long serialVersionUID = -7864604160297181941L; 17 | 18 | private final String code; 19 | 20 | /** 21 | * @Description : 指定枚举类中的错误类 22 | * @Param : [errorCode] 23 | * @Return : 24 | * @Author : SheldonPeng 25 | * @Date : 2019-10-11 26 | */ 27 | public NoSuchAddressException(final BizExceptionCode exceptionCode) { 28 | super(exceptionCode.getMessage()); 29 | this.code = exceptionCode.getCode(); 30 | } 31 | /** 32 | * @Description : 指定具体业务错误的信息 33 | * @Param : [detailedMessage] 34 | * @Return : 35 | * @Author : SheldonPeng 36 | * @Date : 2019-10-11 37 | */ 38 | public NoSuchAddressException(final String message) { 39 | super(message); 40 | this.code = BizExceptionCodeEnum.SPECIFIED.getCode(); 41 | } 42 | 43 | public String getCode() { 44 | return code; 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/exception/bizException/BizExceptionCodeEnum.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.exception.bizException; 2 | 3 | 4 | /** 5 | * @Description : 异常消息的枚举类(此类属于业务异常枚举类) 6 | * @Param : 7 | * @Return : 8 | * @Author : SheldonPeng 9 | * @Date : 2019-10-11 10 | */ 11 | public enum BizExceptionCodeEnum implements BizExceptionCode{ 12 | 13 | // 已指明的异常,在异常使用时message并不返回前端,返回前端的为throw新的异常时指定的message 14 | SPECIFIED("-1","系统发生异常,请稍后重试"), 15 | 16 | // 常用业务异常 17 | USER_NAME_NULL("-1","用户名不能为空,请重新输入!"), 18 | USER_PASSWORD_NULL("-1","密码不能为空,请重新输入!"), 19 | USER_PASSWORD_WRONG("-1","密码错误,请检查后重新输入!"), 20 | PAGE_NUM_NULL("4001","页码不能为空"), 21 | PAGE_SIZE_NULL("4002","页数不能为空"), 22 | SEARCH_NULL("4004","搜索条件不能为空,请检查后重新输入!"), 23 | NO_LOGIN("3001", "用户未进行登录") 24 | ; 25 | 26 | private final String code; 27 | 28 | private final String message; 29 | 30 | /** 31 | * @Description : 32 | * @Param : [code, message] 33 | * @Return : 34 | * @Author : SheldonPeng 35 | * @Date : 2019-10-11 36 | */ 37 | BizExceptionCodeEnum(String code,String message){ 38 | 39 | this.code = code; 40 | this.message = message; 41 | } 42 | 43 | @Override 44 | public String getCode() { 45 | return code; 46 | } 47 | 48 | @Override 49 | public String getMessage() { 50 | return message; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/entity/dto/ResultBean.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.entity.dto; 2 | 3 | import cn.hellochaos.generator.exception.bizException.BizException; 4 | import lombok.AllArgsConstructor; 5 | import lombok.Builder; 6 | import lombok.Data; 7 | import lombok.NoArgsConstructor; 8 | 9 | import java.io.Serializable; 10 | 11 | /** 12 | * @Description: $ 13 | * @Param: $ 14 | * @return: $ 15 | * @author: SheledonPeng 16 | * @Date: $ 17 | */ 18 | @Data 19 | @Builder 20 | @AllArgsConstructor 21 | @NoArgsConstructor 22 | public class ResultBean9 | * ${table.comment!} 服务类 10 | *
11 | * 12 | * @author ${author} 13 | * @since ${date} 14 | */ 15 | <#if kotlin> 16 | interface ${table.serviceName} : ${superServiceClass}<${entity}> 17 | <#else> 18 | public interface ${table.serviceName} { 19 | 20 | /** 21 | * 分页查询${entity} 22 | * 23 | * @param page 当前页数 24 | * @param pageSize 页的大小 25 | * @param factor 搜索关键词 26 | * @return 返回mybatis-plus的Page对象,其中records字段为符合条件的查询结果 27 | * @author ${author} 28 | * @since ${date} 29 | */ 30 | Page<${entity}> list${entity}sByPage(int page, int pageSize, String factor); 31 | 32 | /** 33 | * 根据id查询${entity} 34 | * 35 | * @param id 需要查询的${entity}的id 36 | * @return 返回对应id的${entity}对象 37 | * @author ${author} 38 | * @since ${date} 39 | */ 40 | ${entity} get${entity}ById(int id); 41 | 42 | /** 43 | * 插入${entity} 44 | * 45 | * @param ${entity?uncap_first} 需要插入的${entity}对象 46 | * @return 返回插入成功之后${entity}对象的id 47 | * @author ${author} 48 | * @since ${date} 49 | */ 50 | int insert${entity}(${entity} ${entity?uncap_first}); 51 | 52 | /** 53 | * 根据id删除${entity} 54 | * 55 | * @param id 需要删除的${entity}对象的id 56 | * @return 返回被删除的${entity}对象的id 57 | * @author ${author} 58 | * @since ${date} 59 | */ 60 | int delete${entity}ById(int id); 61 | 62 | /** 63 | * 根据id更新${entity} 64 | * 65 | * @param ${entity?uncap_first} 需要更新的${entity}对象 66 | * @return 返回被更新的${entity}对象的id 67 | * @author ${author} 68 | * @since ${date} 69 | */ 70 | int update${entity}(${entity} ${entity?uncap_first}); 71 | 72 | } 73 | #if> 74 | -------------------------------------------------------------------------------- /src/main/resources/templates/mp/controller.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.Controller}; 2 | 3 | 4 | import org.springframework.web.bind.annotation.*; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import ${package.Entity}.dto.ResultBean; 7 | import ${package.Service}.${table.serviceName}; 8 | import ${package.Entity}.${entity}; 9 | <#if restControllerStyle> 10 | import org.springframework.web.bind.annotation.RestController; 11 | <#else> 12 | import org.springframework.stereotype.Controller; 13 | #if> 14 | <#if superControllerClassPackage??> 15 | import ${superControllerClassPackage}; 16 | #if> 17 | import javax.annotation.Resource; 18 | 19 | 20 | /** 21 | *22 | * ${table.comment!} 前端控制器 23 | *
24 | * 25 | * @author ${author} 26 | * @since ${date} 27 | * @version v1.0 28 | */ 29 | <#if restControllerStyle> 30 | @RestController 31 | <#else> 32 | @Controller 33 | #if> 34 | @RequestMapping("<#if package.ModuleName??>/${package.ModuleName}#if>/api/v1/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}#if>") 35 | <#if kotlin> 36 | class ${table.controllerName}<#if superControllerClass??> : ${superControllerClass}()#if> 37 | <#else> 38 | <#if superControllerClass??> 39 | public class ${table.controllerName} extends ${superControllerClass} { 40 | <#else> 41 | public class ${table.controllerName} { 42 | #if> 43 | 44 | @Resource 45 | private ${table.serviceName} ${table.serviceName?uncap_first}; 46 | 47 | /** 48 | * 查询分页数据 49 | */ 50 | @RequestMapping(method = RequestMethod.GET) 51 | public ResultBean> listByPage(@RequestParam(name = "page", defaultValue = "1") int page, 52 | @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, 53 | @RequestParam(name = "factor", defaultValue = "") String factor) { 54 | return new ResultBean<>(${table.serviceName?uncap_first}.list${entity}sByPage(page, pageSize,factor)); 55 | } 56 | 57 | 58 | /** 59 | * 根据id查询 60 | */ 61 | @RequestMapping(method = RequestMethod.GET, value = "/{id}") 62 | public ResultBean> getById(@PathVariable("id") Integer id) { 63 | return new ResultBean<>(${table.serviceName?uncap_first}.get${entity}ById(id)); 64 | } 65 | 66 | /** 67 | * 新增 68 | */ 69 | @RequestMapping(method = RequestMethod.POST) 70 | public ResultBean> insert(@RequestBody ${entity} ${entity?uncap_first}) { 71 | return new ResultBean<>(${table.serviceName?uncap_first}.insert${entity}(${entity?uncap_first})); 72 | } 73 | 74 | /** 75 | * 删除 76 | */ 77 | @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") 78 | public ResultBean> deleteById(@PathVariable("id") Integer id) { 79 | return new ResultBean<>(${table.serviceName?uncap_first}.delete${entity}ById(id)); 80 | } 81 | 82 | /** 83 | * 修改 84 | */ 85 | @RequestMapping(method = RequestMethod.PUT) 86 | public ResultBean> updateById(@RequestBody ${entity} ${entity?uncap_first}) { 87 | return new ResultBean<>(${table.serviceName?uncap_first}.update${entity}(${entity?uncap_first})); 88 | } 89 | } 90 | #if> -------------------------------------------------------------------------------- /src/main/java/cn/hellochaos/generator/config/LocalDateTimeSerializerConfig.java: -------------------------------------------------------------------------------- 1 | package cn.hellochaos.generator.config; 2 | 3 | 4 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 5 | import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer; 6 | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer; 7 | import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer; 8 | import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; 9 | import org.springframework.context.annotation.Bean; 10 | import org.springframework.context.annotation.Configuration; 11 | import org.springframework.core.convert.converter.Converter; 12 | 13 | import java.time.LocalDate; 14 | import java.time.LocalDateTime; 15 | import java.time.format.DateTimeFormatter; 16 | 17 | /** 18 | * @author 黄钰朝 19 | * @description LocalDateTime序列化配置 20 | * @date 2020-05-04 16:32 21 | */ 22 | @Configuration 23 | public class LocalDateTimeSerializerConfig { 24 | private static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss"; 25 | private static final String DATE_PATTERN = "yyyy-MM-dd"; 26 | 27 | /** 28 | * string转localdate 29 | */ 30 | @Bean 31 | public Converter15 | * ${table.comment!} 服务实现类 16 | *
17 | * 18 | * @author ${author} 19 | * @since ${date} 20 | */ 21 | @Slf4j 22 | @Service 23 | <#if kotlin> 24 | open class ${table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} { 25 | 26 | } 27 | <#else> 28 | public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { 29 | 30 | @Override 31 | public Page<${entity}> list${entity}sByPage(int page, int pageSize, String factor) { 32 | log.info("正在执行分页查询${entity?uncap_first}: page = {} pageSize = {} factor = {}",page,pageSize,factor); 33 | QueryWrapper<${entity}> queryWrapper = new QueryWrapper<${entity}>().like("", factor); 34 | //TODO 这里需要自定义用于匹配的字段,并把wrapper传入下面的page方法 35 | Page<${entity}> result = super.page(new Page<>(page, pageSize)); 36 | log.info("分页查询${entity?uncap_first}完毕: 结果数 = {} ",result.getRecords().size()); 37 | return result; 38 | } 39 | 40 | @Override 41 | public ${entity} get${entity}ById(int id) { 42 | log.info("正在查询${entity?uncap_first}中id为{}的数据",id); 43 | ${entity} ${entity?uncap_first} = super.getById(id); 44 | log.info("查询id为{}的${entity?uncap_first}{}",id,(null == ${entity?uncap_first}?"无结果":"成功")); 45 | return ${entity?uncap_first}; 46 | } 47 | 48 | @Override 49 | public int insert${entity}(${entity} ${entity?uncap_first}) { 50 | log.info("正在插入${entity?uncap_first}"); 51 | if (super.save(${entity?uncap_first})) { 52 | log.info("插入${entity?uncap_first}成功,id为{}",${entity?uncap_first}.get${entity}Id()); 53 | return ${entity?uncap_first}.get${entity}Id(); 54 | } else { 55 | log.error("插入${entity?uncap_first}失败"); 56 | throw new BizException("添加失败"); 57 | } 58 | } 59 | 60 | @Override 61 | public int delete${entity}ById(int id) { 62 | log.info("正在删除id为{}的${entity?uncap_first}",id); 63 | if (super.removeById(id)) { 64 | log.info("删除id为{}的${entity?uncap_first}成功",id); 65 | return id; 66 | } else { 67 | log.error("删除id为{}的${entity?uncap_first}失败",id); 68 | throw new BizException("删除失败[id=" + id + "]"); 69 | } 70 | } 71 | 72 | @Override 73 | public int update${entity}(${entity} ${entity?uncap_first}) { 74 | log.info("正在更新id为{}的${entity?uncap_first}",${entity?uncap_first}.get${entity}Id()); 75 | if (super.updateById(${entity?uncap_first})) { 76 | log.info("更新d为{}的${entity?uncap_first}成功",${entity?uncap_first}.get${entity}Id()); 77 | return ${entity?uncap_first}.get${entity}Id(); 78 | } else { 79 | log.error("更新id为{}的${entity?uncap_first}失败",${entity?uncap_first}.get${entity}Id()); 80 | throw new BizException("更新失败[id=" + ${entity?uncap_first}.get${entity}Id() + "]"); 81 | } 82 | } 83 | 84 | } 85 | #if> 86 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 |53 | * 读取控制台内容 54 | *
55 | */ 56 | public static String scanner(String tip) { 57 | Scanner scanner = new Scanner(System.in); 58 | StringBuilder help = new StringBuilder(); 59 | help.append("请输入" + tip + ":"); 60 | System.out.println(help.toString()); 61 | if (scanner.hasNext()) { 62 | String ipt = scanner.next(); 63 | if (ipt != null && !ipt.trim().isEmpty()) { 64 | return ipt; 65 | } 66 | } 67 | throw new MybatisPlusException("请输入正确的" + tip + "!"); 68 | } 69 | 70 | 71 | public void execute() { 72 | // 代码生成器 73 | AutoGenerator mpg = new AutoGenerator(); 74 | 75 | // 全局配置 76 | GlobalConfig gc = new GlobalConfig(); 77 | gc.setOutputDir(projectPath + "/src/main/java"); 78 | gc.setAuthor(scanner("Author")); 79 | gc.setOpen(false); 80 | gc.setActiveRecord(true); 81 | gc.setIdType(IdType.AUTO); 82 | gc.setServiceName("%sService"); 83 | gc.setBaseResultMap(true); 84 | gc.setBaseColumnList(true); 85 | gc.setFileOverride(true); 86 | // gc.setSwagger2(true); 实体属性 Swagger2 注解 87 | mpg.setGlobalConfig(gc); 88 | 89 | // 数据源配置 90 | mpg.setDataSource(dataSourceConfig()); 91 | 92 | // 包配置 93 | PackageConfig pc = new PackageConfig(); 94 | pc.setParent(parentPackage); 95 | pc.setModuleName(module); 96 | mpg.setPackageInfo(pc); 97 | 98 | // 自定义配置 99 | InjectionConfig cfg = new InjectionConfig() { 100 | @Override 101 | public void initMap() { 102 | // to do nothing 103 | } 104 | }; 105 | mpg.setCfg(cfg); 106 | 107 | // 配置模板 108 | TemplateConfig templateConfig = new TemplateConfig(); 109 | // 配置自定义输出模板 110 | //指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别 111 | templateConfig.setXml(null); 112 | templateConfig.setService(serviceTemplate); 113 | templateConfig.setServiceImpl(serviceImplTemplate); 114 | templateConfig.setMapper(mapperTemplate); 115 | templateConfig.setController(controllerTemplate); 116 | mpg.setTemplate(templateConfig); 117 | 118 | // 策略配置 119 | StrategyConfig strategy = new StrategyConfig(); 120 | strategy.setNaming(NamingStrategy.underline_to_camel); 121 | strategy.setColumnNaming(NamingStrategy.underline_to_camel); 122 | // strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!"); 123 | strategy.setEntityLombokModel(true); 124 | strategy.setRestControllerStyle(true); 125 | // 公共父类 126 | // strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!"); 127 | // 写于父类中的公共字段 128 | strategy.setSuperEntityColumns("id"); 129 | strategy.setInclude(scanner("表名,多个英文逗号分割").split(",")); 130 | strategy.setControllerMappingHyphenStyle(true); 131 | // strategy.setTablePrefix(pc.getModuleName() + "_"); 132 | mpg.setStrategy(strategy); 133 | 134 | 135 | mpg.setTemplateEngine(new FreemarkerTemplateEngine()); 136 | mpg.execute(); 137 | } 138 | 139 | 140 | private static DataSourceConfig dataSourceConfig() { 141 | DataSourceConfig dsc = new DataSourceConfig(); 142 | 143 | String resourcePath = System.getProperty("user.dir") + "/src/main/resources/" + APP_PROPERTY; 144 | try { 145 | InputStream inStream = new FileInputStream(new File(resourcePath)); 146 | Properties prop = new Properties(); 147 | prop.load(inStream); 148 | 149 | dsc.setUrl(prop.getProperty("spring.datasource.url")); 150 | dsc.setDriverName(prop.getProperty("spring.datasource.driver-class-name")); 151 | dsc.setUsername(prop.getProperty("spring.datasource.username")); 152 | dsc.setPassword(prop.getProperty("spring.datasource.password")); 153 | 154 | } catch (IOException e) { 155 | e.printStackTrace(); 156 | } 157 | 158 | return dsc; 159 | } 160 | 161 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 基于mybatis-plus的springboot代码生成器 2 | 3 | 4 | 5 | [](https://github.com/misterchaos/springboot-mybatis-plus-generator) 6 | [](https://github.com/misterchaos/springboot-mybatis-plus-generator/releases) 7 | [](https://github.com/misterchaos/springboot-mybatis-plus-generator/releases) 8 |  9 | 10 | [](https://starchart.cc/misterchaos/springboot-mybatis-plus-generator) 11 | 12 | ## :droplet:Overview 13 | 14 | 这是一个基于mybatis-plus官方的AutoGenerator代码生成器+定制代码模板的[springboot代码生成器](https://github.com/misterchaos/springboot-mybatis-plus-generator)。 15 | 16 | **使用这个生成器你可以在1分钟之内生成数据库表对应的实体类,以及Mapper,Service,Controller层的基本CURD代码,并可以立即运行测试接口。** 17 | 18 | 如果你不了解什么是mybatis-plus,请参考[官方文档](https://mp.baomidou.com/) 19 | 20 | 本代码生成器具有以下优点: 21 | 22 | - 只需三步,即可开始测试CURD接口 23 | - 生成的代码风格良好,注释详细(遵循阿里巴巴开发规范) 24 | - 带有程序执行日志打印和错误日志打印 25 | 26 | ## :star:Features 27 | 28 | - 实现controller restful风格CURD接口 29 | - service层CURD对IService的方法再次封装,方便添加业务逻辑 30 | - serviceImpl中方法实现执行日志打印 31 | - mapper模板在官方模板基础上加入@mapper注解 32 | - 各模板方法添加Javadoc注释 33 | - 实现分页查询,关键词模糊查询(需自定义字段) 34 | ## :point_right:Quick Start 35 | 36 | **动画演示**: 37 | 38 |  39 | 40 | 41 | 42 | **使用步骤:** 43 | 44 | - 修改`src/main/resources/application.properties`配置文件(自行创建),设置数据库信息 45 | ``` 46 | #DataSource Config 47 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 48 | spring.datasource.url=jdbc:mysql://localhost:3306/flower?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT 49 | spring.datasource.username=root 50 | spring.datasource.password= 51 | ``` 52 | - 运行`CodeGenerator`类,输入Author,输入数据库表名 53 | - 运行`SpringbootMybatisPlusGeneratorApplication`,测试接口 54 | 55 | > 注意:数据库表必须符合以下规范74 | * 用户 前端控制器 75 | *
76 | * 77 | * @author chaos 78 | * @since 2020-05-02 79 | * @version v1.0 80 | */ 81 | @RestController 82 | @RequestMapping("/generator/api/v1/user") 83 | public class UserController { 84 | 85 | @Autowired 86 | private UserService userService; 87 | 88 | /** 89 | * 查询分页数据 90 | */ 91 | @RequestMapping(method = RequestMethod.GET) 92 | public ResultBean> listByPage(@RequestParam(name = "page", defaultValue = "1") int page, 93 | @RequestParam(name = "pageSize", defaultValue = "10") int pageSize, 94 | @RequestParam String keyword) { 95 | return new ResultBean<>(userService.listUsersByPage(page, pageSize,keyword)); 96 | } 97 | 98 | 99 | /** 100 | * 根据id查询 101 | */ 102 | @RequestMapping(method = RequestMethod.GET, value = "/{id}") 103 | public ResultBean> getById(@PathVariable("id") Integer id) { 104 | return new ResultBean<>(userService.getUserById(id)); 105 | } 106 | 107 | /** 108 | * 新增 109 | */ 110 | @RequestMapping(method = RequestMethod.POST) 111 | public ResultBean> insert(@RequestBody User user) { 112 | return new ResultBean<>(userService.insertUser(user)); 113 | } 114 | 115 | /** 116 | * 删除 117 | */ 118 | @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") 119 | public ResultBean> deleteById(@PathVariable("id") Integer id) { 120 | return new ResultBean<>(userService.deleteUserById(id)); 121 | } 122 | 123 | /** 124 | * 修改 125 | */ 126 | @RequestMapping(method = RequestMethod.PUT) 127 | public ResultBean> updateById(@RequestBody User user) { 128 | return new ResultBean<>(userService.updateUser(user)); 129 | } 130 | } 131 | 132 | ``` 133 | 134 | ### 2.Service模板代码示例 135 | ```java 136 | package cn.hellochaos.generator.service; 137 | 138 | import cn.hellochaos.generator.entity.User; 139 | import com.baomidou.mybatisplus.extension.service.IService; 140 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 141 | 142 | /** 143 | *144 | * 用户 服务类 145 | *
146 | * 147 | * @author chaos 148 | * @since 2020-05-02 149 | */ 150 | public interface UserService { 151 | 152 | /** 153 | * 分页查询User 154 | * 155 | * @param page 当前页数 156 | * @param pageSize 页的大小 157 | * @param keyword 搜索关键词 158 | * @return 返回mybatis-plus的Page对象,其中records字段为符合条件的查询结果 159 | * @author chaos 160 | * @since 2020-05-02 161 | */ 162 | Page224 | * 用户 服务实现类 225 | *
226 | * 227 | * @author chaos 228 | * @since 2020-05-02 229 | */ 230 | @Slf4j 231 | @Service 232 | public class UserServiceImpl extends ServiceImpl