大佬新搭建的项目是mybatis—plus,因而自己学习使用下,对于这些固定的代码还是一键生成比较好。可根据模板自定义生成内容。
直接上代码了。整个项目我附上网盘地址链接: https://pan.baidu.com/s/1EhAa__HTcGJSYWBnvAuLNA 提取码: nd8a
maven依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 <dependency > <groupId > mysql</groupId > <artifactId > mysql-connector-java</artifactId > <version > 5.1.37</version > </dependency > <dependency > <groupId > org.projectlombok</groupId > <artifactId > lombok</artifactId > </dependency > <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-boot-starter</artifactId > <version > 3.0.5</version > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > com.baomidou</groupId > <artifactId > mybatis-plus-generator</artifactId > <version > 3.3.2</version > </dependency > <dependency > <groupId > org.apache.velocity</groupId > <artifactId > velocity-engine-core</artifactId > <version > 2.2</version > </dependency > <dependency > <groupId > org.freemarker</groupId > <artifactId > freemarker</artifactId > <version > 2.3.30</version > </dependency > <dependency > <groupId > com.ibeetl</groupId > <artifactId > beetl</artifactId > <version > 3.1.3.RELEASE</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger2</artifactId > <version > 2.7.0</version > </dependency > <dependency > <groupId > io.springfox</groupId > <artifactId > springfox-swagger-ui</artifactId > <version > 2.7.0</version > </dependency > <dependency > <groupId > com.alibaba</groupId > <artifactId > fastjson</artifactId > <version > 1.2.36</version > </dependency >
新建 GenerateMybatisPlus.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 import com.baomidou.mybatisplus.annotation.DbType;import com.baomidou.mybatisplus.annotation.FieldFill;import com.baomidou.mybatisplus.annotation.IdType;import com.baomidou.mybatisplus.generator.AutoGenerator;import com.baomidou.mybatisplus.generator.config.*;import com.baomidou.mybatisplus.generator.config.po.TableFill;import com.baomidou.mybatisplus.generator.config.rules.DateType;import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.test.context.TestPropertySource;import java.util.ArrayList;import java.util.Arrays;public class GenerateMybatisPlus { public void generate (String dataSourceurl, String dataSourcename, String dataSourcepassword, String dataSourcedriver, String tables, String packageParent, boolean isNormalize) { AutoGenerator mpg = new AutoGenerator (); GlobalConfig gc = new GlobalConfig (); String projectPath = System.getProperty("user.dir" ); gc.setOutputDir(projectPath + "/src/main/java" ); gc.setAuthor("liyh" ); gc.setOpen(false ); gc.setFileOverride(true ); gc.setIdType(IdType.ASSIGN_UUID); gc.setDateType(DateType.ONLY_DATE); gc.setSwagger2(true ); gc.setActiveRecord(true ); gc.setEnableCache(false ); gc.setBaseResultMap(true ); gc.setBaseColumnList(false ); gc.setControllerName("%sController" ); gc.setServiceName("%sService" ); gc.setServiceImplName("%sServiceImpl" ); gc.setMapperName("%sMapper" ); gc.setXmlName("%sMapper" ); mpg.setGlobalConfig(gc); DataSourceConfig dsc = new DataSourceConfig (); dsc.setDbType(DbType.MYSQL); dsc.setUrl(dataSourceurl); dsc.setDriverName(dataSourcedriver); dsc.setUsername(dataSourcename); dsc.setPassword(dataSourcepassword); mpg.setDataSource(dsc); PackageConfig pc = new PackageConfig (); pc.setParent(packageParent); pc.setController("controller" ); pc.setService("service" ); pc.setServiceImpl("service.impl" ); pc.setMapper("dao" ); pc.setEntity("entity" ); pc.setXml("mapping" ); pc.setModuleName("demo" ); mpg.setPackageInfo(pc); StrategyConfig strategy = new StrategyConfig (); strategy.setInclude(tables.split("," )); strategy.setNaming(NamingStrategy.underline_to_camel); strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(true ); strategy.setCapitalMode(false ); strategy.setSuperMapperClass(null ); if (true == isNormalize) { strategy.setLogicDeleteFieldName("deleted" ); TableFill gmtCreate = new TableFill ("gmt_create" , FieldFill.INSERT); TableFill gmtModified = new TableFill ("gmt_modified" , FieldFill.INSERT_UPDATE); ArrayList<TableFill> tableFills = new ArrayList <>(); tableFills.add(gmtCreate); tableFills.add(gmtModified); strategy.setTableFillList(tableFills); strategy.setVersionFieldName("version" ); } strategy.setRestControllerStyle(true ); strategy.setControllerMappingHyphenStyle(true ); strategy.setEntityTableFieldAnnotationEnable(true ); strategy.setEntityBooleanColumnRemoveIsPrefix(true ); strategy.setTablePrefix("mdm_" ); strategy.setControllerMappingHyphenStyle(false ); mpg.setStrategy(strategy); mpg.setTemplateEngine(new FreemarkerTemplateEngine ()); TemplateConfig tc = new TemplateConfig (); tc.setController("/templatesFreemaker/controller.java" ); tc.setService("/templatesFreemaker/service.java" ); tc.setServiceImpl("/templatesFreemaker/serviceImpl.java" ); tc.setEntity("/templatesFreemaker/entity.java" ); tc.setMapper("/templatesFreemaker/mapper.java" ); tc.setXml("/templatesFreemaker/mapper.xml" ); mpg.setTemplate(tc); mpg.execute(); } }
在resource下新建文件 generate.properties,常用的可以放在这里
1 2 3 4 5 6 7 8 9 datasource.username =root datasource.password =123456 datasource.url =jdbc:mysql://127.0.0.1:3306/basic_project?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8 datasource.driver-class-name =com.mysql.jdbc.Driver package.parent =com.xxxx.generate.generatedemo datatables.name =user,mdm_inf_rec_data
在springboot项目下test下新建 GenerateApplicationTests.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 import com.hong.generate.mybatisplus.GenerateMybatisPlus;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Value;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.TestPropertySource;@SpringBootTest @TestPropertySource("classpath:generate.properties") class GenerateApplicationTests { @Value("${datasource.url}") private String dataSourceurl; @Value("${datasource.username}") private String dataSourcename; @Value("${datasource.password}") private String dataSourcepassword; @Value("${datasource.driver-class-name}") private String dataSourcedriver; @Value("${datatables.name}") private String tables; @Value("${package.parent}") private String packageParent; @Value("${datatables.isNormalize}") private boolean isNormalize; @Test void generateMybatisPlusTest () { new GenerateMybatisPlus ().generate( dataSourceurl, dataSourcename, dataSourcepassword, dataSourcedriver, tables, packageParent, isNormalize); } }
最重要的模板内容
controller.java.ftl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 package ${package .Controller};import ${package .Service}.${table.serviceName};import ${package .Entity}.${entity};import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;import com.hong.generate.common.PageResult;import com.hong.generate.common.Result;import com.hong.generate.common.StatusCode;import com.baomidou.mybatisplus.core.metadata.IPage;import com.baomidou.mybatisplus.extension.plugins.pagination.Page;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import java.util.List;<#if restControllerStyle> import org.springframework.web.bind.annotation.RestController; <#else > import org.springframework.stereotype.Controller;</#if > <#if superControllerClassPackage??> import ${superControllerClassPackage}; </#if > @Slf4j @Api(tags = "${table.comment!}") <#if restControllerStyle> @RestController <#else > @Controller </#if > @RequestMapping("<#if package.ModuleName??>/${package.ModuleName}</#if>/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${table.entityPath}</#if>") <#if kotlin> class $ {table.controllerName}<#if superControllerClass??> : ${superControllerClass}()</#if > <#else > <#if superControllerClass??> public class $ {table.controllerName} extends $ {superControllerClass} { <#else > public class $ {table.controllerName} { @Autowired public ${table.serviceName} ${table.entityPath}Service; @ApiOperation(value = "新增") @PostMapping("/save") public Result save (@RequestBody ${entity} ${table.entityPath}) { ${table.entityPath}Service.save(${table.entityPath}); return new Result (StatusCode.SUCCESS,"保存成功" ); } @ApiOperation(value = "根据id删除") @PostMapping("/delete/{id}") public Result delete (@PathVariable("id") Long id) { ${table.entityPath}Service.removeById(id); return new Result (StatusCode.SUCCESS,"删除成功" ); } @ApiOperation(value = "条件查询") @PostMapping("/get") public Result list (@RequestBody ${entity} ${table.entityPath}) { List<${entity}> ${table.entityPath}List = ${table.entityPath}Service.list(new QueryWrapper <>(${table.entityPath})); return new Result (StatusCode.SUCCESS,"查询成功" ,${table.entityPath}List); } @ApiOperation(value = "列表(分页)") @GetMapping("/list/{pageNum}/{pageSize}") public Object list (@PathVariable("pageNum") Long pageNum, @PathVariable("pageSize") Long pageSize) { IPage<${entity}> page = ${table.entityPath}Service.page( new Page <>(pageNum, pageSize), null ); return new Result (StatusCode.SUCCESS,"查询成功" ,new PageResult <>(page.getTotal(),page.getRecords())); } @ApiOperation(value = "详情") @GetMapping("/get/{id}") public Result get (@PathVariable("id") String id) { ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id); return new Result (StatusCode.SUCCESS,"查询成功" ,${table.entityPath}); } @ApiOperation(value = "根据id修改") @PostMapping("/update/{id}") public Result update (@PathVariable("id") String id, @RequestBody ${entity} ${table.entityPath}) { ${table.entityPath}.setId(id); ${table.entityPath}Service.updateById(${table.entityPath}); return new Result (StatusCode.SUCCESS,"更新成功" ); } </#if > } </#if >
entity.java.ftl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 package ${package .Entity};<#list table.importPackages as pkg> import ${pkg};</#list> <#if table.convert> import com.baomidou.mybatisplus.annotation.TableName;</#if > <#if swagger2> import io.swagger.annotations.ApiModel;import io.swagger.annotations.ApiModelProperty;</#if > <#if entityLombokModel> import lombok.Data;import lombok.EqualsAndHashCode;import lombok.experimental.Accessors;</#if > <#if entityLombokModel> @Data <#if superEntityClass??> @EqualsAndHashCode(callSuper = true) <#else > @EqualsAndHashCode(callSuper = false) </#if > @Accessors(chain = true) </#if > <#if table.convert> @TableName("${table.name}") </#if > <#if swagger2> @ApiModel(value = "${entity}对象", description = "${table.comment!}") </#if > <#if superEntityClass??> public class $ {entity} extends $ {superEntityClass}<#if activeRecord><${entity}></#if > {<#elseif activeRecord> public class $ {entity} extends Model <${entity}> {<#else > public class $ {entity} implements Serializable {</#if > private static final long serialVersionUID = 1L ; <#-- ---------- BEGIN 字段循环遍历 ----------> <#list table.fields as field> <#if field.keyFlag> <#assign keyPropertyName="${field.propertyName}" /> </#if > <#if field.comment!?length gt 0 > <#if swagger2> @ApiModelProperty(value = "${field.comment}") <#else > </#if > </#if > <#if field.keyFlag> <#-- 主键 --> <#if field.keyIdentityFlag> @TableId(value = "${field.name}", type = IdType.AUTO) <#elseif idType??> @TableId(value = "${field.name}", type = IdType.${idType}) <#elseif field.convert> @TableId("${field.name}") </#if > <#-- 普通字段 --> <#elseif field.fill??> <#-- ----- 存在字段填充设置 -----> <#if field.convert> @TableField(value = "${field.name}", fill = FieldFill.${field.fill}) <#else > @TableField(fill = FieldFill.${field.fill}) </#if > <#elseif field.convert> @TableField("${field.name}") </#if > <#-- 乐观锁注解 --> <#if (versionFieldName!"" ) == field.name> @Version </#if > <#-- 逻辑删除注解 --> <#if (logicDeleteFieldName!"" ) == field.name> @TableLogic </#if > private ${field.propertyType} ${field.propertyName}; </#list> <#------------ END 字段循环遍历 ----------> <#if !entityLombokModel> <#list table.fields as field> <#if field.propertyType == "boolean" > <#assign getprefix="is" /> <#else > <#assign getprefix="get" /> </#if > public ${field.propertyType} ${getprefix}${field.capitalName}() { return ${field.propertyName}; } <#if entityBuilderModel> public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) { <#else > public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) { </#if > this .${field.propertyName} = ${field.propertyName}; <#if entityBuilderModel> return this ; </#if > } </#list> </#if > <#if entityColumnConstant> <#list table.fields as field> public static final String ${field.name?upper_case} = "${field.name}" ; </#list> </#if > <#if activeRecord> @Override protected Serializable pkVal () { <#if keyPropertyName??> return this .${keyPropertyName}; <#else > return null ; </#if > } </#if > <#if !entityLombokModel> @Override public String toString () { return "${entity}{" + <#list table.fields as field> <#if field_index==0 > "${field.propertyName}=" + ${field.propertyName} + <#else > ", ${field.propertyName}=" + ${field.propertyName} + </#if > </#list> "}" ; } </#if > }
mapper.java.ftl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 package ${package .Mapper};import ${package .Entity}.${entity};import ${superMapperClassPackage};import org.apache.ibatis.annotations.Mapper;<#if kotlin> interface $ {table.mapperName} : ${superMapperClass}<${entity}><#else > @Mapper public interface $ {table.mapperName} extends $ {superMapperClass}<${entity}> {} </#if >
mapper.xml.ftl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 <?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="${package.Mapper}.${table.mapperName}" > <#if enableCache> <!-- 开启二级缓存 --> <cache type="org.mybatis.caches.ehcache.LoggingEhcache" /> </#if > <#if baseResultMap> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="${package.Entity}.${entity}" > <#list table.fields as field> <#if field.keyFlag><#--生成主键排在第一位--> <id column="${field.name}" property="${field.propertyName}" /> </#if > </#list> <#list table.commonFields as field><#--生成公共字段 --> <result column="${field.name}" property="${field.propertyName}" /> </#list> <#list table.fields as field> <#if !field.keyFlag><#--生成普通字段 --> <result column="${field.name}" property="${field.propertyName}" /> </#if > </#list> </resultMap> </#if > <#if baseColumnList> <!-- 通用查询结果列 --> <sql id="Base_Column_List" > <#list table.commonFields as field> ${field.name}, </#list> ${table.fieldNames} </sql> </#if > </mapper>
service.java.ftl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 package ${package .Service};import ${package .Entity}.${entity};import ${superServiceClassPackage};<#if kotlin> interface $ {table.serviceName} : ${superServiceClass}<${entity}><#else > public interface $ {table.serviceName} extends $ {superServiceClass}<${entity}> {} </#if >
serviceImpl.java.ftl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package ${package .ServiceImpl};import ${package .Entity}.${entity};import ${package .Mapper}.${table.mapperName};import ${package .Service}.${table.serviceName};import ${superServiceImplClassPackage};import org.springframework.stereotype.Service;@Service <#if kotlin> open class $ {table.serviceImplName} : ${superServiceImplClass}<${table.mapperName}, ${entity}>(), ${table.serviceName} { } <#else > public class $ {table.serviceImplName} extends $ {superServiceImplClass}<${table.mapperName}, ${entity}> implements $ {table.serviceName} {} </#if >
辅助类:
PageResult.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import java.util.List;@Data @NoArgsConstructor @AllArgsConstructor public class PageResult <T>{ private long total; private List<T> rows; }
Result.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 import lombok.*;import org.apache.ibatis.annotations.ConstructorArgs;@Data @NoArgsConstructor @AllArgsConstructor @RequiredArgsConstructor public class Result { @NonNull private Integer code; @NonNull private String message; private Object data; }
StatusCode.java
1 2 3 4 5 6 7 8 9 10 public class StatusCode { public static final int SUCCESS = 200 ; public static final int ERROR = 500 ; }
不足之处,望多多之处,定会改正。