├── .gitignore ├── pom.xml └── src └── main ├── java └── com │ └── example │ └── generator │ ├── AutoGenerator.java │ ├── CodeGeneratorDemo.java │ ├── builder │ └── ConfigBuilder.java │ ├── common │ ├── dao │ │ └── BaseMapper.java │ ├── request │ │ └── BaseRequest.java │ ├── response │ │ └── PageUtils.java │ └── service │ │ └── BaseService.java │ ├── config │ ├── DataSourceConfig.java │ ├── GlobalConfig.java │ ├── PackageConfig.java │ ├── StrategyConfig.java │ ├── converts │ │ ├── ITypeConvert.java │ │ ├── ITypeHandler.java │ │ └── apdater │ │ │ └── MySqlTypeConvert.java │ ├── rules │ │ ├── DbColumnType.java │ │ └── IColumnType.java │ └── type │ │ ├── DbType.java │ │ ├── IDbQuery.java │ │ └── querys │ │ └── MySqlQuery.java │ ├── constant │ ├── ConstValue.java │ └── StringPool.java │ ├── engine │ └── TemplateEngine.java │ ├── entity │ ├── TableField.java │ └── TableInfo.java │ └── util │ ├── AssetUtil.java │ └── NamingStrategyHelper.java └── resources └── templates ├── api.java.ftl ├── dao.java.ftl ├── dto.java.ftl ├── dubbo.java.ftl ├── entity.java.ftl ├── junit.java.ftl ├── mapper.xml.ftl ├── provider.java.ftl ├── request.java.ftl ├── requestAdd.java.ftl ├── requestSearch.java.ftl ├── requestUpd.java.ftl ├── service.java.ftl └── web.java.ftl /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | !.mvn/wrapper/maven-wrapper.jar 3 | 4 | ### STS ### 5 | .apt_generated 6 | .classpath 7 | .factorypath 8 | .project 9 | .settings 10 | .springBeans 11 | .sts4-cache 12 | 13 | ### IntelliJ IDEA ### 14 | .idea 15 | *.iws 16 | *.iml 17 | *.ipr 18 | *.class 19 | 20 | ### NetBeans ### 21 | /nbproject/private/ 22 | /nbbuild/ 23 | /dist/ 24 | /nbdist/ 25 | /.nb-gradle/ 26 | /build/ 27 | 28 | ### mac ### 29 | .DS_Store 30 | file/ 31 | logs/ 32 | */target 33 | target/ 34 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | springboot-frame-example 7 | com.example.springboot 8 | 1.0.0 9 | 10 | 4.0.0 11 | 12 | springboot-example-generator 13 | 14 | 15 | 1.8 16 | UTF-8 17 | UTF-8 18 | 5.1.2.RELEASE 19 | 8.0.13 20 | 21 | 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter 27 | 28 | 29 | 30 | org.springframework.boot 31 | spring-boot-starter-web 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-starter-tomcat 37 | provided 38 | 39 | 40 | 41 | org.springframework.boot 42 | spring-boot-starter-test 43 | test 44 | 45 | 46 | 47 | mysql 48 | mysql-connector-java 49 | ${mysql.version} 50 | runtime 51 | 52 | 53 | 54 | com.alibaba 55 | druid-spring-boot-starter 56 | 1.1.10 57 | 58 | 59 | com.alibaba 60 | druid 61 | 62 | 63 | 64 | 65 | com.alibaba 66 | druid 67 | 1.1.12 68 | 69 | 70 | 71 | org.mybatis.spring.boot 72 | mybatis-spring-boot-starter 73 | 2.0.0 74 | 75 | 76 | 77 | org.projectlombok 78 | lombok 79 | 1.18.4 80 | provided 81 | 82 | 83 | 84 | com.google.guava 85 | guava 86 | 27.0.1-jre 87 | 88 | 89 | 90 | com.alibaba 91 | fastjson 92 | 1.2.46 93 | 94 | 95 | 96 | javax.servlet 97 | javax.servlet-api 98 | 3.1.0 99 | provided 100 | 101 | 102 | org.aspectj 103 | aspectjweaver 104 | 105 | 106 | org.apache.commons 107 | commons-lang3 108 | 109 | 110 | org.freemarker 111 | freemarker 112 | 2.3.23 113 | 114 | 115 | io.swagger 116 | swagger-annotations 117 | 1.6.2 118 | 119 | 120 | 121 | 122 | ${project.artifactId} 123 | 124 | 125 | org.springframework.boot 126 | spring-boot-maven-plugin 127 | 128 | true 129 | 130 | 131 | 132 | 133 | org.apache.maven.plugins 134 | maven-surefire-plugin 135 | 136 | true 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/AutoGenerator.java: -------------------------------------------------------------------------------- 1 | package com.example.generator; 2 | 3 | import com.example.generator.builder.ConfigBuilder; 4 | import com.example.generator.config.DataSourceConfig; 5 | import com.example.generator.config.GlobalConfig; 6 | import com.example.generator.config.PackageConfig; 7 | import com.example.generator.config.StrategyConfig; 8 | import com.example.generator.engine.TemplateEngine; 9 | import com.example.generator.util.AssetUtil; 10 | 11 | /** 12 | * @author panzhi 13 | * @Description 14 | * @since 2021-03-11 15 | */ 16 | public class AutoGenerator { 17 | 18 | /** 19 | * 配置信息 20 | */ 21 | protected ConfigBuilder config; 22 | 23 | /** 24 | * 全局 相关配置 25 | */ 26 | private GlobalConfig globalConfig; 27 | 28 | /** 29 | * 数据源配置 30 | */ 31 | private DataSourceConfig dataSource; 32 | 33 | /** 34 | * 包相关配置 35 | */ 36 | private PackageConfig packageInfo; 37 | 38 | /** 39 | * 数据库表配置 40 | */ 41 | private StrategyConfig strategy; 42 | 43 | /** 44 | * 初始化模版引擎 45 | */ 46 | private TemplateEngine templateEngine; 47 | 48 | 49 | /** 50 | * 生成代码 51 | */ 52 | public void execute() { 53 | System.out.println("==========================准备生成文件...=========================="); 54 | validateData(); 55 | // 初始化配置 56 | if (null == config) { 57 | config = new ConfigBuilder(globalConfig, dataSource, packageInfo, strategy); 58 | } 59 | if (null == templateEngine) { 60 | templateEngine = new TemplateEngine(); 61 | } 62 | // 模板引擎初始化执行文件输出 63 | templateEngine.init(config).mkdirs().batchOutput().open(); 64 | System.out.println("==========================文件生成完成!!!=========================="); 65 | } 66 | 67 | /** 68 | * 参数验证 69 | */ 70 | private void validateData(){ 71 | //全局配置校验 72 | AssetUtil.notEmpty(globalConfig, "全局配置不能为空"); 73 | AssetUtil.notBlank(globalConfig.getOutputDir(), "全局配置【outputDir】不能为空"); 74 | AssetUtil.notBlank(globalConfig.getAuthor(), "全局配置【author】不能为空"); 75 | AssetUtil.notBlank(globalConfig.getProjectModuleCode(), "全局配置【projectModuleCode】不能为空"); 76 | //数据源配置校验 77 | AssetUtil.notEmpty(dataSource, "数据源配置不能为空"); 78 | AssetUtil.notBlank(dataSource.getUrl(), "数据源配置【URL】不能为空"); 79 | AssetUtil.notBlank(dataSource.getDriverName(), "数据源配置【driverName】不能为空"); 80 | AssetUtil.notBlank(dataSource.getUsername(), "数据源配置【userName】不能为空"); 81 | AssetUtil.notBlank(dataSource.getPassword(), "数据源配置【password】不能为空"); 82 | AssetUtil.notEmpty(dataSource.getDbType(), "数据源配置【dbType】不能为空"); 83 | //包路径配置校验 84 | AssetUtil.notEmpty(packageInfo, "包配置不能为空"); 85 | AssetUtil.notBlank(packageInfo.getParent(), "包配置【parent】不能为空"); 86 | //实体生成策略配置校验 87 | AssetUtil.notEmpty(strategy, "实体生成策略配置不能为空"); 88 | } 89 | 90 | 91 | // ================================== 相关配置 ================================== 92 | 93 | 94 | public ConfigBuilder getConfig() { 95 | return config; 96 | } 97 | 98 | public AutoGenerator setConfig(ConfigBuilder config) { 99 | this.config = config; 100 | return this; 101 | } 102 | 103 | public GlobalConfig getGlobalConfig() { 104 | return globalConfig; 105 | } 106 | 107 | public AutoGenerator setGlobalConfig(GlobalConfig globalConfig) { 108 | this.globalConfig = globalConfig; 109 | return this; 110 | } 111 | 112 | public DataSourceConfig getDataSource() { 113 | return dataSource; 114 | } 115 | 116 | public AutoGenerator setDataSource(DataSourceConfig dataSource) { 117 | this.dataSource = dataSource; 118 | return this; 119 | } 120 | 121 | public PackageConfig getPackageInfo() { 122 | return packageInfo; 123 | } 124 | 125 | public AutoGenerator setPackageInfo(PackageConfig packageInfo) { 126 | this.packageInfo = packageInfo; 127 | return this; 128 | } 129 | 130 | public StrategyConfig getStrategy() { 131 | return strategy; 132 | } 133 | 134 | public AutoGenerator setStrategy(StrategyConfig strategy) { 135 | this.strategy = strategy; 136 | return this; 137 | } 138 | 139 | public TemplateEngine getTemplateEngine() { 140 | return templateEngine; 141 | } 142 | 143 | public AutoGenerator setTemplateEngine(TemplateEngine templateEngine) { 144 | this.templateEngine = templateEngine; 145 | return this; 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/CodeGeneratorDemo.java: -------------------------------------------------------------------------------- 1 | package com.example.generator; 2 | 3 | import com.example.generator.common.dao.BaseMapper; 4 | import com.example.generator.common.request.BaseRequest; 5 | import com.example.generator.common.service.BaseService; 6 | import com.example.generator.config.DataSourceConfig; 7 | import com.example.generator.config.GlobalConfig; 8 | import com.example.generator.config.PackageConfig; 9 | import com.example.generator.config.StrategyConfig; 10 | import com.example.generator.config.converts.ITypeHandler; 11 | import com.example.generator.config.converts.apdater.MySqlTypeConvert; 12 | import com.example.generator.config.rules.IColumnType; 13 | import com.example.generator.config.type.DbType; 14 | import com.google.common.collect.ImmutableMap; 15 | 16 | import java.util.Map; 17 | 18 | /** 19 | * @author panzhi 20 | * @Description 21 | * @since 2021-03-11 22 | */ 23 | public class CodeGeneratorDemo { 24 | 25 | /** 26 | * 作者 27 | */ 28 | private static final String author = "panzhi"; 29 | /** 30 | * 项目位置 31 | */ 32 | private static final String projectPath = "/Users/panzhi/Documents/pz/coding/yjp-git/yjgj-git/yjgj-microservice-baseservice/basic-service-provider";//项目在硬盘上的基础路径 33 | /** 34 | * 生成类文件存放位置 35 | */ 36 | private static final String classOutPath = projectPath + "/src/main/java"; 37 | /** 38 | * 生成xxxMapper.xml文件存放位置 39 | */ 40 | private static final String mapperXmlPath = projectPath + "/src/main/resources/META-INF/mapping"; 41 | /** 42 | * 模块结构(模块路径) 43 | */ 44 | private static final String modulePath = "com..project"; 45 | 46 | /** 47 | * 模块名 48 | */ 49 | private static final String moduleName = "user"; 50 | 51 | /** 52 | * 指定表,支持正则批量生成 53 | */ 54 | private static final String[] tableNames = { 55 | "test_db" 56 | }; 57 | 58 | /** 59 | * 指定表和实体名称,如果两者都填写,默认优先取上面数组 60 | */ 61 | private static final Map tableMap = ImmutableMap.builder() 62 | .put("test_db", "TestEntity") 63 | .build(); 64 | 65 | public static void main(String[] args) { 66 | // 代码生成器 67 | AutoGenerator mpg = new AutoGenerator(); 68 | 69 | // 全局配置 70 | GlobalConfig gc = new GlobalConfig(); 71 | gc.setOutputDir(classOutPath); 72 | // gc.setXmlOutputDir(mapperXmlPath); 73 | gc.setAuthor(author); 74 | gc.setFileOverride(true);//是否覆盖旧文件 75 | gc.setOpen(true);//生成之后是否打开文件夹 76 | gc.setyApiEnable(false);//是否开启自动生成接口文档 77 | gc.setyApiToken("");//如果开启,必须填写接口文档token 78 | gc.setProjectModuleCode("hello"); 79 | gc.setRequestAddOrUpdateEnable(false); 80 | 81 | // 自定义文件命名,注意 %s 会自动填充表实体属性!默认可以不填写 82 | gc.setRequestAddName("%sRequestAdd"); 83 | gc.setRequestUpdateName("%sRequestUpd"); 84 | gc.setRequestName("%sRequest"); 85 | gc.setRequestSearchName("%sRequestSearch"); 86 | gc.setDtoName("%sDTO"); 87 | gc.setXmlName("%sMapper"); 88 | gc.setDaoName("%sMapper"); 89 | gc.setServiceName("%sService"); 90 | gc.setApiName("%sApi"); 91 | gc.setProviderName("%sApiImpl"); 92 | gc.setJunitName("%sJunit"); 93 | mpg.setGlobalConfig(gc); 94 | 95 | // 数据源配置 96 | DataSourceConfig dsc = new DataSourceConfig().setUrl("jdbc:mysql://127.0.0.1:3306/yjgj_base") 97 | .setDriverName("com.mysql.jdbc.Driver") 98 | .setUsername("root") 99 | .setPassword("Hello@123456") 100 | .setDbType(DbType.MYSQL) 101 | .setTypeConvert(new MySqlTypeConvert(){ 102 | 103 | @Override 104 | public IColumnType customerType(String fieldName, String fieldType) { 105 | //支持针对某个特定字段进行实体类型转换,例如数据库类型为int,实体为Date 106 | return null; 107 | } 108 | }); 109 | mpg.setDataSource(dsc); 110 | 111 | // 包路径配置,支持自定义各个包名,默认可以不填写 112 | PackageConfig pc = new PackageConfig(); 113 | pc.setParent(modulePath); 114 | pc.setModuleName(moduleName); 115 | pc.setEntity("entity"); 116 | pc.setRequest("request"); 117 | pc.setDto("dto"); 118 | pc.setXml("META-INF.mapper"); 119 | pc.setMenuJson("META-INF.config"); 120 | pc.setDao("dao"); 121 | pc.setService("service"); 122 | pc.setApi("api"); 123 | pc.setProvider("dubbo"); 124 | pc.setJunit("junit"); 125 | mpg.setPackageInfo(pc); 126 | 127 | // 实体生成策略配置 128 | StrategyConfig strategy = new StrategyConfig(); 129 | strategy.setSuperEntityClass(null); 130 | strategy.setSuperDtoClass(null); 131 | strategy.setSuperRequestClass(BaseRequest.class.getCanonicalName()); 132 | strategy.setSuperDaoClass(BaseMapper.class.getCanonicalName()); 133 | strategy.setSuperServiceClass(BaseService.class.getCanonicalName()); 134 | strategy.setTablePrefix("ts_", "tb_", "td_"); 135 | strategy.setIncludeTable(tableNames); 136 | strategy.setTableMap(tableMap); 137 | strategy.setTypeHandler(new ITypeHandler(){ 138 | 139 | @Override 140 | public boolean enable(String fieldName) { 141 | //是否开启某个字段mybatis类型适配转换 142 | return false; 143 | } 144 | 145 | @Override 146 | public String processTypeHandler(String fieldName) { 147 | //返回需要转换的类名(含包路径) 148 | return null; 149 | } 150 | }); 151 | mpg.setStrategy(strategy); 152 | mpg.execute(); 153 | } 154 | } 155 | 156 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/builder/ConfigBuilder.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.builder; 2 | 3 | import com.example.generator.config.DataSourceConfig; 4 | import com.example.generator.config.GlobalConfig; 5 | import com.example.generator.config.PackageConfig; 6 | import com.example.generator.config.StrategyConfig; 7 | import com.example.generator.config.rules.IColumnType; 8 | import com.example.generator.config.type.IDbQuery; 9 | import com.example.generator.constant.ConstValue; 10 | import com.example.generator.constant.StringPool; 11 | import com.example.generator.entity.TableField; 12 | import com.example.generator.entity.TableInfo; 13 | import com.example.generator.util.NamingStrategyHelper; 14 | import org.apache.commons.lang3.StringUtils; 15 | 16 | import java.io.File; 17 | import java.sql.Connection; 18 | import java.sql.PreparedStatement; 19 | import java.sql.ResultSet; 20 | import java.sql.SQLException; 21 | import java.util.*; 22 | import java.util.regex.Pattern; 23 | 24 | /** 25 | * @author panzhi 26 | * @Description 27 | * @since 2021-03-11 28 | */ 29 | public class ConfigBuilder { 30 | 31 | /** 32 | * SQL连接 33 | */ 34 | private Connection connection; 35 | /** 36 | * SQL语句类型 37 | */ 38 | private IDbQuery dbQuery; 39 | 40 | /** 41 | * 全局配置信息 42 | */ 43 | private GlobalConfig globalConfig; 44 | 45 | /** 46 | * 数据库配置 47 | */ 48 | private DataSourceConfig dataSourceConfig; 49 | 50 | /** 51 | * 包配置详情 52 | */ 53 | private Map packageInfo; 54 | 55 | /** 56 | * 策略配置 57 | */ 58 | private StrategyConfig strategyConfig; 59 | 60 | /** 61 | * 路径配置信息(封装文件输出路径) 62 | */ 63 | private Map pathInfo; 64 | 65 | /** 66 | * 数据库表信息(封装文件配置信息) 67 | */ 68 | private List tableInfoList; 69 | 70 | /** 71 | * 在构造器中处理配置 72 | * @param globalConfig 全局配置 73 | * @param dataSourceConfig 数据源配置 74 | * @param packageConfig 包配置 75 | * @param strategyConfig 表配置 76 | */ 77 | public ConfigBuilder(GlobalConfig globalConfig, DataSourceConfig dataSourceConfig, PackageConfig packageConfig, StrategyConfig strategyConfig) { 78 | this.globalConfig = (Objects.isNull(globalConfig) ? new GlobalConfig() : globalConfig); 79 | this.dataSourceConfig = (Objects.isNull(dataSourceConfig) ? new DataSourceConfig() : dataSourceConfig); 80 | this.strategyConfig = (Objects.isNull(strategyConfig) ? new StrategyConfig() : strategyConfig); 81 | // 包路径配置 82 | handlerPackage(globalConfig.getOutputDir(), globalConfig.getXmlOutputDir(), (Objects.isNull(packageConfig) ? new PackageConfig() : packageConfig)); 83 | // 数据源配置 84 | handlerDataSource(dataSourceConfig); 85 | // 表结构生成实体配置 86 | handlerStrategy(strategyConfig); 87 | } 88 | 89 | 90 | /** 91 | * 处理包配置 92 | * @param outputDir 93 | * @param xmlOutputDir 94 | * @param config 95 | */ 96 | private void handlerPackage(String outputDir, String xmlOutputDir, PackageConfig config) { 97 | // 包信息 98 | packageInfo = new HashMap<>(); 99 | packageInfo.put(ConstValue.PACKAGE_ENTITY, joinPackage(config.getParent(), config.getEntity())); 100 | packageInfo.put(ConstValue.PACKAGE_REQUEST, joinPackage(config.getParent(), config.getRequest())); 101 | packageInfo.put(ConstValue.PACKAGE_DTO, joinPackage(config.getParent(), config.getDto())); 102 | packageInfo.put(ConstValue.PACKAGE_DAO, joinPackage(config.getParent(), config.getDao())); 103 | packageInfo.put(ConstValue.PACKAGE_SERVICE, joinPackage(config.getParent(), config.getService())); 104 | packageInfo.put(ConstValue.PACKAGE_API, joinPackage(config.getParent(), config.getApi())); 105 | packageInfo.put(ConstValue.PACKAGE_PROVIDER, joinPackage(config.getParent(), config.getProvider())); 106 | packageInfo.put(ConstValue.PACKAGE_JUNIT, joinPackage(config.getParent(), config.getJunit())); 107 | packageInfo.put(ConstValue.PACKAGE_XML, joinPackage(config.getParent(), config.getXml())); 108 | packageInfo.put(ConstValue.PACKAGE_MENU_JSON, joinPackage(config.getParent(), config.getMenuJson())); 109 | 110 | // 生成路径信息 111 | pathInfo = new HashMap<>(6); 112 | pathInfo.put(ConstValue.ENTITY_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_ENTITY))); 113 | pathInfo.put(ConstValue.REQUEST_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_REQUEST))); 114 | pathInfo.put(ConstValue.DTO_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_DTO))); 115 | pathInfo.put(ConstValue.DAO_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_DAO))); 116 | pathInfo.put(ConstValue.SERVICE_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_SERVICE))); 117 | pathInfo.put(ConstValue.API_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_API))); 118 | pathInfo.put(ConstValue.PROVIDER_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_PROVIDER))); 119 | pathInfo.put(ConstValue.JUNIT_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_JUNIT))); 120 | if(StringUtils.isNotBlank(xmlOutputDir)){ 121 | pathInfo.put(ConstValue.XML_PATH, xmlOutputDir); 122 | pathInfo.put(ConstValue.MENU_PATH, xmlOutputDir); 123 | } else { 124 | pathInfo.put(ConstValue.XML_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_XML))); 125 | pathInfo.put(ConstValue.MENU_PATH, joinPath(outputDir, packageInfo.get(ConstValue.PACKAGE_MENU_JSON))); 126 | } 127 | } 128 | 129 | /** 130 | * 处理数据源配置 131 | * @param config 132 | */ 133 | private void handlerDataSource(DataSourceConfig config) { 134 | connection = config.getConn(); 135 | dbQuery = config.getDbQuery(); 136 | try { 137 | PreparedStatement preparedStatement = connection.prepareStatement(dbQuery.dataBase()); 138 | ResultSet results = preparedStatement.executeQuery(); 139 | while (results.next()) { 140 | dataSourceConfig.setDataBaseName(results.getString(1)); 141 | } 142 | preparedStatement.close(); 143 | } catch (Exception e){ 144 | e.printStackTrace(); 145 | } 146 | } 147 | 148 | /** 149 | * 处理数据库表 加载数据库表、列、注释相关数据集 150 | * @param config 151 | */ 152 | private void handlerStrategy(StrategyConfig config) { 153 | String[] includeTables = config.getIncludeTable(); 154 | Map tableMap = config.getTableMap(); 155 | if((Objects.isNull(includeTables)) && Objects.isNull(tableMap)){ 156 | throw new RuntimeException("需要生成的表配置项不能为空!"); 157 | } 158 | //需要反向生成的表信息 159 | List includeTableList = new ArrayList<>(); 160 | try { 161 | PreparedStatement preparedStatement = connection.prepareStatement(dbQuery.tablesSql()); 162 | ResultSet results = preparedStatement.executeQuery(); 163 | while (results.next()) { 164 | String tableName = results.getString(dbQuery.tableName()); 165 | if (StringUtils.isNotEmpty(tableName)) { 166 | String tableComment = results.getString(dbQuery.tableComment()); 167 | if (config.isSkipView() && "VIEW".equals(tableComment)) { 168 | // 跳过视图 169 | continue; 170 | } 171 | TableInfo tableInfo = new TableInfo(); 172 | tableInfo.setName(tableName); 173 | tableInfo.setComment(tableComment); 174 | if (includeTables != null && includeTables.length > 0) { 175 | for (String includeTable : includeTables) { 176 | // 忽略大小写等于 或 正则 true 177 | if (tableNameMatches(includeTable, tableName)) { 178 | includeTableList.add(tableInfo); 179 | } 180 | } 181 | } else if (Objects.nonNull(tableMap)) { 182 | if(tableMap.containsKey(tableName)){ 183 | tableInfo.setEntityName(tableMap.get(tableName)); 184 | includeTableList.add(tableInfo); 185 | } 186 | } 187 | } else { 188 | System.err.println("当前数据库为空!!!"); 189 | } 190 | } 191 | includeTableList.forEach(ti -> convertTableFields(ti)); 192 | preparedStatement.close(); 193 | } catch (SQLException e) { 194 | e.printStackTrace(); 195 | } finally { 196 | // 释放资源 197 | try { 198 | if (connection != null) { 199 | connection.close(); 200 | } 201 | } catch (SQLException e) { 202 | e.printStackTrace(); 203 | } 204 | } 205 | tableInfoList = processTable(includeTableList, config); 206 | } 207 | 208 | 209 | /** 210 | * 将字段信息与表信息关联 211 | * @param tableInfo 212 | * @return 213 | */ 214 | private TableInfo convertTableFields(TableInfo tableInfo) { 215 | List fieldList = new ArrayList<>(); 216 | try { 217 | String tableFieldsSql = String.format(dbQuery.tableFieldsSql(), tableInfo.getName(), dataSourceConfig.getDataBaseName()); 218 | PreparedStatement preparedStatement = connection.prepareStatement(tableFieldsSql); 219 | ResultSet results = preparedStatement.executeQuery(); 220 | while (results.next()) { 221 | TableField field = new TableField(); 222 | // 获取表字段信息 223 | field.setFieldName(results.getString(dbQuery.fieldName())); 224 | field.setFieldType(results.getString(dbQuery.fieldType())); 225 | field.setFieldLength(Integer.valueOf(results.getInt(dbQuery.fieldLength())).intValue()); 226 | field.setComment(results.getString(dbQuery.fieldComment())); 227 | String key = results.getString(dbQuery.fieldKey()); 228 | // 获取主键信息 229 | boolean isId = StringUtils.isNotEmpty(key) && "PRI".equals(key.toUpperCase()); 230 | if(isId){ 231 | tableInfo.setPrimaryId(field.getFieldName()); 232 | } 233 | //类型转换 234 | field.setFieldType(dataSourceConfig.getTypeConvert().convertJdbcType(field.getFieldName(), field.getFieldType())); 235 | field.setPropertyName(processName(field.getFieldName(), null)); 236 | IColumnType columnType = dataSourceConfig.getTypeConvert().convertPropertyType(field.getFieldName(), field.getFieldType()); 237 | field.setPropertyType(columnType.getType()); 238 | field.setPropertyTypePackage(columnType.getPkg()); 239 | boolean isTypeHandler = strategyConfig.getTypeHandler().enable(field.getFieldName()); 240 | field.setIsTypeHandler(isTypeHandler); 241 | if(isTypeHandler){ 242 | String typeHandlerClass = strategyConfig.getTypeHandler().processTypeHandler(field.getFieldName()); 243 | field.setTypeHandler(typeHandlerClass); 244 | } 245 | fieldList.add(field); 246 | } 247 | preparedStatement.close(); 248 | } catch (SQLException e) { 249 | System.err.println("SQL Exception:" + e.getMessage()); 250 | } 251 | tableInfo.setFields(fieldList); 252 | return tableInfo; 253 | } 254 | 255 | 256 | /** 257 | * 处理表对应的类名称 258 | * @param tableList 259 | * @param config 260 | * @return 261 | */ 262 | private List processTable(List tableList, StrategyConfig config) { 263 | String[] tablePrefix = config.getTablePrefix(); 264 | for (TableInfo tableInfo : tableList) { 265 | String entityName = (StringUtils.isNotBlank(tableInfo.getEntityName()) ? tableInfo.getEntityName() : NamingStrategyHelper.capitalFirstUpper(processName(tableInfo.getName(), tablePrefix))); 266 | if (StringUtils.isNotBlank(globalConfig.getEntityName())) { 267 | tableInfo.setEntityName(String.format(globalConfig.getEntityName(), entityName)); 268 | } else { 269 | tableInfo.setEntityName(entityName); 270 | } 271 | if (StringUtils.isNotBlank(globalConfig.getRequestName())) { 272 | tableInfo.setRequestName(String.format(globalConfig.getRequestName(), entityName)); 273 | } else { 274 | tableInfo.setRequestName(entityName + ConstValue.GLOBAL_REQUEST); 275 | } 276 | if (StringUtils.isNotBlank(globalConfig.getRequestAddName())) { 277 | tableInfo.setRequestAddName(String.format(globalConfig.getRequestAddName(), entityName)); 278 | } else { 279 | tableInfo.setRequestAddName(entityName + ConstValue.GLOBAL_REQUEST_ADD); 280 | } 281 | if (StringUtils.isNotBlank(globalConfig.getRequestUpdateName())) { 282 | tableInfo.setRequestUpdateName(String.format(globalConfig.getRequestUpdateName(), entityName)); 283 | } else { 284 | tableInfo.setRequestUpdateName(entityName + ConstValue.GLOBAL_REQUEST_UPDATE); 285 | } 286 | if (StringUtils.isNotBlank(globalConfig.getRequestSearchName())) { 287 | tableInfo.setRequestSearchName(String.format(globalConfig.getRequestSearchName(), entityName)); 288 | } else { 289 | tableInfo.setRequestSearchName(entityName + ConstValue.GLOBAL_REQUEST_SEARCH); 290 | } 291 | if (StringUtils.isNotBlank(globalConfig.getDtoName())) { 292 | tableInfo.setDtoName(String.format(globalConfig.getDtoName(), entityName)); 293 | } else { 294 | tableInfo.setDtoName(entityName + ConstValue.GLOBAL_DTO); 295 | } 296 | if (StringUtils.isNotBlank(globalConfig.getXmlName())) { 297 | tableInfo.setXmlName(String.format(globalConfig.getXmlName(), entityName)); 298 | } else { 299 | tableInfo.setXmlName(entityName + ConstValue.GLOBAL_XML); 300 | } 301 | if (StringUtils.isNotBlank(globalConfig.getDaoName())) { 302 | tableInfo.setDaoName(String.format(globalConfig.getDaoName(), entityName)); 303 | } else { 304 | tableInfo.setDaoName(entityName + ConstValue.GLOBAL_DAO); 305 | } 306 | if (StringUtils.isNotBlank(globalConfig.getServiceName())) { 307 | tableInfo.setServiceName(String.format(globalConfig.getServiceName(), entityName)); 308 | } else { 309 | tableInfo.setServiceName(entityName + ConstValue.GLOBAL_SERVICE); 310 | } 311 | if (StringUtils.isNotBlank(globalConfig.getApiName())) { 312 | tableInfo.setApiName(String.format(globalConfig.getApiName(), entityName)); 313 | } else { 314 | tableInfo.setApiName(entityName + ConstValue.GLOBAL_API); 315 | } 316 | if (StringUtils.isNotBlank(globalConfig.getProviderName())) { 317 | tableInfo.setProviderName(String.format(globalConfig.getProviderName(), entityName)); 318 | } else { 319 | tableInfo.setProviderName(entityName + ConstValue.GLOBAL_PROVIDER); 320 | } 321 | if (StringUtils.isNotBlank(globalConfig.getJunitName())) { 322 | tableInfo.setJunitName(String.format(globalConfig.getJunitName(), entityName)); 323 | } else { 324 | tableInfo.setJunitName(entityName + ConstValue.GLOBAL_JUNIT); 325 | } 326 | } 327 | return tableList; 328 | } 329 | 330 | /** 331 | * 处理表/字段名称 332 | * @param name 333 | * @param prefix 334 | * @return 335 | */ 336 | private String processName(String name, String[] prefix) { 337 | boolean removePrefix = false; 338 | if (prefix != null && prefix.length >= 1) { 339 | removePrefix = true; 340 | } 341 | String propertyName; 342 | if (removePrefix) { 343 | propertyName = NamingStrategyHelper.removePrefixAndCamel(name, prefix); 344 | } else { 345 | // 下划线转驼峰 346 | propertyName = NamingStrategyHelper.underlineToCamel(name); 347 | } 348 | return propertyName; 349 | } 350 | 351 | /** 352 | *

353 | * 表名匹配 354 | *

355 | * 356 | * @param setTableName 设置表名 357 | * @param dbTableName 数据库表单 358 | * @return 359 | */ 360 | private boolean tableNameMatches(String setTableName, String dbTableName) { 361 | return setTableName.equals(dbTableName) 362 | || matches(setTableName, dbTableName); 363 | } 364 | 365 | /** 366 | *

367 | * 连接父子包名 368 | *

369 | * 370 | * @param parent 父包名 371 | * @param subPackage 子包名 372 | * @return 连接后的包名 373 | */ 374 | private String joinPackage(String parent, String subPackage) { 375 | if (StringUtils.isEmpty(parent)) { 376 | return subPackage; 377 | } 378 | return parent + StringPool.DOT + subPackage; 379 | } 380 | 381 | 382 | /** 383 | * 连接路径字符串 384 | * @param parentDir 385 | * @param packageName 386 | * @return 387 | */ 388 | private String joinPath(String parentDir, String packageName) { 389 | if (StringUtils.isBlank(parentDir)) { 390 | parentDir = System.getProperty(ConstValue.JAVA_TMPDIR); 391 | } 392 | if (!StringUtils.endsWith(parentDir, File.separator)) { 393 | parentDir += File.separator; 394 | } 395 | packageName = packageName.replaceAll("\\.", StringPool.BACK_SLASH + File.separator); 396 | return parentDir + packageName; 397 | } 398 | 399 | /** 400 | * 正则表达式匹配 401 | * @param regex 402 | * @param input 403 | * @return 404 | */ 405 | private boolean matches(String regex, String input) { 406 | if (null == regex || null == input) { 407 | return false; 408 | } 409 | return Pattern.matches(regex, input); 410 | } 411 | 412 | // ================================== 相关配置 ================================== 413 | 414 | 415 | public Connection getConnection() { 416 | return connection; 417 | } 418 | 419 | public ConfigBuilder setConnection(Connection connection) { 420 | this.connection = connection; 421 | return this; 422 | } 423 | 424 | public IDbQuery getDbQuery() { 425 | return dbQuery; 426 | } 427 | 428 | public ConfigBuilder setDbQuery(IDbQuery dbQuery) { 429 | this.dbQuery = dbQuery; 430 | return this; 431 | } 432 | 433 | public GlobalConfig getGlobalConfig() { 434 | return globalConfig; 435 | } 436 | 437 | public ConfigBuilder setGlobalConfig(GlobalConfig globalConfig) { 438 | this.globalConfig = globalConfig; 439 | return this; 440 | } 441 | 442 | public DataSourceConfig getDataSourceConfig() { 443 | return dataSourceConfig; 444 | } 445 | 446 | public ConfigBuilder setDataSourceConfig(DataSourceConfig dataSourceConfig) { 447 | this.dataSourceConfig = dataSourceConfig; 448 | return this; 449 | } 450 | 451 | public Map getPackageInfo() { 452 | return packageInfo; 453 | } 454 | 455 | public ConfigBuilder setPackageInfo(Map packageInfo) { 456 | this.packageInfo = packageInfo; 457 | return this; 458 | } 459 | 460 | public StrategyConfig getStrategyConfig() { 461 | return strategyConfig; 462 | } 463 | 464 | public ConfigBuilder setStrategyConfig(StrategyConfig strategyConfig) { 465 | this.strategyConfig = strategyConfig; 466 | return this; 467 | } 468 | 469 | public Map getPathInfo() { 470 | return pathInfo; 471 | } 472 | 473 | public ConfigBuilder setPathInfo(Map pathInfo) { 474 | this.pathInfo = pathInfo; 475 | return this; 476 | } 477 | 478 | public List getTableInfoList() { 479 | return tableInfoList; 480 | } 481 | 482 | public ConfigBuilder setTableInfoList(List tableInfoList) { 483 | this.tableInfoList = tableInfoList; 484 | return this; 485 | } 486 | } 487 | 488 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/common/dao/BaseMapper.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.common.dao; 2 | 3 | import org.apache.ibatis.annotations.Param; 4 | 5 | import java.io.Serializable; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | /** 10 | * @author panzhi 11 | * @Description 12 | * @since 2021-03-11 13 | */ 14 | public interface BaseMapper { 15 | 16 | /** 17 | * 批量插入 18 | * @param list 19 | * @return 20 | */ 21 | int insertList(@Param("list") List list); 22 | 23 | /** 24 | * 按需插入一条记录 25 | * @param entity 26 | * @return 27 | */ 28 | int insertPrimaryKeySelective(T entity); 29 | 30 | /** 31 | * 按需修改一条记录(通过主键ID) 32 | * @return 33 | */ 34 | int updatePrimaryKeySelective(T entity); 35 | 36 | /** 37 | * 批量按需修改记录(通过主键ID) 38 | * @param list 39 | * @return 40 | */ 41 | int updateBatchByIds(@Param("list") List list); 42 | 43 | /** 44 | * 根据ID删除 45 | * @param id 主键ID 46 | * @return 47 | */ 48 | int deleteByPrimaryKey(@Param("id") Serializable id); 49 | 50 | /** 51 | * 根据ID查询 52 | * @param id 主键ID 53 | * @return 54 | */ 55 | T selectByPrimaryKey(@Param("id") Serializable id); 56 | 57 | /** 58 | * 按需查询 59 | * @param entity 60 | * @return 61 | */ 62 | List selectByPrimaryKeySelective(T entity); 63 | 64 | /** 65 | * 批量查询 66 | * @param ids 主键ID集合 67 | * @return 68 | */ 69 | List selectByIds(@Param("ids") List ids); 70 | 71 | /** 72 | * 查询(根据 columnMap 条件) 73 | * @param columnMap 表字段 map 对象 74 | * @return 75 | */ 76 | List selectByMap(Map columnMap); 77 | } 78 | 79 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/common/request/BaseRequest.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.common.request; 2 | 3 | import com.example.generator.common.response.PageUtils; 4 | 5 | import javax.validation.constraints.Min; 6 | import java.io.Serializable; 7 | 8 | /** 9 | * @author panzhi 10 | * @Description 11 | * @since 2021-03-11 12 | */ 13 | public class BaseRequest implements Serializable { 14 | 15 | public static final String notBlankMsg = "不能为空!"; 16 | 17 | /** 18 | * 提交的token 19 | */ 20 | private String token; 21 | 22 | private String submitToken; 23 | 24 | @Min(value = 0, message = "页码必须大于等于0的整数") 25 | private Integer page; 26 | 27 | @Min(value = 0, message = "pageSize必须大于等于0的整数") 28 | private Integer pageSize; 29 | 30 | private Integer start; 31 | 32 | private Integer end; 33 | 34 | private String loginUserId; 35 | 36 | private String loginUserName; 37 | 38 | private String secretToken; 39 | 40 | private String ip; 41 | 42 | private String loginMobile; 43 | 44 | private String userAgent; 45 | 46 | private String referer; 47 | 48 | private String remoteHost; 49 | 50 | /** 51 | * 是否开启分页 52 | */ 53 | private boolean pageEnable = true; 54 | 55 | public Integer getPage() { 56 | if (this.page == null){ 57 | this.page = 1; 58 | } 59 | return page; 60 | } 61 | 62 | public Integer getStart() { 63 | if (this.page != null && this.page > 0) { 64 | start = (page - 1) * getPageSize(); 65 | return start; 66 | } 67 | return start == null ? 0 : start; 68 | } 69 | 70 | public Integer getEnd() { 71 | return getPageSize(); 72 | } 73 | 74 | public Integer getPageSize() { 75 | if (pageSize == null || pageSize == 0) { 76 | return PageUtils.pageSizeInit; 77 | } 78 | return pageSize; 79 | } 80 | 81 | public String getLoginUserId() { 82 | return loginUserId == null ? "" : loginUserId; 83 | } 84 | 85 | public String getLoginUserName() { 86 | return loginUserName == null ? "" : loginUserName; 87 | } 88 | 89 | public String getToken() { 90 | return token; 91 | } 92 | 93 | public void setToken(String token) { 94 | this.token = token; 95 | } 96 | 97 | public String getSubmitToken() { 98 | return submitToken; 99 | } 100 | 101 | public void setSubmitToken(String submitToken) { 102 | this.submitToken = submitToken; 103 | } 104 | 105 | public void setPage(Integer page) { 106 | this.page = page; 107 | } 108 | 109 | public void setPageSize(Integer pageSize) { 110 | this.pageSize = pageSize; 111 | } 112 | 113 | public void setStart(Integer start) { 114 | this.start = start; 115 | } 116 | 117 | public void setEnd(Integer end) { 118 | this.end = end; 119 | } 120 | 121 | public void setLoginUserId(String loginUserId) { 122 | this.loginUserId = loginUserId; 123 | } 124 | 125 | public void setLoginUserName(String loginUserName) { 126 | this.loginUserName = loginUserName; 127 | } 128 | 129 | public String getSecretToken() { 130 | return secretToken; 131 | } 132 | 133 | public void setSecretToken(String secretToken) { 134 | this.secretToken = secretToken; 135 | } 136 | 137 | public String getIp() { 138 | return ip; 139 | } 140 | 141 | public void setIp(String ip) { 142 | this.ip = ip; 143 | } 144 | 145 | public String getLoginMobile() { 146 | return loginMobile; 147 | } 148 | 149 | public void setLoginMobile(String loginMobile) { 150 | this.loginMobile = loginMobile; 151 | } 152 | 153 | public String getUserAgent() { 154 | return userAgent; 155 | } 156 | 157 | public void setUserAgent(String userAgent) { 158 | this.userAgent = userAgent; 159 | } 160 | 161 | public String getReferer() { 162 | return referer; 163 | } 164 | 165 | public void setReferer(String referer) { 166 | this.referer = referer; 167 | } 168 | 169 | public String getRemoteHost() { 170 | return remoteHost; 171 | } 172 | 173 | public void setRemoteHost(String remoteHost) { 174 | this.remoteHost = remoteHost; 175 | } 176 | 177 | public boolean getPageEnable() { 178 | return pageEnable; 179 | } 180 | 181 | public void setPageEnable(boolean pageEnable) { 182 | this.pageEnable = pageEnable; 183 | } 184 | } 185 | 186 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/common/response/PageUtils.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.common.response; 2 | 3 | import com.example.generator.common.request.BaseRequest; 4 | 5 | import java.io.Serializable; 6 | import java.util.List; 7 | 8 | /** 9 | * @author panzhi 10 | * @Description 11 | * @since 2021-03-11 12 | */ 13 | public class PageUtils implements Serializable { 14 | private static final long serialVersionUID = 1L; 15 | 16 | public static final Integer pageSizeInit = 20; 17 | 18 | /** 19 | * 总记录数 20 | */ 21 | private int totalCount; 22 | /** 23 | * 每页记录数 24 | */ 25 | private int pageSize; 26 | /** 27 | * 总页数 28 | */ 29 | private int totalPage; 30 | /** 31 | * 当前页数 32 | */ 33 | private int currPage; 34 | /** 35 | * 列表数据 36 | */ 37 | private List list; 38 | 39 | /** 40 | * 分页 41 | * 42 | * @param list 列表数据 43 | * @param totalCount 总记录数 44 | * @param pageSize 每页记录数 45 | * @param currPage 当前页数 46 | */ 47 | public PageUtils(List list, int totalCount, int pageSize, int currPage) { 48 | this.list = list; 49 | this.totalCount = totalCount; 50 | this.pageSize = pageSize; 51 | this.currPage = currPage; 52 | this.totalPage = (int) Math.ceil((double) totalCount / pageSize); 53 | } 54 | 55 | 56 | /** 57 | * @param list 列表数据 58 | * @param totalCount 总记录数 59 | * @param request BaseRequest 60 | */ 61 | public PageUtils(List list, int totalCount, BaseRequest request) { 62 | this.list = list; 63 | this.totalCount = totalCount; 64 | this.pageSize = request.getPageSize(); 65 | this.currPage = request.getPage(); 66 | this.totalPage = (int) Math.ceil((double) totalCount / pageSize); 67 | } 68 | 69 | 70 | /** 71 | * 默认构造器 72 | */ 73 | public PageUtils() { 74 | this.pageSize = pageSizeInit; 75 | this.currPage = 1; 76 | } 77 | 78 | /** 79 | * 自定义构造器 80 | */ 81 | public PageUtils(int pageSize, int currPage) { 82 | this.pageSize = pageSize; 83 | this.currPage = currPage; 84 | } 85 | 86 | 87 | public int getTotalCount() { 88 | return totalCount; 89 | } 90 | 91 | public void setTotalCount(int totalCount) { 92 | this.totalCount = totalCount; 93 | } 94 | 95 | public int getPageSize() { 96 | return pageSize; 97 | } 98 | 99 | public void setPageSize(int pageSize) { 100 | this.pageSize = pageSize; 101 | } 102 | 103 | public int getTotalPage() { 104 | return totalPage; 105 | } 106 | 107 | public void setTotalPage(int totalPage) { 108 | this.totalPage = totalPage; 109 | } 110 | 111 | public int getCurrPage() { 112 | return currPage; 113 | } 114 | 115 | public void setCurrPage(int currPage) { 116 | this.currPage = currPage; 117 | } 118 | 119 | public List getList() { 120 | return list; 121 | } 122 | 123 | public void setList(List list) { 124 | this.list = list; 125 | } 126 | 127 | } 128 | 129 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/common/service/BaseService.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.common.service; 2 | 3 | import com.example.generator.common.dao.BaseMapper; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import org.springframework.transaction.annotation.Transactional; 6 | 7 | import java.io.Serializable; 8 | import java.util.List; 9 | import java.util.Map; 10 | 11 | /** 12 | * @author panzhi 13 | * @Description 14 | * @since 2021-03-11 15 | */ 16 | public abstract class BaseService, T> { 17 | 18 | @Autowired 19 | protected M baseMapper; 20 | 21 | /** 22 | * 新增 23 | * @param entity 24 | * @return boolean 25 | */ 26 | @Transactional(rollbackFor = {Exception.class}) 27 | public boolean insert(T entity){ 28 | return returnBool(baseMapper.insertPrimaryKeySelective(entity)); 29 | } 30 | 31 | /** 32 | * 批量新增 33 | * @param list 34 | * @return boolean 35 | */ 36 | @Transactional(rollbackFor = {Exception.class}) 37 | public boolean insertList(List list){ 38 | return returnBool(baseMapper.insertList(list)); 39 | } 40 | 41 | /** 42 | * 通过ID修改记录(如果想全部更新,只需保证字段都不为NULL) 43 | * @param entity 44 | * @return boolean 45 | */ 46 | @Transactional(rollbackFor = {Exception.class}) 47 | public boolean updateById(T entity){ 48 | return returnBool(baseMapper.updatePrimaryKeySelective(entity)); 49 | } 50 | 51 | /** 52 | * 通过ID批量修改记录(如果想全部更新,只需保证字段都不为NULL) 53 | * @param list 54 | * @return boolean 55 | */ 56 | @Transactional(rollbackFor = {Exception.class}) 57 | public boolean updateBatchByIds(List list){ 58 | return returnBool(baseMapper.updateBatchByIds(list)); 59 | } 60 | 61 | /** 62 | * 根据ID删除 63 | * @param id 主键ID 64 | * @return boolean 65 | */ 66 | @Transactional(rollbackFor = {Exception.class}) 67 | public boolean deleteById(Serializable id){ 68 | return returnBool(baseMapper.deleteByPrimaryKey(id)); 69 | } 70 | 71 | /** 72 | * 根据ID查询 73 | * @param id 主键ID 74 | * @return 75 | */ 76 | public T selectById(Serializable id){ 77 | return baseMapper.selectByPrimaryKey(id); 78 | } 79 | 80 | /** 81 | * 按需查询 82 | * @param entity 83 | * @return 84 | */ 85 | public List selectByPrimaryKeySelective(T entity){ 86 | return baseMapper.selectByPrimaryKeySelective(entity); 87 | } 88 | 89 | /** 90 | * 批量查询 91 | * @param ids 92 | * @return 93 | */ 94 | public List selectByIds(List ids){ 95 | return baseMapper.selectByIds(ids); 96 | } 97 | 98 | /** 99 | * 根据条件查询 100 | * @param columnMap 101 | * @return 102 | */ 103 | public List selectByMap(Map columnMap){ 104 | return baseMapper.selectByMap(columnMap); 105 | } 106 | 107 | /** 108 | * 判断数据库操作是否成功 109 | * @param result 数据库操作返回影响条数 110 | * @return boolean 111 | */ 112 | protected boolean returnBool(Integer result) { 113 | return null != result && result >= 1; 114 | } 115 | 116 | } 117 | 118 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/DataSourceConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config; 2 | 3 | import com.example.generator.config.converts.ITypeConvert; 4 | import com.example.generator.config.converts.apdater.MySqlTypeConvert; 5 | import com.example.generator.config.type.DbType; 6 | import com.example.generator.config.type.IDbQuery; 7 | import com.example.generator.config.type.querys.MySqlQuery; 8 | 9 | import java.sql.Connection; 10 | import java.sql.DriverManager; 11 | import java.sql.SQLException; 12 | 13 | /** 14 | * @author panzhi 15 | * @Description 16 | * @since 2021-03-11 17 | */ 18 | public class DataSourceConfig { 19 | 20 | /** 21 | * 数据库类型 22 | */ 23 | private DbType dbType; 24 | 25 | /** 26 | * 数据库信息查询 27 | */ 28 | private IDbQuery dbQuery; 29 | 30 | /** 31 | * 驱动连接的URL 32 | */ 33 | private String url; 34 | 35 | /** 36 | * 数据库名称 37 | */ 38 | private String dataBaseName; 39 | /** 40 | * 驱动名称 41 | */ 42 | private String driverName; 43 | /** 44 | * 数据库连接用户名 45 | */ 46 | private String username; 47 | /** 48 | * 数据库连接密码 49 | */ 50 | private String password; 51 | 52 | /** 53 | * 字段类型转换 54 | */ 55 | private ITypeConvert typeConvert; 56 | 57 | 58 | 59 | public DataSourceConfig setDbType(DbType dbType) { 60 | this.dbType = dbType; 61 | return this; 62 | } 63 | 64 | public DataSourceConfig setDbQuery(IDbQuery dbQuery) { 65 | this.dbQuery = dbQuery; 66 | return this; 67 | } 68 | 69 | public String getUrl() { 70 | return url; 71 | } 72 | 73 | public DataSourceConfig setUrl(String url) { 74 | this.url = url; 75 | return this; 76 | } 77 | 78 | public String getDriverName() { 79 | return driverName; 80 | } 81 | 82 | public DataSourceConfig setDriverName(String driverName) { 83 | this.driverName = driverName; 84 | return this; 85 | } 86 | 87 | public String getUsername() { 88 | return username; 89 | } 90 | 91 | public DataSourceConfig setUsername(String username) { 92 | this.username = username; 93 | return this; 94 | } 95 | 96 | public String getPassword() { 97 | return password; 98 | } 99 | 100 | public DataSourceConfig setPassword(String password) { 101 | this.password = password; 102 | return this; 103 | } 104 | 105 | public DataSourceConfig setTypeConvert(ITypeConvert typeConvert) { 106 | this.typeConvert = typeConvert; 107 | return this; 108 | } 109 | 110 | public String getDataBaseName() { 111 | return dataBaseName; 112 | } 113 | 114 | public DataSourceConfig setDataBaseName(String dataBaseName) { 115 | this.dataBaseName = dataBaseName; 116 | return this; 117 | } 118 | 119 | /** 120 | * 判断数据库类型 121 | * @return 122 | */ 123 | public DbType getDbType() { 124 | if (null == dbType) { 125 | if (driverName.contains("mysql")) { 126 | dbType = DbType.MYSQL; 127 | } else if (driverName.contains("oracle")) { 128 | dbType = DbType.ORACLE; 129 | } else { 130 | throw new RuntimeException("Unknown type of database!"); 131 | } 132 | } 133 | return dbType; 134 | } 135 | 136 | /** 137 | * 获取数据库查询SQL 138 | * @return 139 | */ 140 | public IDbQuery getDbQuery() { 141 | if(null == dbQuery){ 142 | switch (getDbType()) { 143 | case ORACLE: 144 | //预留 145 | break; 146 | default: 147 | // 默认 MYSQL 148 | dbQuery = new MySqlQuery(); 149 | break; 150 | } 151 | } 152 | return dbQuery; 153 | } 154 | 155 | /** 156 | * 获取字段类型转换器 157 | * @return 158 | */ 159 | public ITypeConvert getTypeConvert() { 160 | if (null == typeConvert) { 161 | switch (getDbType()) { 162 | case ORACLE: 163 | //预留 164 | break; 165 | default: 166 | // 默认 MYSQL 167 | typeConvert = new MySqlTypeConvert(); 168 | break; 169 | } 170 | } 171 | return typeConvert; 172 | } 173 | 174 | /** 175 | * 创建数据库连接对象 176 | * 177 | * @return Connection 178 | */ 179 | public Connection getConn() { 180 | Connection conn = null; 181 | try { 182 | Class.forName(driverName); 183 | conn = DriverManager.getConnection(url, username, password); 184 | } catch (ClassNotFoundException | SQLException e) { 185 | e.printStackTrace(); 186 | } 187 | return conn; 188 | } 189 | } 190 | 191 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/GlobalConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public class GlobalConfig { 9 | 10 | /** 11 | * 生成类文件存放位置 12 | */ 13 | private String outputDir; 14 | 15 | /** 16 | * 生成xxxMapper.xml文件存放位置,默认会输出到META-INF/mapper 17 | */ 18 | private String xmlOutputDir; 19 | 20 | /** 21 | * 是否覆盖源文件 22 | */ 23 | private boolean fileOverride = true; 24 | 25 | /** 26 | * 是否打开输出目录 27 | */ 28 | private boolean open = true; 29 | 30 | /** 31 | * 开发人员 32 | */ 33 | private String author; 34 | 35 | /** 36 | * 是否开启文档生成器 37 | */ 38 | private boolean yApiEnable = false; 39 | 40 | /** 41 | * 开启文档生成器之后,需要指定token 42 | */ 43 | private String yApiToken; 44 | 45 | /** 46 | * 请求实体,add和update是否分开 47 | */ 48 | private boolean requestAddOrUpdateEnable = true; 49 | 50 | /** 51 | * 项目编码 52 | */ 53 | private String projectModuleCode; 54 | 55 | /** 56 | * 各层文件名称方式,例如: %sAction 生成 UserAction 57 | * %s 为占位符 58 | */ 59 | private String entityName; 60 | 61 | private String requestAddName; 62 | 63 | private String requestUpdateName; 64 | 65 | private String requestName; 66 | 67 | private String requestSearchName; 68 | 69 | private String dtoName; 70 | 71 | private String xmlName; 72 | 73 | private String daoName; 74 | 75 | private String serviceName; 76 | 77 | private String apiName; 78 | 79 | private String providerName; 80 | 81 | private String junitName; 82 | 83 | public String getOutputDir() { 84 | return outputDir; 85 | } 86 | 87 | public GlobalConfig setOutputDir(String outputDir) { 88 | this.outputDir = outputDir; 89 | return this; 90 | } 91 | 92 | public String getXmlOutputDir() { 93 | return xmlOutputDir; 94 | } 95 | 96 | public GlobalConfig setXmlOutputDir(String xmlOutputDir) { 97 | this.xmlOutputDir = xmlOutputDir; 98 | return this; 99 | } 100 | 101 | public String getAuthor() { 102 | return author; 103 | } 104 | 105 | public GlobalConfig setAuthor(String author) { 106 | this.author = author; 107 | return this; 108 | } 109 | 110 | public boolean isyApiEnable() { 111 | return yApiEnable; 112 | } 113 | 114 | public GlobalConfig setyApiEnable(boolean yApiEnable) { 115 | this.yApiEnable = yApiEnable; 116 | return this; 117 | } 118 | 119 | public String getyApiToken() { 120 | return yApiToken; 121 | } 122 | 123 | public GlobalConfig setyApiToken(String yApiToken) { 124 | this.yApiToken = yApiToken; 125 | return this; 126 | } 127 | 128 | public boolean isRequestAddOrUpdateEnable() { 129 | return requestAddOrUpdateEnable; 130 | } 131 | 132 | public GlobalConfig setRequestAddOrUpdateEnable(boolean requestAddOrUpdateEnable) { 133 | this.requestAddOrUpdateEnable = requestAddOrUpdateEnable; 134 | return this; 135 | } 136 | 137 | public String getEntityName() { 138 | return entityName; 139 | } 140 | 141 | public GlobalConfig setEntityName(String entityName) { 142 | this.entityName = entityName; 143 | return this; 144 | } 145 | 146 | public String getRequestAddName() { 147 | return requestAddName; 148 | } 149 | 150 | public GlobalConfig setRequestAddName(String requestAddName) { 151 | this.requestAddName = requestAddName; 152 | return this; 153 | } 154 | 155 | public String getRequestUpdateName() { 156 | return requestUpdateName; 157 | } 158 | 159 | public GlobalConfig setRequestUpdateName(String requestUpdateName) { 160 | this.requestUpdateName = requestUpdateName; 161 | return this; 162 | } 163 | 164 | public String getRequestName() { 165 | return requestName; 166 | } 167 | 168 | public GlobalConfig setRequestName(String requestName) { 169 | this.requestName = requestName; 170 | return this; 171 | } 172 | 173 | public String getRequestSearchName() { 174 | return requestSearchName; 175 | } 176 | 177 | public GlobalConfig setRequestSearchName(String requestSearchName) { 178 | this.requestSearchName = requestSearchName; 179 | return this; 180 | } 181 | 182 | public String getDtoName() { 183 | return dtoName; 184 | } 185 | 186 | public GlobalConfig setDtoName(String dtoName) { 187 | this.dtoName = dtoName; 188 | return this; 189 | } 190 | 191 | public String getXmlName() { 192 | return xmlName; 193 | } 194 | 195 | public GlobalConfig setXmlName(String xmlName) { 196 | this.xmlName = xmlName; 197 | return this; 198 | } 199 | 200 | public String getDaoName() { 201 | return daoName; 202 | } 203 | 204 | public GlobalConfig setDaoName(String daoName) { 205 | this.daoName = daoName; 206 | return this; 207 | } 208 | 209 | public String getServiceName() { 210 | return serviceName; 211 | } 212 | 213 | public GlobalConfig setServiceName(String serviceName) { 214 | this.serviceName = serviceName; 215 | return this; 216 | } 217 | 218 | public String getApiName() { 219 | return apiName; 220 | } 221 | 222 | public GlobalConfig setApiName(String apiName) { 223 | this.apiName = apiName; 224 | return this; 225 | } 226 | 227 | public String getProviderName() { 228 | return providerName; 229 | } 230 | 231 | public GlobalConfig setProviderName(String providerName) { 232 | this.providerName = providerName; 233 | return this; 234 | } 235 | 236 | public String getJunitName() { 237 | return junitName; 238 | } 239 | 240 | public GlobalConfig setJunitName(String junitName) { 241 | this.junitName = junitName; 242 | return this; 243 | } 244 | 245 | public boolean isFileOverride() { 246 | return fileOverride; 247 | } 248 | 249 | public GlobalConfig setFileOverride(boolean fileOverride) { 250 | this.fileOverride = fileOverride; 251 | return this; 252 | } 253 | 254 | public boolean isOpen() { 255 | return open; 256 | } 257 | 258 | public GlobalConfig setOpen(boolean open) { 259 | this.open = open; 260 | return this; 261 | } 262 | 263 | public String getProjectModuleCode() { 264 | return projectModuleCode; 265 | } 266 | 267 | public GlobalConfig setProjectModuleCode(String projectModuleCode) { 268 | this.projectModuleCode = projectModuleCode; 269 | return this; 270 | } 271 | } 272 | 273 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/PackageConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | /** 6 | * @author panzhi 7 | * @Description 8 | * @since 2021-03-11 9 | */ 10 | public class PackageConfig { 11 | 12 | /** 13 | * 父包名。如果为空,将下面子包名必须写全部,否则就只需写子包名 14 | */ 15 | private String parent; 16 | 17 | /** 18 | * 父包模块名 19 | */ 20 | private String moduleName = null; 21 | 22 | /** 23 | * Entity包名 24 | */ 25 | private String entity = "entity"; 26 | 27 | /** 28 | * Request包名 29 | */ 30 | private String request = "request"; 31 | 32 | /** 33 | * Dto包名 34 | */ 35 | private String dto = "dto"; 36 | 37 | /** 38 | * Mapper XML包名 39 | */ 40 | private String xml = "META-INF.mapper"; 41 | 42 | /** 43 | * Menu json包名 44 | */ 45 | private String menuJson = "META-INF.config"; 46 | 47 | /** 48 | * Dao包名 49 | */ 50 | private String dao = "dao"; 51 | 52 | /** 53 | * Service包名 54 | */ 55 | private String service = "service"; 56 | 57 | /** 58 | * Api包名 59 | */ 60 | private String api = "api"; 61 | 62 | /** 63 | * Provider包名 64 | */ 65 | private String provider = "dubbo"; 66 | 67 | /** 68 | * Junit包名 69 | */ 70 | private String junit = "junit"; 71 | 72 | 73 | /** 74 | * 父包名 75 | */ 76 | public String getParent() { 77 | if (StringUtils.isNotBlank(moduleName)) { 78 | return parent + "." + moduleName; 79 | } 80 | return parent; 81 | } 82 | 83 | public PackageConfig setParent(String parent) { 84 | this.parent = parent; 85 | return this; 86 | } 87 | 88 | public String getModuleName() { 89 | return moduleName; 90 | } 91 | 92 | public PackageConfig setModuleName(String moduleName) { 93 | this.moduleName = moduleName; 94 | return this; 95 | } 96 | 97 | public String getEntity() { 98 | return entity; 99 | } 100 | 101 | public PackageConfig setEntity(String entity) { 102 | this.entity = entity; 103 | return this; 104 | } 105 | 106 | public String getRequest() { 107 | return request; 108 | } 109 | 110 | public PackageConfig setRequest(String request) { 111 | this.request = request; 112 | return this; 113 | } 114 | 115 | public String getDto() { 116 | return dto; 117 | } 118 | 119 | public PackageConfig setDto(String dto) { 120 | this.dto = dto; 121 | return this; 122 | } 123 | 124 | public String getDao() { 125 | return dao; 126 | } 127 | 128 | public PackageConfig setDao(String dao) { 129 | this.dao = dao; 130 | return this; 131 | } 132 | 133 | public String getService() { 134 | return service; 135 | } 136 | 137 | public PackageConfig setService(String service) { 138 | this.service = service; 139 | return this; 140 | } 141 | 142 | public String getApi() { 143 | return api; 144 | } 145 | 146 | public PackageConfig setApi(String api) { 147 | this.api = api; 148 | return this; 149 | } 150 | 151 | public String getProvider() { 152 | return provider; 153 | } 154 | 155 | public PackageConfig setProvider(String provider) { 156 | this.provider = provider; 157 | return this; 158 | } 159 | 160 | public String getJunit() { 161 | return junit; 162 | } 163 | 164 | public PackageConfig setJunit(String junit) { 165 | this.junit = junit; 166 | return this; 167 | } 168 | 169 | public String getXml() { 170 | return xml; 171 | } 172 | 173 | public PackageConfig setXml(String xml) { 174 | this.xml = xml; 175 | return this; 176 | } 177 | 178 | public String getMenuJson() { 179 | return menuJson; 180 | } 181 | 182 | public PackageConfig setMenuJson(String menuJson) { 183 | this.menuJson = menuJson; 184 | return this; 185 | } 186 | } 187 | 188 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/StrategyConfig.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config; 2 | 3 | import com.example.generator.config.converts.ITypeHandler; 4 | 5 | import java.util.Map; 6 | 7 | /** 8 | * @author panzhi 9 | * @Description 10 | * @since 2021-03-11 11 | */ 12 | public class StrategyConfig { 13 | 14 | /** 15 | * 是否跳过视图 16 | */ 17 | private boolean skipView = false; 18 | 19 | /** 20 | * 自定义继承的Entity类全称,带包名 21 | */ 22 | private String superEntityClass; 23 | 24 | /** 25 | * 自定义继承的Request类全称,带包名 26 | */ 27 | private String superRequestClass; 28 | 29 | /** 30 | * 自定义继承的DTO类全称,带包名 31 | */ 32 | private String superDtoClass; 33 | 34 | /** 35 | * 自定义继承的Dao类全称,带包名 36 | */ 37 | private String superDaoClass; 38 | 39 | /** 40 | * 自定义继承的Service类全称,带包名 41 | */ 42 | private String superServiceClass; 43 | 44 | /** 45 | * 表前缀,反向生成的时候会排除前缀 46 | */ 47 | private String[] tablePrefix; 48 | 49 | /** 50 | * 需要生成的表名,允许正则表达式(与 tableMap 二选一配置) 51 | */ 52 | private String[] includeTable; 53 | 54 | /** 55 | * 需要生成的表名,填充格式(key-value):表名-实体,例如,tb_user: User 56 | */ 57 | private Map tableMap; 58 | 59 | /** 60 | * mybatis指定字段类型转化 61 | */ 62 | private ITypeHandler typeHandler; 63 | 64 | public boolean isSkipView() { 65 | return skipView; 66 | } 67 | 68 | public StrategyConfig setSkipView(boolean skipView) { 69 | this.skipView = skipView; 70 | return this; 71 | } 72 | 73 | public String getSuperRequestClass() { 74 | return superRequestClass; 75 | } 76 | 77 | public StrategyConfig setSuperRequestClass(String superRequestClass) { 78 | this.superRequestClass = superRequestClass; 79 | return this; 80 | } 81 | 82 | public String getSuperDtoClass() { 83 | return superDtoClass; 84 | } 85 | 86 | public StrategyConfig setSuperDtoClass(String superDtoClass) { 87 | this.superDtoClass = superDtoClass; 88 | return this; 89 | } 90 | 91 | public String getSuperEntityClass() { 92 | return superEntityClass; 93 | } 94 | 95 | public StrategyConfig setSuperEntityClass(String superEntityClass) { 96 | this.superEntityClass = superEntityClass; 97 | return this; 98 | } 99 | 100 | public String getSuperServiceClass() { 101 | return superServiceClass; 102 | } 103 | 104 | public StrategyConfig setSuperServiceClass(String superServiceClass) { 105 | this.superServiceClass = superServiceClass; 106 | return this; 107 | } 108 | 109 | public String getSuperDaoClass() { 110 | return superDaoClass; 111 | } 112 | 113 | public StrategyConfig setSuperDaoClass(String superDaoClass) { 114 | this.superDaoClass = superDaoClass; 115 | return this; 116 | } 117 | 118 | public String[] getTablePrefix() { 119 | return tablePrefix; 120 | } 121 | 122 | public StrategyConfig setTablePrefix(String... tablePrefix) { 123 | this.tablePrefix = tablePrefix; 124 | return this; 125 | } 126 | 127 | public String[] getIncludeTable() { 128 | return includeTable; 129 | } 130 | 131 | public StrategyConfig setIncludeTable(String[] includeTable) { 132 | this.includeTable = includeTable; 133 | return this; 134 | } 135 | 136 | public Map getTableMap() { 137 | return tableMap; 138 | } 139 | 140 | public StrategyConfig setTableMap(Map tableMap) { 141 | this.tableMap = tableMap; 142 | return this; 143 | } 144 | 145 | public ITypeHandler getTypeHandler() { 146 | return typeHandler; 147 | } 148 | 149 | public StrategyConfig setTypeHandler(ITypeHandler typeHandler) { 150 | this.typeHandler = typeHandler; 151 | return this; 152 | } 153 | 154 | 155 | } 156 | 157 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/converts/ITypeConvert.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.converts; 2 | 3 | import com.example.generator.config.rules.IColumnType; 4 | 5 | /** 6 | * @author panzhi 7 | * @Description 字段类型转换器 8 | * @since 2021-03-11 9 | */ 10 | public interface ITypeConvert { 11 | 12 | /** 13 | * 执行数据库类型转换 14 | * @param fieldName 15 | * @param fieldType 16 | * @return 17 | */ 18 | String convertJdbcType(String fieldName, String fieldType); 19 | 20 | /** 21 | * 执行实体类型转换 22 | * @param fieldName 23 | * @param fieldType 24 | * @return 25 | */ 26 | IColumnType convertPropertyType(String fieldName, String fieldType); 27 | 28 | /** 29 | * 支持自定义类型转化 30 | * @param fieldName 31 | * @param fieldType 32 | * @return 33 | */ 34 | default IColumnType customerType(String fieldName,String fieldType){ 35 | return null; 36 | }; 37 | } 38 | 39 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/converts/ITypeHandler.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.converts; 2 | 3 | import com.example.generator.constant.StringPool; 4 | 5 | /** 6 | * @author panzhi 7 | * @Description 字段适配转换器 8 | * @since 2021-03-11 9 | */ 10 | public interface ITypeHandler { 11 | 12 | /** 13 | * 是否启用转化 14 | * @param fieldName 15 | * @return 16 | */ 17 | default boolean enable(String fieldName){ 18 | return false; 19 | } 20 | 21 | /** 22 | * Mybatis字段类型转化 23 | * @param fieldName 24 | * @return 25 | */ 26 | default String processTypeHandler(String fieldName){ 27 | return StringPool.EMPTY; 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/converts/apdater/MySqlTypeConvert.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.converts.apdater; 2 | 3 | import com.example.generator.config.converts.ITypeConvert; 4 | import com.example.generator.config.rules.DbColumnType; 5 | import com.example.generator.config.rules.IColumnType; 6 | import org.apache.commons.lang3.StringUtils; 7 | 8 | import java.util.Objects; 9 | 10 | /** 11 | * @author panzhi 12 | * @Description 13 | * @since 2021-03-11 14 | */ 15 | public class MySqlTypeConvert implements ITypeConvert { 16 | 17 | 18 | @Override 19 | public String convertJdbcType(String fieldName, String fieldType) { 20 | String dbFieldType = fieldType.toUpperCase(); 21 | if (StringUtils.equals(dbFieldType, "DATETIME")) { 22 | return "TIMESTAMP"; 23 | } 24 | if (StringUtils.endsWithIgnoreCase(dbFieldType, "TEXT")) { 25 | return "VARCHAR"; 26 | } 27 | if (StringUtils.equals(dbFieldType, "INTEGER") 28 | || StringUtils.equals(dbFieldType, "INT") 29 | || StringUtils.equals(dbFieldType, "TINYINT") 30 | || StringUtils.equals(dbFieldType, "SMALLINT") 31 | || StringUtils.equals(dbFieldType, "MEDIUMINT")) { 32 | return "INTEGER"; 33 | } 34 | return dbFieldType; 35 | } 36 | 37 | @Override 38 | public IColumnType convertPropertyType(String fieldName, String fieldType) { 39 | IColumnType customerType = customerType(fieldName, fieldType); 40 | if(Objects.nonNull(customerType)){ 41 | return customerType; 42 | } 43 | String t = fieldType.toLowerCase(); 44 | if (t.contains("char")) { 45 | return DbColumnType.STRING; 46 | } else if (t.contains("bigint")) { 47 | return DbColumnType.LONG; 48 | } else if (t.contains("tinyint(1)")) { 49 | return DbColumnType.BOOLEAN; 50 | } else if (t.contains("int")) { 51 | return DbColumnType.INTEGER; 52 | } else if (t.contains("text")) { 53 | return DbColumnType.STRING; 54 | } else if (t.contains("bit")) { 55 | return DbColumnType.BOOLEAN; 56 | } else if (t.contains("decimal")) { 57 | return DbColumnType.BIG_DECIMAL; 58 | } else if (t.contains("clob")) { 59 | return DbColumnType.CLOB; 60 | } else if (t.contains("blob")) { 61 | return DbColumnType.BLOB; 62 | } else if (t.contains("binary")) { 63 | return DbColumnType.BYTE_ARRAY; 64 | } else if (t.contains("float")) { 65 | return DbColumnType.FLOAT; 66 | } else if (t.contains("double")) { 67 | return DbColumnType.DOUBLE; 68 | } else if (t.contains("json") || t.contains("enum")) { 69 | return DbColumnType.STRING; 70 | } else if (t.contains("date") || t.contains("time") || t.contains("year")) { 71 | return DbColumnType.DATE; 72 | } 73 | return DbColumnType.STRING; 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/rules/DbColumnType.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.rules; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public enum DbColumnType implements IColumnType { 9 | 10 | // 基本类型 11 | BASE_BYTE("byte", null), 12 | BASE_SHORT("short", null), 13 | BASE_CHAR("char", null), 14 | BASE_INT("int", null), 15 | BASE_LONG("long", null), 16 | BASE_FLOAT("float", null), 17 | BASE_DOUBLE("double", null), 18 | BASE_BOOLEAN("boolean", null), 19 | 20 | // 包装类型 21 | BYTE("Byte", null), 22 | SHORT("Short", null), 23 | CHARACTER("Character", null), 24 | INTEGER("Integer", null), 25 | LONG("Long", null), 26 | FLOAT("Float", null), 27 | DOUBLE("Double", null), 28 | BOOLEAN("Boolean", null), 29 | STRING("String", null), 30 | 31 | // sql 包下数据类型 32 | DATE_SQL("Date", "java.sql.Date"), 33 | TIME("Time", "java.sql.Time"), 34 | TIMESTAMP("Timestamp", "java.sql.Timestamp"), 35 | BLOB("Blob", "java.sql.Blob"), 36 | CLOB("Clob", "java.sql.Clob"), 37 | 38 | // 其他杂类 39 | BYTE_ARRAY("byte[]", null), 40 | OBJECT("Object", null), 41 | DATE("Date", "java.util.Date"), 42 | BIG_INTEGER("BigInteger", "java.math.BigInteger"), 43 | BIG_DECIMAL("BigDecimal", "java.math.BigDecimal"); 44 | 45 | /** 46 | * 类型 47 | */ 48 | private final String type; 49 | 50 | /** 51 | * 包路径 52 | */ 53 | private final String pkg; 54 | 55 | DbColumnType(final String type, final String pkg) { 56 | this.type = type; 57 | this.pkg = pkg; 58 | } 59 | 60 | @Override 61 | public String getType() { 62 | return type; 63 | } 64 | 65 | @Override 66 | public String getPkg() { 67 | return pkg; 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/rules/IColumnType.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.rules; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public interface IColumnType { 9 | 10 | /** 11 | * 12 | * 获取字段类型 13 | * @return 字段类型 14 | */ 15 | String getType(); 16 | 17 | /** 18 | * 19 | * 获取字段类型完整名 20 | * @return 字段类型完整名 21 | */ 22 | String getPkg(); 23 | } 24 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/type/DbType.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.type; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public enum DbType { 9 | 10 | MYSQL, 11 | ORACLE, 12 | SQL_SERVER 13 | } 14 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/type/IDbQuery.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.type; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public interface IDbQuery { 9 | 10 | /** 11 | * 数据库类型 12 | */ 13 | DbType dbType(); 14 | 15 | /** 16 | * 数据库查询 SQL 17 | * @return 18 | */ 19 | String dataBase(); 20 | 21 | /** 22 | * 表信息查询 SQL 23 | */ 24 | String tablesSql(); 25 | 26 | 27 | /** 28 | * 表字段信息查询 SQL 29 | */ 30 | String tableFieldsSql(); 31 | 32 | 33 | /** 34 | * 表名称 35 | */ 36 | String tableName(); 37 | 38 | 39 | /** 40 | * 表注释 41 | */ 42 | String tableComment(); 43 | 44 | 45 | /** 46 | * 字段名称 47 | */ 48 | String fieldName(); 49 | 50 | 51 | /** 52 | * 字段类型 53 | */ 54 | String fieldType(); 55 | 56 | /** 57 | * 字段长度 58 | * @return 59 | */ 60 | String fieldLength(); 61 | 62 | /** 63 | * 字段注释 64 | */ 65 | String fieldComment(); 66 | 67 | /** 68 | * 主键字段 69 | */ 70 | String fieldKey(); 71 | } 72 | 73 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/config/type/querys/MySqlQuery.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.config.type.querys; 2 | 3 | import com.example.generator.config.type.DbType; 4 | import com.example.generator.config.type.IDbQuery; 5 | 6 | /** 7 | * @author panzhi 8 | * @Description 9 | * @since 2021-03-11 10 | */ 11 | public class MySqlQuery implements IDbQuery { 12 | 13 | 14 | @Override 15 | public DbType dbType() { 16 | return DbType.MYSQL; 17 | } 18 | 19 | @Override 20 | public String dataBase() { 21 | return "select database()"; 22 | } 23 | 24 | 25 | @Override 26 | public String tablesSql() { 27 | return "show table status"; 28 | } 29 | 30 | 31 | @Override 32 | public String tableFieldsSql() { 33 | return "SELECT * FROM information_schema.columns where table_name = '%s' and table_schema = '%s'"; 34 | } 35 | 36 | 37 | @Override 38 | public String tableName() { 39 | return "NAME"; 40 | } 41 | 42 | 43 | @Override 44 | public String tableComment() { 45 | return "COMMENT"; 46 | } 47 | 48 | 49 | @Override 50 | public String fieldName() { 51 | return "COLUMN_NAME"; 52 | } 53 | 54 | 55 | @Override 56 | public String fieldType() { 57 | return "DATA_TYPE"; 58 | } 59 | 60 | @Override 61 | public String fieldLength() { 62 | return "CHARACTER_MAXIMUM_LENGTH"; 63 | } 64 | 65 | 66 | @Override 67 | public String fieldComment() { 68 | return "COLUMN_COMMENT"; 69 | } 70 | 71 | @Override 72 | public String fieldKey() { 73 | return "COLUMN_KEY"; 74 | } 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/constant/ConstValue.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.constant; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public class ConstValue { 9 | 10 | // ************************ 全局配置常量 ***************************** 11 | 12 | public static final String GLOBAL_REQUEST = "Request"; 13 | public static final String GLOBAL_REQUEST_ADD = "RequestAdd"; 14 | public static final String GLOBAL_REQUEST_UPDATE = "RequestUpd"; 15 | public static final String GLOBAL_REQUEST_SEARCH = "RequestSearch"; 16 | public static final String GLOBAL_DTO = "DTO"; 17 | public static final String GLOBAL_XML = "Mapper"; 18 | public static final String GLOBAL_DAO = "Mapper"; 19 | public static final String GLOBAL_SERVICE = "Service"; 20 | public static final String GLOBAL_API = "Api"; 21 | public static final String GLOBAL_PROVIDER = "ApiImpl"; 22 | public static final String GLOBAL_JUNIT = "Junit"; 23 | 24 | 25 | // ************************ 包配置常量key ***************************** 26 | 27 | public static final String PACKAGE_ENTITY = "entity"; 28 | public static final String PACKAGE_REQUEST = "request"; 29 | public static final String PACKAGE_DTO = "dto"; 30 | public static final String PACKAGE_DAO = "dao"; 31 | public static final String PACKAGE_SERVICE = "service"; 32 | public static final String PACKAGE_API = "api"; 33 | public static final String PACKAGE_PROVIDER = "provider"; 34 | public static final String PACKAGE_XML = "xml"; 35 | public static final String PACKAGE_JUNIT = "junit"; 36 | public static final String PACKAGE_MENU_JSON = "menuJson"; 37 | 38 | // ************************ 文件路径配置常量key ***************************** 39 | 40 | public static final String ENTITY_PATH = "entity_path"; 41 | public static final String REQUEST_PATH = "request_path"; 42 | public static final String DTO_PATH = "dto_path"; 43 | public static final String XML_PATH = "xml_path"; 44 | public static final String DAO_PATH = "dao_path"; 45 | public static final String SERVICE_PATH = "service_path"; 46 | public static final String API_PATH = "api_path"; 47 | public static final String PROVIDER_PATH = "provider_path"; 48 | public static final String JUNIT_PATH = "junit_path"; 49 | public static final String MENU_PATH = "menu_path"; 50 | 51 | 52 | // ************************ 模版配置后缀常量 ***************************** 53 | 54 | public static final String TEMPLATE_JAVA_SUFFIX = "%s.java"; 55 | public static final String TEMPLATE_XML_SUFFIX = "%s.xml"; 56 | public static final String TEMPLATE_JSON_SUFFIX = "menu.json"; 57 | 58 | 59 | // ************************ 模版配置后缀常量 ***************************** 60 | 61 | public static final String TEMPLATE_ENTITY = "/templates/entity.java.ftl"; 62 | public static final String TEMPLATE_REQUEST = "/templates/request.java.ftl"; 63 | public static final String TEMPLATE_REQUEST_ADD = "/templates/requestAdd.java.ftl"; 64 | public static final String TEMPLATE_REQUEST_UPDATE = "/templates/requestUpd.java.ftl"; 65 | public static final String TEMPLATE_REQUEST_SEARCH = "/templates/requestSearch.java.ftl"; 66 | public static final String TEMPLATE_DTO = "/templates/dto.java.ftl"; 67 | public static final String TEMPLATE_XML = "/templates/mapper.xml.ftl"; 68 | public static final String TEMPLATE_DAO = "/templates/dao.java.ftl"; 69 | public static final String TEMPLATE_SERVICE = "/templates/service.java.ftl"; 70 | public static final String TEMPLATE_API = "/templates/api.java.ftl"; 71 | public static final String TEMPLATE_PROVIDER = "/templates/provider.java.ftl"; 72 | public static final String TEMPLATE_JUNIT = "/templates/junit.java.ftl"; 73 | public static final String TEMPLATE_MENU_JSON = "/templates/menu.json.ftl"; 74 | 75 | // ************************ 菜单配置常量 ***************************** 76 | 77 | public static final String MENU_NOTE = "权限编码:"; 78 | public static final String MENU_PAGE_CODE = "page"; 79 | public static final String MENU_PAGE_URL = "请填写前端页面地址"; 80 | public static final String MENU_API_SELECT_PAGE = "selectPage"; 81 | public static final String MENU_API_SELECT_PAGE_NAME = "列表"; 82 | public static final String MENU_API_GET_DETAIL = "getDetail"; 83 | public static final String MENU_API_GET_DETAIL_NAME = "详情"; 84 | public static final String MENU_API_ADD = "add"; 85 | public static final String MENU_API_ADD_NAME = "新增"; 86 | public static final String MENU_API_EDIT = "edit"; 87 | public static final String MENU_API_EDIT_NAME = "编辑"; 88 | public static final String MENU_API_DELETE = "delete"; 89 | public static final String MENU_API_DELETE_NAME = "删除"; 90 | 91 | // ************************ 其他配置常量 ***************************** 92 | 93 | public static final String TABLE_PRIMARY = "id"; 94 | public static final String JAVA_TMPDIR = "java.io.tmpdir"; 95 | 96 | } 97 | 98 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/constant/StringPool.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.constant; 2 | 3 | import java.nio.charset.Charset; 4 | 5 | /** 6 | * @author panzhi 7 | * @Description 8 | * @since 2021-03-11 9 | */ 10 | public class StringPool { 11 | 12 | public static final String DOT = "."; 13 | 14 | public static final String BACK_SLASH = "\\"; 15 | 16 | public static final String EMPTY = ""; 17 | 18 | public static final String UNDERLINE = "_"; 19 | 20 | public static final String HAT = "^"; 21 | 22 | public static final String COLON = ":"; 23 | 24 | public static final String SLASH = "/"; 25 | 26 | public static final String UTF8 = Charset.forName("UTF-8").name(); 27 | 28 | } -------------------------------------------------------------------------------- /src/main/java/com/example/generator/engine/TemplateEngine.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.engine; 2 | 3 | import com.example.generator.builder.ConfigBuilder; 4 | import com.example.generator.common.dao.BaseMapper; 5 | import com.example.generator.common.request.BaseRequest; 6 | import com.example.generator.common.service.BaseService; 7 | import com.example.generator.config.GlobalConfig; 8 | import com.example.generator.config.StrategyConfig; 9 | import com.example.generator.constant.ConstValue; 10 | import com.example.generator.constant.StringPool; 11 | import com.example.generator.entity.TableField; 12 | import com.example.generator.entity.TableInfo; 13 | import freemarker.template.Configuration; 14 | import freemarker.template.Template; 15 | import org.apache.commons.lang3.StringUtils; 16 | 17 | import java.io.File; 18 | import java.io.FileOutputStream; 19 | import java.io.IOException; 20 | import java.io.OutputStreamWriter; 21 | import java.text.SimpleDateFormat; 22 | import java.util.*; 23 | 24 | /** 25 | * @author panzhi 26 | * @Description 27 | * @since 2021-03-11 28 | */ 29 | public class TemplateEngine { 30 | 31 | /** 32 | * 模版生成器 33 | */ 34 | private Configuration configuration; 35 | 36 | /** 37 | * 配置信息 38 | */ 39 | private ConfigBuilder configBuilder; 40 | 41 | /** 42 | * 模板引擎初始化 43 | * @param configBuilder 44 | * @return 45 | */ 46 | public TemplateEngine init(ConfigBuilder configBuilder) { 47 | this.configBuilder = configBuilder; 48 | configuration = new Configuration(Configuration.VERSION_2_3_23); 49 | configuration.setDefaultEncoding(StringPool.UTF8); 50 | configuration.setClassForTemplateLoading(TemplateEngine.class, "/"); 51 | return this; 52 | } 53 | 54 | /** 55 | * 处理输出目录 56 | * @return 57 | */ 58 | public TemplateEngine mkdirs() { 59 | getConfigBuilder().getPathInfo().forEach((key, value) -> { 60 | File dir = new File(value); 61 | if (!dir.exists()) { 62 | boolean result = dir.mkdirs(); 63 | if (result) { 64 | System.out.println("创建目录: [" + value + "]"); 65 | } 66 | } 67 | }); 68 | return this; 69 | } 70 | 71 | /** 72 | * 输出 java xml 文件 73 | * @return 74 | */ 75 | public TemplateEngine batchOutput() { 76 | try { 77 | List tableInfoList = configBuilder.getTableInfoList(); 78 | for (TableInfo tableInfo : tableInfoList) { 79 | Map objectMap = getObjectMap(tableInfo); 80 | Map pathInfo = configBuilder.getPathInfo(); 81 | // Entity.java 82 | if (StringUtils.isNotBlank(tableInfo.getEntityName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.ENTITY_PATH))) { 83 | String entityFile = String.format((pathInfo.get(ConstValue.ENTITY_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getEntityName()); 84 | if (isCreate(entityFile)) { 85 | writer(objectMap, ConstValue.TEMPLATE_ENTITY, entityFile); 86 | } 87 | } 88 | // Request.java 89 | if(!configBuilder.getGlobalConfig().isRequestAddOrUpdateEnable()){ 90 | if (StringUtils.isNotBlank(tableInfo.getRequestName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.REQUEST_PATH))) { 91 | String requestFile = String.format((pathInfo.get(ConstValue.REQUEST_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getRequestName()); 92 | if (isCreate(requestFile)) { 93 | writer(objectMap, ConstValue.TEMPLATE_REQUEST, requestFile); 94 | } 95 | } 96 | } else { 97 | // RequestAdd.java RequestUpd.java 98 | if (StringUtils.isNotBlank(tableInfo.getRequestAddName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.REQUEST_PATH))) { 99 | String requestAddFile = String.format((pathInfo.get(ConstValue.REQUEST_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getRequestAddName()); 100 | if (isCreate(requestAddFile)) { 101 | writer(objectMap, ConstValue.TEMPLATE_REQUEST_ADD, requestAddFile); 102 | } 103 | } 104 | if (StringUtils.isNotBlank(tableInfo.getRequestUpdateName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.REQUEST_PATH))) { 105 | String requestUpdFile = String.format((pathInfo.get(ConstValue.REQUEST_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getRequestUpdateName()); 106 | if (isCreate(requestUpdFile)) { 107 | writer(objectMap, ConstValue.TEMPLATE_REQUEST_UPDATE, requestUpdFile); 108 | } 109 | } 110 | } 111 | // RequestSearch.java 112 | if (StringUtils.isNotBlank(tableInfo.getRequestSearchName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.REQUEST_PATH))) { 113 | String requestSearchFile = String.format((pathInfo.get(ConstValue.REQUEST_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getRequestSearchName()); 114 | if (isCreate(requestSearchFile)) { 115 | writer(objectMap, ConstValue.TEMPLATE_REQUEST_SEARCH, requestSearchFile); 116 | } 117 | } 118 | // DTO.java 119 | if (StringUtils.isNotBlank(tableInfo.getDtoName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.DTO_PATH))) { 120 | String dtoFile = String.format((pathInfo.get(ConstValue.DTO_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getDtoName()); 121 | if (isCreate(dtoFile)) { 122 | writer(objectMap, ConstValue.TEMPLATE_DTO, dtoFile); 123 | } 124 | } 125 | // Mapper.xml 126 | if (StringUtils.isNotBlank(tableInfo.getXmlName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.XML_PATH))) { 127 | String xmlFile = String.format((pathInfo.get(ConstValue.XML_PATH) + File.separator + ConstValue.TEMPLATE_XML_SUFFIX), tableInfo.getXmlName()); 128 | if (isCreate(xmlFile)) { 129 | writer(objectMap, ConstValue.TEMPLATE_XML, xmlFile); 130 | } 131 | } 132 | // Menu.json 133 | if (StringUtils.isNotBlank(pathInfo.get(ConstValue.MENU_PATH))) { 134 | String menuJsonFile = pathInfo.get(ConstValue.MENU_PATH) + File.separator + ConstValue.TEMPLATE_JSON_SUFFIX; 135 | if (isCreate(menuJsonFile)) { 136 | writer(objectMap, ConstValue.TEMPLATE_MENU_JSON, menuJsonFile); 137 | } 138 | } 139 | // Dao.java 140 | if (StringUtils.isNotBlank(tableInfo.getDaoName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.DAO_PATH))) { 141 | String daoFile = String.format((pathInfo.get(ConstValue.DAO_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getDaoName()); 142 | if (isCreate(daoFile)) { 143 | writer(objectMap, ConstValue.TEMPLATE_DAO, daoFile); 144 | } 145 | } 146 | // Service.java 147 | if (StringUtils.isNotBlank(tableInfo.getServiceName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.SERVICE_PATH))) { 148 | String serviceFile = String.format((pathInfo.get(ConstValue.SERVICE_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getServiceName()); 149 | if (isCreate(serviceFile)) { 150 | writer(objectMap, ConstValue.TEMPLATE_SERVICE, serviceFile); 151 | } 152 | } 153 | // Api.java 154 | if (StringUtils.isNotBlank(tableInfo.getApiName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.API_PATH))) { 155 | String apiFile = String.format((pathInfo.get(ConstValue.API_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getApiName()); 156 | if (isCreate(apiFile)) { 157 | writer(objectMap, ConstValue.TEMPLATE_API, apiFile); 158 | } 159 | } 160 | // Provider.java 161 | if (StringUtils.isNotBlank(tableInfo.getProviderName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.PROVIDER_PATH))) { 162 | String providerFile = String.format((pathInfo.get(ConstValue.PROVIDER_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getProviderName()); 163 | if (isCreate(providerFile)) { 164 | writer(objectMap, ConstValue.TEMPLATE_PROVIDER, providerFile); 165 | } 166 | } 167 | // Junit.java 168 | if (StringUtils.isNotBlank(tableInfo.getJunitName()) && StringUtils.isNotBlank(pathInfo.get(ConstValue.JUNIT_PATH))) { 169 | String junitFile = String.format((pathInfo.get(ConstValue.JUNIT_PATH) + File.separator + ConstValue.TEMPLATE_JAVA_SUFFIX), tableInfo.getJunitName()); 170 | if (isCreate(junitFile)) { 171 | writer(objectMap, ConstValue.TEMPLATE_JUNIT, junitFile); 172 | } 173 | } 174 | } 175 | } catch (Exception e) { 176 | System.out.println("无法创建文件,请检查配置信息,错误信息:" + e.getMessage()); 177 | } 178 | return this; 179 | } 180 | 181 | /** 182 | * 打开输出目录 183 | */ 184 | public void open() { 185 | String outDir = getConfigBuilder().getGlobalConfig().getOutputDir(); 186 | if (getConfigBuilder().getGlobalConfig().isOpen() 187 | && StringUtils.isNotEmpty(outDir)) { 188 | try { 189 | String osName = System.getProperty("os.name"); 190 | if (osName != null) { 191 | if (osName.contains("Mac")) { 192 | Runtime.getRuntime().exec("open " + outDir); 193 | } else if (osName.contains("Windows")) { 194 | Runtime.getRuntime().exec("cmd /c start " + outDir); 195 | } else { 196 | System.out.println("文件输出目录:" + outDir); 197 | } 198 | } 199 | } catch (IOException e) { 200 | e.printStackTrace(); 201 | } 202 | } 203 | } 204 | 205 | 206 | 207 | /** 208 | * 将模板转化成为文件 209 | * @param objectMap 渲染对象 MAP 信息 210 | * @param templatePath 模板文件 211 | * @param outputFile 文件生成的目录 212 | */ 213 | public void writer(Map objectMap, String templatePath, String outputFile) throws Exception { 214 | Template template = configuration.getTemplate(templatePath); 215 | FileOutputStream fileOutputStream = new FileOutputStream(new File(outputFile)); 216 | template.process(objectMap, new OutputStreamWriter(fileOutputStream, StringPool.UTF8)); 217 | fileOutputStream.close(); 218 | System.out.println("模板:" + templatePath + "; 文件:" + outputFile); 219 | }; 220 | 221 | 222 | 223 | /** 224 | * 渲染对象 MAP 信息 225 | * @param tableInfo 表信息对象 226 | * @return 227 | */ 228 | private Map getObjectMap(TableInfo tableInfo) { 229 | Map objectMap = new HashMap<>(); 230 | ConfigBuilder config = getConfigBuilder(); 231 | objectMap.put("package", config.getPackageInfo()); 232 | // objectMap.put("resourceList", config.getResourceList()); 233 | GlobalConfig globalConfig = config.getGlobalConfig(); 234 | objectMap.put("projectModuleCode", globalConfig.getProjectModuleCode()); 235 | objectMap.put("isRequestAddOrUpdateEnable", globalConfig.isRequestAddOrUpdateEnable()); 236 | objectMap.put("author", globalConfig.getAuthor()); 237 | objectMap.put("date", new SimpleDateFormat("yyyy-MM-dd").format(new Date())); 238 | objectMap.put("tableName", tableInfo.getName()); 239 | objectMap.put("tableComment", tableInfo.getComment()); 240 | objectMap.put("primaryId", tableInfo.getPrimaryId()); 241 | objectMap.put("columns", tableInfo.getFields()); 242 | objectMap.put("fieldsImportPackage", getFieldImportPackage(tableInfo.getFields())); 243 | objectMap.put("entityClass", tableInfo.getEntityName()); 244 | objectMap.put("requestClass", tableInfo.getRequestName()); 245 | objectMap.put("requestAddClass", tableInfo.getRequestAddName()); 246 | objectMap.put("requestUpdateClass", tableInfo.getRequestUpdateName()); 247 | objectMap.put("requestSearchClass", tableInfo.getRequestSearchName()); 248 | objectMap.put("dtoClass", tableInfo.getDtoName()); 249 | objectMap.put("daoClass", tableInfo.getDaoName()); 250 | objectMap.put("serviceClass", tableInfo.getServiceName()); 251 | objectMap.put("apiClass", tableInfo.getApiName()); 252 | objectMap.put("providerClass", tableInfo.getProviderName()); 253 | objectMap.put("junitClass", tableInfo.getJunitName()); 254 | StrategyConfig strategyConfig = config.getStrategyConfig(); 255 | //继承类封装处理 256 | if(StringUtils.isNotBlank(strategyConfig.getSuperEntityClass())){ 257 | objectMap.put("superEntityClass", getSuperClassName(strategyConfig.getSuperEntityClass())); 258 | objectMap.put("superEntityClassPackage", strategyConfig.getSuperEntityClass()); 259 | } 260 | if(StringUtils.isNotBlank(strategyConfig.getSuperRequestClass())){ 261 | objectMap.put("superRequestClass", getSuperClassName(strategyConfig.getSuperRequestClass())); 262 | objectMap.put("superRequestClassPackage", strategyConfig.getSuperRequestClass()); 263 | } else { 264 | objectMap.put("superRequestClass", BaseRequest.class.getSimpleName()); 265 | objectMap.put("superRequestClassPackage", BaseRequest.class.getCanonicalName()); 266 | } 267 | if(StringUtils.isNotBlank(strategyConfig.getSuperDtoClass())){ 268 | objectMap.put("superDtoClass", getSuperClassName(strategyConfig.getSuperDtoClass())); 269 | objectMap.put("superDtoClassPackage", strategyConfig.getSuperDtoClass()); 270 | } 271 | if(StringUtils.isNotBlank(strategyConfig.getSuperDaoClass())){ 272 | objectMap.put("superDaoClass", getSuperClassName(strategyConfig.getSuperDaoClass())); 273 | objectMap.put("superDaoClassPackage", strategyConfig.getSuperDaoClass()); 274 | } else { 275 | objectMap.put("superDaoClass", BaseMapper.class.getSimpleName()); 276 | objectMap.put("superDaoClassPackage", BaseMapper.class.getCanonicalName()); 277 | } 278 | if(StringUtils.isNotBlank(strategyConfig.getSuperServiceClass())){ 279 | objectMap.put("superServiceClass", getSuperClassName(strategyConfig.getSuperServiceClass())); 280 | objectMap.put("superServiceClassPackage", strategyConfig.getSuperServiceClass()); 281 | } else { 282 | objectMap.put("superServiceClass", BaseService.class.getSimpleName()); 283 | objectMap.put("superServiceClassPackage", BaseService.class.getCanonicalName()); 284 | } 285 | return objectMap; 286 | } 287 | 288 | 289 | private Set getFieldImportPackage(List fields){ 290 | Set fieldImportPackage = new HashSet<>(); 291 | if(Objects.nonNull(fields)){ 292 | for (TableField field : fields) { 293 | if(StringUtils.isNotBlank(field.getPropertyTypePackage())){ 294 | fieldImportPackage.add(field.getPropertyTypePackage()); 295 | } 296 | } 297 | } 298 | return fieldImportPackage; 299 | } 300 | 301 | 302 | /** 303 | * 检测文件是否存在 304 | * 305 | * @return 是否 306 | */ 307 | private boolean isCreate(String filePath) { 308 | // 全局判断【默认】 309 | File file = new File(filePath); 310 | boolean exist = file.exists(); 311 | if (!exist) { 312 | mkDir(file.getParentFile()); 313 | } 314 | return !exist || configBuilder.getGlobalConfig().isFileOverride(); 315 | } 316 | 317 | /** 318 | * 新建文件目录 319 | * @param file 文件 320 | */ 321 | private void mkDir(File file) { 322 | if (file.getParentFile().exists()) { 323 | file.mkdir(); 324 | } else { 325 | mkDir(file.getParentFile()); 326 | file.mkdir(); 327 | } 328 | } 329 | 330 | /** 331 | * 获取类名 332 | * 333 | * @param classPath 334 | * @return 335 | */ 336 | private String getSuperClassName(String classPath) { 337 | if (StringUtils.isEmpty(classPath)) { 338 | return null; 339 | } 340 | return classPath.substring(classPath.lastIndexOf(StringPool.DOT) + 1); 341 | } 342 | 343 | 344 | 345 | public ConfigBuilder getConfigBuilder() { 346 | return configBuilder; 347 | } 348 | 349 | public void setConfigBuilder(ConfigBuilder configBuilder) { 350 | this.configBuilder = configBuilder; 351 | } 352 | 353 | 354 | } 355 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/entity/TableField.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.entity; 2 | 3 | /** 4 | * @author panzhi 5 | * @Description 6 | * @since 2021-03-11 7 | */ 8 | public class TableField { 9 | 10 | /** 11 | * 数据库原字段名 12 | */ 13 | private String fieldName; 14 | 15 | /** 16 | * 数据库原字段类型 17 | */ 18 | private String fieldType; 19 | 20 | /** 21 | * 数据库原字段长度 22 | */ 23 | private Integer fieldLength; 24 | 25 | /** 26 | * 转换成实体类的变量-变量注释 27 | */ 28 | private String comment; 29 | 30 | /** 31 | * 转换成实体类的-变量名 32 | */ 33 | private String propertyName; 34 | 35 | /** 36 | * 转换成实体类的-变量类型 37 | */ 38 | private String propertyType; 39 | 40 | /** 41 | * 转换成实体类的-变量类型完整路径 42 | */ 43 | private String propertyTypePackage; 44 | 45 | 46 | /** 47 | * 判断是否进行mybatis类型处理 48 | */ 49 | private boolean isTypeHandler; 50 | 51 | /** 52 | * mybatis类型处理 53 | */ 54 | private String typeHandler; 55 | 56 | 57 | public String getFieldName() { 58 | return fieldName; 59 | } 60 | 61 | public TableField setFieldName(String fieldName) { 62 | this.fieldName = fieldName; 63 | return this; 64 | } 65 | 66 | public String getFieldType() { 67 | return fieldType; 68 | } 69 | 70 | public TableField setFieldType(String fieldType) { 71 | this.fieldType = fieldType; 72 | return this; 73 | } 74 | 75 | public Integer getFieldLength() { 76 | return fieldLength; 77 | } 78 | 79 | public TableField setFieldLength(Integer fieldLength) { 80 | this.fieldLength = fieldLength; 81 | return this; 82 | } 83 | 84 | public String getComment() { 85 | return comment; 86 | } 87 | 88 | public TableField setComment(String comment) { 89 | this.comment = comment; 90 | return this; 91 | } 92 | 93 | public String getPropertyName() { 94 | return propertyName; 95 | } 96 | 97 | public TableField setPropertyName(String propertyName) { 98 | this.propertyName = propertyName; 99 | return this; 100 | } 101 | 102 | public String getPropertyType() { 103 | return propertyType; 104 | } 105 | 106 | public TableField setPropertyType(String propertyType) { 107 | this.propertyType = propertyType; 108 | return this; 109 | } 110 | 111 | public boolean getIsTypeHandler() { 112 | return isTypeHandler; 113 | } 114 | 115 | public TableField setIsTypeHandler(boolean typeHandler) { 116 | isTypeHandler = typeHandler; 117 | return this; 118 | } 119 | 120 | public String getTypeHandler() { 121 | return typeHandler; 122 | } 123 | 124 | public TableField setTypeHandler(String typeHandler) { 125 | this.typeHandler = typeHandler; 126 | return this; 127 | } 128 | 129 | public String getPropertyTypePackage() { 130 | return propertyTypePackage; 131 | } 132 | 133 | public TableField setPropertyTypePackage(String propertyTypePackage) { 134 | this.propertyTypePackage = propertyTypePackage; 135 | return this; 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/entity/TableInfo.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.entity; 2 | 3 | import com.example.generator.constant.ConstValue; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author panzhi 9 | * @Description 10 | * @since 2021-03-11 11 | */ 12 | public class TableInfo { 13 | 14 | /** 15 | * 表名称 16 | */ 17 | private String name; 18 | 19 | /** 20 | * 表注释 21 | */ 22 | private String comment; 23 | 24 | /** 25 | * 主键ID 26 | */ 27 | private String primaryId = ConstValue.TABLE_PRIMARY; 28 | 29 | /** 30 | * 各个类 31 | */ 32 | private String entityName; 33 | private String requestAddName; 34 | private String requestUpdateName; 35 | private String requestName; 36 | private String requestSearchName; 37 | private String dtoName; 38 | private String xmlName; 39 | private String daoName; 40 | private String serviceName; 41 | private String apiName; 42 | private String providerName; 43 | private String junitName; 44 | 45 | /** 46 | * 属性字段 47 | */ 48 | private List fields; 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public TableInfo setName(String name) { 55 | this.name = name; 56 | return this; 57 | } 58 | 59 | public String getComment() { 60 | return comment; 61 | } 62 | 63 | public TableInfo setComment(String comment) { 64 | this.comment = comment; 65 | return this; 66 | } 67 | 68 | public String getPrimaryId() { 69 | return primaryId; 70 | } 71 | 72 | public TableInfo setPrimaryId(String primaryId) { 73 | this.primaryId = primaryId; 74 | return this; 75 | } 76 | 77 | public String getEntityName() { 78 | return entityName; 79 | } 80 | 81 | public TableInfo setEntityName(String entityName) { 82 | this.entityName = entityName; 83 | return this; 84 | } 85 | 86 | public String getRequestAddName() { 87 | return requestAddName; 88 | } 89 | 90 | public TableInfo setRequestAddName(String requestAddName) { 91 | this.requestAddName = requestAddName; 92 | return this; 93 | } 94 | 95 | public String getRequestUpdateName() { 96 | return requestUpdateName; 97 | } 98 | 99 | public TableInfo setRequestUpdateName(String requestUpdateName) { 100 | this.requestUpdateName = requestUpdateName; 101 | return this; 102 | } 103 | 104 | public String getRequestName() { 105 | return requestName; 106 | } 107 | 108 | public TableInfo setRequestName(String requestName) { 109 | this.requestName = requestName; 110 | return this; 111 | } 112 | 113 | public String getRequestSearchName() { 114 | return requestSearchName; 115 | } 116 | 117 | public TableInfo setRequestSearchName(String requestSearchName) { 118 | this.requestSearchName = requestSearchName; 119 | return this; 120 | } 121 | 122 | public String getDtoName() { 123 | return dtoName; 124 | } 125 | 126 | public TableInfo setDtoName(String dtoName) { 127 | this.dtoName = dtoName; 128 | return this; 129 | } 130 | 131 | public String getXmlName() { 132 | return xmlName; 133 | } 134 | 135 | public TableInfo setXmlName(String xmlName) { 136 | this.xmlName = xmlName; 137 | return this; 138 | } 139 | 140 | public String getDaoName() { 141 | return daoName; 142 | } 143 | 144 | public TableInfo setDaoName(String daoName) { 145 | this.daoName = daoName; 146 | return this; 147 | } 148 | 149 | public String getServiceName() { 150 | return serviceName; 151 | } 152 | 153 | public TableInfo setServiceName(String serviceName) { 154 | this.serviceName = serviceName; 155 | return this; 156 | } 157 | 158 | public String getApiName() { 159 | return apiName; 160 | } 161 | 162 | public TableInfo setApiName(String apiName) { 163 | this.apiName = apiName; 164 | return this; 165 | } 166 | 167 | public String getProviderName() { 168 | return providerName; 169 | } 170 | 171 | public TableInfo setProviderName(String providerName) { 172 | this.providerName = providerName; 173 | return this; 174 | } 175 | 176 | public String getJunitName() { 177 | return junitName; 178 | } 179 | 180 | public TableInfo setJunitName(String junitName) { 181 | this.junitName = junitName; 182 | return this; 183 | } 184 | 185 | public List getFields() { 186 | return fields; 187 | } 188 | 189 | public TableInfo setFields(List fields) { 190 | this.fields = fields; 191 | return this; 192 | } 193 | } 194 | 195 | -------------------------------------------------------------------------------- /src/main/java/com/example/generator/util/AssetUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.util; 2 | 3 | import org.apache.commons.lang3.StringUtils; 4 | 5 | import java.util.Objects; 6 | 7 | /** 8 | * @author panzhi 9 | * @Description 10 | * @since 2021-03-11 11 | */ 12 | public class AssetUtil { 13 | 14 | public static void notEmpty(Object param, String message){ 15 | if(Objects.isNull(param)){ 16 | throw new RuntimeException(message); 17 | } 18 | } 19 | 20 | public static void notBlank(String param, String message){ 21 | if(StringUtils.isBlank(param)){ 22 | throw new RuntimeException(message); 23 | } 24 | } 25 | 26 | } -------------------------------------------------------------------------------- /src/main/java/com/example/generator/util/NamingStrategyHelper.java: -------------------------------------------------------------------------------- 1 | package com.example.generator.util; 2 | 3 | import com.example.generator.constant.StringPool; 4 | import org.apache.commons.lang3.StringUtils; 5 | 6 | import java.util.Arrays; 7 | 8 | /** 9 | * @author panzhi 10 | * @Description 11 | * @since 2021-03-11 12 | */ 13 | public class NamingStrategyHelper { 14 | 15 | /** 16 | * 去掉下划线前缀且将后半部分转成驼峰格式 17 | * @param name 18 | * @param tablePrefix 19 | * @return 20 | */ 21 | public static String removePrefixAndCamel(String name, String[] tablePrefix) { 22 | return underlineToCamel(removePrefix(name, tablePrefix)); 23 | } 24 | 25 | /** 26 | * 去掉指定的前缀 27 | * @param name 28 | * @param prefix 29 | * @return 30 | */ 31 | public static String removePrefix(String name, String... prefix) { 32 | if (StringUtils.isEmpty(name)) { 33 | return StringPool.EMPTY; 34 | } 35 | if (null != prefix) { 36 | // 判断是否有匹配的前缀,然后截取前缀 37 | // 删除前缀 38 | return Arrays.stream(prefix).filter(pf -> name.toLowerCase() 39 | .matches(StringPool.HAT + pf.toLowerCase() + ".*")) 40 | .findFirst().map(pf -> name.substring(pf.length())).orElse(name); 41 | } 42 | return name; 43 | } 44 | 45 | /** 46 | * 下划线转驼峰命名 47 | * @param name 48 | * @return 49 | */ 50 | public static String underlineToCamel(String name) { 51 | // 快速检查 52 | if (StringUtils.isEmpty(name)) { 53 | // 没必要转换 54 | return StringPool.EMPTY; 55 | } 56 | // 如果不包含下划线,首字母小些 57 | if (!StringUtils.contains(name, StringPool.UNDERLINE)) { 58 | return capitalFirstLower(name); 59 | } 60 | String tempName = name.toLowerCase(); 61 | StringBuilder result = new StringBuilder(); 62 | // 用下划线将原始字符串分割 63 | String[] camels = tempName.split(StringPool.UNDERLINE); 64 | // 跳过原始字符串中开头、结尾的下换线或双重下划线 65 | // 处理真正的驼峰片段 66 | Arrays.stream(camels).filter(camel -> StringUtils.isNotEmpty(camel)).forEach(camel -> { 67 | if (result.length() == 0) { 68 | // 第一个驼峰片段,全部字母都小写 69 | result.append(camel); 70 | } else { 71 | // 其他的驼峰片段,首字母大写 72 | result.append(capitalFirstUpper(camel)); 73 | } 74 | }); 75 | return result.toString(); 76 | } 77 | 78 | /** 79 | * 实体首字母大写 80 | * @param name 81 | * @return 82 | */ 83 | public static String capitalFirstUpper(String name) { 84 | if (StringUtils.isNotEmpty(name)) { 85 | return name.substring(0, 1).toUpperCase() + name.substring(1); 86 | } 87 | return StringPool.EMPTY; 88 | } 89 | 90 | /** 91 | * 实体首字母小写 92 | * @param name 93 | * @return 94 | */ 95 | public static String capitalFirstLower(String name) { 96 | if (StringUtils.isNotEmpty(name)) { 97 | return name.substring(0, 1).toLowerCase() + name.substring(1); 98 | } 99 | return StringPool.EMPTY; 100 | } 101 | 102 | 103 | } 104 | 105 | -------------------------------------------------------------------------------- /src/main/resources/templates/api.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.api}; 2 | 3 | import com.yijiupi.basic.request.BaseResponse; 4 | import com.yijiupi.basic.request.IdRequest; 5 | import com.yijiupi.basic.utils.PageUtils; 6 | import java.util.List; 7 | import ${package.request}.${requestSearchClass}; 8 | <#if isRequestAddOrUpdateEnable> 9 | import ${package.request}.${requestAddClass}; 10 | import ${package.request}.${requestUpdateClass}; 11 | <#else> 12 | import ${package.request}.${requestClass}; 13 | 14 | import ${package.dto}.${dtoClass}; 15 | 16 | /** 17 | *

18 | * ${entityClass} Api层 19 | *

20 | * 21 | * @author ${author} 22 | * @since ${date} 23 | */ 24 | public interface ${apiClass} { 25 | 26 | /** 27 | * 分页列表 28 | * @param request 29 | * @return 30 | */ 31 | BaseResponse> selectPage(${requestSearchClass} request); 32 | 33 | /** 34 | * 查询数据集(下拉框) 35 | * @param request 36 | * @return 37 | */ 38 | BaseResponse> selectAll(${requestSearchClass} request); 39 | 40 | /** 41 | * 查询详情 42 | * @param request 43 | * @return 44 | */ 45 | BaseResponse<${dtoClass}> getDetail(IdRequest request); 46 | 47 | /** 48 | * 新增操作 49 | * @param request 50 | * @return 51 | */ 52 | <#if isRequestAddOrUpdateEnable> 53 | BaseResponse add(${requestAddClass} request); 54 | <#else> 55 | BaseResponse add(${requestClass} request); 56 | 57 | 58 | /** 59 | * 编辑操作 60 | * @param request 61 | * @return 62 | */ 63 | <#if isRequestAddOrUpdateEnable> 64 | BaseResponse edit(${requestUpdateClass} request); 65 | <#else> 66 | BaseResponse edit(${requestClass} request); 67 | 68 | 69 | /** 70 | * 删除操作 71 | * @param request 72 | * @return 73 | */ 74 | BaseResponse delete(IdRequest request); 75 | } 76 | -------------------------------------------------------------------------------- /src/main/resources/templates/dao.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.dao}; 2 | 3 | import java.util.List; 4 | 5 | import ${superDaoClassPackage}; 6 | import ${package.entity}.${entityClass}; 7 | import ${package.request}.${requestSearchClass}; 8 | 9 | /** 10 | *

11 | * ${entityClass} 数据操作层 12 | *

13 | * 14 | * @author ${author} 15 | * @since ${date} 16 | */ 17 | public interface ${daoClass} extends ${superDaoClass}<${entityClass}> { 18 | 19 | int countPage(${requestSearchClass} request); 20 | 21 | List<${entityClass}> selectPage(${requestSearchClass} request); 22 | } -------------------------------------------------------------------------------- /src/main/resources/templates/dto.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.dto}; 2 | 3 | import java.io.Serializable; 4 | 5 | import io.swagger.annotations.ApiModel; 6 | import io.swagger.annotations.ApiModelProperty; 7 | 8 | <#list fieldsImportPackage as importPackage> 9 | import ${importPackage}; 10 | 11 | 12 | <#if superDtoClass??> 13 | import ${superDtoClassPackage}; 14 | 15 | 16 | 17 | /** 18 | *

19 | * ${tableComment} 20 | *

21 | * 22 | * @author ${author} 23 | * @since ${date} 24 | */ 25 | @ApiModel 26 | <#if superDtoClass??> 27 | public class ${dtoClass} extends ${superDtoClass} implements Serializable { 28 | <#else> 29 | public class ${dtoClass} implements Serializable { 30 | 31 | 32 | private static final long serialVersionUID = 1L; 33 | 34 | <#--属性遍历--> 35 | <#list columns as pro> 36 | 37 | /** 38 | * ${pro.comment} 39 | */ 40 | @ApiModelProperty(value = "${pro.comment}") 41 | private ${pro.propertyType} ${pro.propertyName}; 42 | 43 | 44 | <#--属性get||set方法--> 45 | <#list columns as pro> 46 | public ${pro.propertyType} get${pro.propertyName?cap_first}() { 47 | return this.${pro.propertyName}; 48 | } 49 | 50 | public ${dtoClass} set${pro.propertyName?cap_first}(${pro.propertyType} ${pro.propertyName}) { 51 | this.${pro.propertyName} = ${pro.propertyName}; 52 | return this; 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/resources/templates/dubbo.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.provider}; 2 | 3 | import com.alibaba.dubbo.config.annotation.Service; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import com.yijiupi.basic.acpect.DubboConfiguration; 6 | import com.yijiupi.basic.request.BaseResponse; 7 | import com.yijiupi.basic.request.IdRequest; 8 | import com.yijiupi.basic.utils.PageUtils; 9 | import java.util.List; 10 | import io.swagger.annotations.Api; 11 | import io.swagger.annotations.ApiOperation; 12 | 13 | import ${package.api}.${apiClass}; 14 | import ${package.service}.${serviceClass}; 15 | import ${package.request}.${requestSearchClass}; 16 | <#if isRequestAddOrUpdateEnable> 17 | import ${package.request}.${requestAddClass}; 18 | import ${package.request}.${requestUpdateClass}; 19 | <#else> 20 | import ${package.request}.${requestClass}; 21 | 22 | import ${package.dto}.${dtoClass}; 23 | 24 | /** 25 | *

26 | * ${entityClass} Api实现层 27 | *

28 | * 29 | * @author ${author} 30 | * @since ${date} 31 | */ 32 | @DubboConfiguration 33 | @Service 34 | @Api(value = "${tableComment}") 35 | public class ${providerClass} implements ${apiClass} { 36 | 37 | @Autowired 38 | private ${serviceClass} ${serviceClass?uncap_first}; 39 | 40 | @Override 41 | @ApiOperation(value = "查询分页" , notes="权限编码:${projectModuleCode}:${apiClass}:selectPage") 42 | public BaseResponse> selectPage(${requestSearchClass} request){ 43 | PageUtils<${dtoClass}> pageUtils = ${serviceClass?uncap_first}.selectPage(request); 44 | return BaseResponse.getSuccessResponse(pageUtils); 45 | } 46 | 47 | @Override 48 | @ApiOperation(value = "下拉框查询" , notes="权限编码:${projectModuleCode}:${apiClass}:selectAll") 49 | public BaseResponse> selectAll(${requestSearchClass} request){ 50 | List<${dtoClass}> resultList = ${serviceClass?uncap_first}.selectAll(request); 51 | return BaseResponse.getSuccessResponse(resultList); 52 | } 53 | 54 | @Override 55 | @ApiOperation(value = "获取详情" , notes="权限编码:${projectModuleCode}:${apiClass}:getDetail") 56 | public BaseResponse<${dtoClass}> getDetail(IdRequest request){ 57 | ${dtoClass} result = ${serviceClass?uncap_first}.getDetail(request); 58 | return BaseResponse.getSuccessResponse(result); 59 | } 60 | 61 | // @SubmitToken 62 | @Override 63 | @ApiOperation(value = "新增" , notes="权限编码:${projectModuleCode}:${apiClass}:add") 64 | <#if isRequestAddOrUpdateEnable> 65 | public BaseResponse add(${requestAddClass} request){ 66 | <#else> 67 | public BaseResponse add(${requestClass} request){ 68 | 69 | ${serviceClass?uncap_first}.add(request); 70 | return BaseResponse.getSuccessResponse(); 71 | } 72 | 73 | // @SubmitToken 74 | @Override 75 | @ApiOperation(value = "修改" , notes="权限编码:${projectModuleCode}:${apiClass}:edit") 76 | <#if isRequestAddOrUpdateEnable> 77 | public BaseResponse edit(${requestUpdateClass} request){ 78 | <#else> 79 | public BaseResponse edit(${requestClass} request){ 80 | 81 | ${serviceClass?uncap_first}.update(request); 82 | return BaseResponse.getSuccessResponse(); 83 | } 84 | 85 | // @SubmitToken 86 | @Override 87 | @ApiOperation(value = "删除" , notes="权限编码:${projectModuleCode}:${apiClass}:delete") 88 | public BaseResponse delete(IdRequest request){ 89 | ${serviceClass?uncap_first}.delete(request); 90 | return BaseResponse.getSuccessResponse(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/resources/templates/entity.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.entity}; 2 | 3 | import java.io.Serializable; 4 | 5 | <#list fieldsImportPackage as importPackage> 6 | import ${importPackage}; 7 | 8 | 9 | <#if superEntityClass??> 10 | import ${superEntityClassPackage}; 11 | 12 | 13 | 14 | /** 15 | *

16 | * ${tableComment} 17 | *

18 | * 19 | * @author ${author} 20 | * @since ${date} 21 | */ 22 | <#if superEntityClass??> 23 | public class ${entityClass} extends ${superEntityClass} implements Serializable { 24 | <#else> 25 | public class ${entityClass} implements Serializable { 26 | 27 | 28 | private static final long serialVersionUID = 1L; 29 | 30 | <#--属性遍历--> 31 | <#list columns as pro> 32 | 33 | /** 34 | * ${pro.comment} 35 | */ 36 | private ${pro.propertyType} ${pro.propertyName}; 37 | 38 | 39 | <#--属性get||set方法--> 40 | <#list columns as pro> 41 | public ${pro.propertyType} get${pro.propertyName?cap_first}() { 42 | return this.${pro.propertyName}; 43 | } 44 | 45 | public ${entityClass} set${pro.propertyName?cap_first}(${pro.propertyType} ${pro.propertyName}) { 46 | this.${pro.propertyName} = ${pro.propertyName}; 47 | return this; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/resources/templates/junit.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.junit}; 2 | 3 | import org.junit.Test; 4 | import org.junit.runner.RunWith; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.boot.test.context.SpringBootTest; 7 | import org.springframework.test.context.junit4.SpringRunner; 8 | import org.springframework.test.context.ActiveProfiles; 9 | import com.yijiupi.basic.generator.menu.MenuJsonGenerator; 10 | import com.yijiupi.basic.yapi.SwaggerGenerator; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | import java.util.*; 14 | 15 | import com.yijiupi.basic.request.BaseResponse; 16 | import com.yijiupi.basic.request.IdRequest; 17 | import com.yijiupi.basic.utils.JacksonUtils; 18 | import ${package.api}.${apiClass}; 19 | import ${package.provider}.${providerClass}; 20 | import ${package.request}.${requestSearchClass}; 21 | <#if isRequestAddOrUpdateEnable> 22 | import ${package.request}.${requestAddClass}; 23 | import ${package.request}.${requestUpdateClass}; 24 | <#else> 25 | import ${package.request}.${requestClass}; 26 | 27 | 28 | 29 | /** 30 | *

31 | * ${entityClass} 单元测试(请将当前类移动到src/test/java下) 32 | *

33 | * 34 | * @author ${author} 35 | * @since ${date} 36 | */ 37 | @ActiveProfiles("dev") 38 | @RunWith(SpringRunner.class) 39 | @SpringBootTest 40 | public class ${junitClass} { 41 | 42 | private static final Logger log = LoggerFactory.getLogger(${junitClass}.class); 43 | 44 | //公共参数 45 | private static String loginUserId = ""; 46 | private static String loginUserName = ""; 47 | private static String submitToken = ""; 48 | private static String projectModuleCode = ""; 49 | 50 | @Autowired 51 | private ${apiClass} ${apiClass?uncap_first}; 52 | 53 | @Test 54 | public void selectPage(){ 55 | ${requestSearchClass} request = new ${requestSearchClass}(); 56 | //公共参数 57 | request.setLoginUserId(loginUserId); 58 | request.setLoginUserName(loginUserName); 59 | request.setPage(1); 60 | request.setPageSize(20); 61 | 62 | //业务参数... 63 | 64 | log.info("请求参数:{}", JacksonUtils.toJson(request)); 65 | BaseResponse baseResponse = ${apiClass?uncap_first}.selectPage(request); 66 | log.info("返回参数:{}", JacksonUtils.toJson(baseResponse)); 67 | } 68 | 69 | @Test 70 | public void selectAll(){ 71 | ${requestSearchClass} request = new ${requestSearchClass}(); 72 | //公共参数 73 | request.setLoginUserId(loginUserId); 74 | request.setLoginUserName(loginUserName); 75 | request.setPage(1); 76 | request.setPageSize(20); 77 | 78 | //业务参数... 79 | 80 | log.info("请求参数:{}", JacksonUtils.toJson(request)); 81 | BaseResponse baseResponse = ${apiClass?uncap_first}.selectAll(request); 82 | log.info("返回参数:{}", JacksonUtils.toJson(baseResponse)); 83 | } 84 | 85 | @Test 86 | public void getDetail(){ 87 | IdRequest request = new IdRequest(); 88 | //公共参数 89 | request.setLoginUserId(loginUserId); 90 | request.setLoginUserName(loginUserName); 91 | 92 | //业务参数... 93 | 94 | log.info("请求参数:{}", JacksonUtils.toJson(request)); 95 | BaseResponse baseResponse = ${apiClass?uncap_first}.getDetail(request); 96 | log.info("返回参数:{}", JacksonUtils.toJson(baseResponse)); 97 | } 98 | 99 | @Test 100 | public void add(){ 101 | <#if isRequestAddOrUpdateEnable> 102 | ${requestAddClass} request = new ${requestAddClass}(); 103 | <#else> 104 | ${requestClass} request = new ${requestClass}(); 105 | 106 | //公共参数 107 | request.setLoginUserId(loginUserId); 108 | request.setLoginUserName(loginUserName); 109 | request.setSubmitToken(submitToken); 110 | 111 | //业务参数... 112 | 113 | log.info("请求参数:{}", JacksonUtils.toJson(request)); 114 | BaseResponse baseResponse = ${apiClass?uncap_first}.add(request); 115 | log.info("返回参数:{}", JacksonUtils.toJson(baseResponse)); 116 | } 117 | 118 | @Test 119 | public void edit(){ 120 | <#if isRequestAddOrUpdateEnable> 121 | ${requestUpdateClass} request = new ${requestUpdateClass}(); 122 | <#else> 123 | ${requestClass} request = new ${requestClass}(); 124 | 125 | //公共参数 126 | request.setLoginUserId(loginUserId); 127 | request.setLoginUserName(loginUserName); 128 | request.setSubmitToken(submitToken); 129 | 130 | //业务参数... 131 | 132 | log.info("请求参数:{}", JacksonUtils.toJson(request)); 133 | BaseResponse baseResponse = ${apiClass?uncap_first}.edit(request); 134 | log.info("返回参数:{}", JacksonUtils.toJson(baseResponse)); 135 | } 136 | 137 | @Test 138 | public void delete(){ 139 | IdRequest request = new IdRequest(); 140 | //公共参数 141 | request.setLoginUserId(loginUserId); 142 | request.setLoginUserName(loginUserName); 143 | request.setSubmitToken(submitToken); 144 | 145 | //业务参数... 146 | 147 | log.info("请求参数:{}", JacksonUtils.toJson(request)); 148 | BaseResponse baseResponse = ${apiClass?uncap_first}.delete(request); 149 | log.info("返回参数:{}", JacksonUtils.toJson(baseResponse)); 150 | } 151 | 152 | @Test 153 | public void createYApiDocument(){ 154 | //生成接口文档 155 | String yApiToken = ""; 156 | SwaggerGenerator.generateAndImport(projectModuleCode, yApiToken, ${providerClass}.class); 157 | } 158 | 159 | @Test 160 | public void createMenuJsonDocument(){ 161 | //生成菜单Json文档 162 | String resourceName = "菜单名称";//一级菜单名称 163 | String frontUrl = "/hello.html";//一级菜单路径 164 | MenuJsonGenerator.createMenuJson(projectModuleCode, resourceName, frontUrl, ${providerClass}.class); 165 | } 166 | 167 | } 168 | -------------------------------------------------------------------------------- /src/main/resources/templates/mapper.xml.ftl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | <#list columns as pro> 9 | <#if pro.propertyName == primaryId> 10 | 11 | <#else> 12 | <#if pro.isTypeHandler> 13 | 15 | <#else> 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | <#list columns as pro> 25 | <#if pro_index == 0> 26 | `${pro.fieldName}` 27 | <#else> 28 | ,`${pro.fieldName}` 29 | 30 | 31 | 32 | 33 | 34 | 35 | insert into `${tableName}` ( 36 | <#list columns as pro> 37 | <#if pro_index == 0> 38 | `${pro.fieldName}` 39 | <#elseif pro_index == 1> 40 | ,`${pro.fieldName}` 41 | <#else> 42 | ,`${pro.fieldName}` 43 | 44 | 45 | ) 46 | values 47 | 48 | 49 | <#list columns as pro> 50 | <#if pro.isTypeHandler> 51 | ${r"#{obj." + pro.propertyName + r",jdbcType=" + pro.fieldType + r",typeHandler=" + pro.typeHandler + r"}"}, 52 | <#else> 53 | ${r"#{obj." + pro.propertyName + r"}"}, 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | insert into `${tableName}` 63 | 64 | <#list columns as pro> 65 | 66 | `${pro.fieldName}`, 67 | 68 | 69 | 70 | 71 | <#list columns as pro> 72 | 73 | <#if pro.isTypeHandler> 74 | ${r"#{" + pro.propertyName + r",jdbcType=" + pro.fieldType + r",typeHandler=" + pro.typeHandler + r"}"}, 75 | <#else> 76 | ${r"#{" + pro.propertyName + r",jdbcType=" + pro.fieldType + r"}"}, 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | update `${tableName}` 86 | 87 | <#list columns as pro> 88 | <#if pro.fieldName != primaryId> 89 | 90 | <#if pro.isTypeHandler> 91 | `${pro.fieldName}` = ${r"#{" + pro.propertyName + r",jdbcType=" + pro.fieldType + r",typeHandler=" + pro.typeHandler + r"}"}, 92 | <#else> 93 | `${pro.fieldName}` = ${r"#{" + pro.propertyName + r",jdbcType=" + pro.fieldType +r"}"}, 94 | 95 | 96 | 97 | 98 | 99 | where ${primaryId} = ${r"#{" + "${primaryId}" + r",jdbcType=BIGINT}"} 100 | 101 | 102 | 103 | 104 | update `${tableName}` 105 | 106 | <#list columns as pro> 107 | <#if pro.fieldName != primaryId && pro.fieldName != primaryId> 108 | 109 | 110 | 111 | <#if pro.isTypeHandler> 112 | when id = ${r"#{" + "obj.id" + r"}"} 113 | then ${r"#{obj." + pro.propertyName + r",jdbcType=" + pro.fieldType + r",typeHandler=" + pro.typeHandler + r"}"} 114 | <#else> 115 | when id = ${r"#{" + "obj.id" + r"}"} 116 | then ${r"#{obj." + pro.propertyName + r",jdbcType=" + pro.fieldType + r"}"} 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | where 125 | 126 | id = ${r"#{" + "obj.id" + r"}"} 127 | 128 | 129 | 130 | 131 | 132 | delete from `${tableName}` 133 | where `${primaryId}` = ${r"#{" + "${primaryId}" + r",jdbcType=BIGINT}"} 134 | 135 | 136 | 137 | 143 | 144 | 145 | 152 | 153 | 154 | 168 | 169 | 170 | 176 | 177 | 178 | 183 | 184 | 185 | 195 | 196 | 197 | 198 | 199 | <#-- <#list columns as pro>--> 200 | <#-- <#if pro.propertyName != 'isDelete'--> 201 | <#-- && pro.propertyName != 'createUserid'--> 202 | <#-- && pro.propertyName != 'createUserName'--> 203 | <#-- && pro.propertyName != 'createTime'--> 204 | <#-- && pro.propertyName != 'createtime'--> 205 | <#-- && pro.propertyName != 'updateUserid'--> 206 | <#-- && pro.propertyName != 'updateUserName'--> 207 | <#-- && pro.propertyName != 'updateTime'--> 208 | <#-- && pro.propertyName != 'lastupdatetime'--> 209 | <#-- >--> 210 | <#-- --> 211 | <#-- and ${pro.fieldName} = ${r"#{" + pro.propertyName + r",jdbcType=" + pro.fieldType +r"}"}--> 212 | <#-- --> 213 | <#-- --> 214 | <#-- --> 215 | 216 | 217 | 218 | 219 | 220 | 221 | <#-- <#list columns as pro>--> 222 | <#-- <#if pro.propertyName != 'isDelete'--> 223 | <#-- && pro.propertyName != 'createUserid'--> 224 | <#-- && pro.propertyName != 'createUserName'--> 225 | <#-- && pro.propertyName != 'createTime'--> 226 | <#-- && pro.propertyName != 'createtime'--> 227 | <#-- && pro.propertyName != 'updateUserid'--> 228 | <#-- && pro.propertyName != 'updateUserName'--> 229 | <#-- && pro.propertyName != 'updateTime'--> 230 | <#-- && pro.propertyName != 'lastupdatetime'--> 231 | <#-- && pro.propertyName != primaryId--> 232 | <#-- >--> 233 | <#-- --> 234 | <#-- and ${pro.fieldName} = ${r"#{" + pro.propertyName + r",jdbcType=" + pro.fieldType +r"}"}--> 235 | <#-- --> 236 | <#-- --> 237 | <#-- --> 238 | 239 | 240 | 241 | 242 | -------------------------------------------------------------------------------- /src/main/resources/templates/provider.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.provider}; 2 | 3 | import com.alibaba.dubbo.config.annotation.Service; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import com.yijiupi.basic.acpect.DubboConfiguration; 6 | import com.yijiupi.basic.request.BaseResponse; 7 | import com.yijiupi.basic.request.IdRequest; 8 | import com.yijiupi.basic.utils.PageUtils; 9 | import java.util.List; 10 | import io.swagger.annotations.Api; 11 | import io.swagger.annotations.ApiOperation; 12 | 13 | import ${package.api}.${apiClass}; 14 | import ${package.service}.${serviceClass}; 15 | import ${package.request}.${requestSearchClass}; 16 | <#if isRequestAddOrUpdateEnable> 17 | import ${package.request}.${requestAddClass}; 18 | import ${package.request}.${requestUpdateClass}; 19 | <#else> 20 | import ${package.request}.${requestClass}; 21 | 22 | import ${package.dto}.${dtoClass}; 23 | 24 | /** 25 | *

26 | * ${entityClass} Api实现层 27 | *

28 | * 29 | * @author ${author} 30 | * @since ${date} 31 | */ 32 | @DubboConfiguration 33 | @Service 34 | @Api(value = "${tableComment}") 35 | public class ${providerClass} implements ${apiClass} { 36 | 37 | @Autowired 38 | private ${serviceClass} ${serviceClass?uncap_first}; 39 | 40 | @Override 41 | @ApiOperation(value = "查询分页" , notes="权限编码:${projectModuleCode}:${apiClass}:selectPage") 42 | public BaseResponse> selectPage(${requestSearchClass} request){ 43 | PageUtils<${dtoClass}> pageUtils = ${serviceClass?uncap_first}.selectPage(request); 44 | return BaseResponse.getSuccessResponse(pageUtils); 45 | } 46 | 47 | @Override 48 | @ApiOperation(value = "下拉框查询" , notes="权限编码:${projectModuleCode}:${apiClass}:selectAll") 49 | public BaseResponse> selectAll(${requestSearchClass} request){ 50 | List<${dtoClass}> resultList = ${serviceClass?uncap_first}.selectAll(request); 51 | return BaseResponse.getSuccessResponse(resultList); 52 | } 53 | 54 | @Override 55 | @ApiOperation(value = "获取详情" , notes="权限编码:${projectModuleCode}:${apiClass}:getDetail") 56 | public BaseResponse<${dtoClass}> getDetail(IdRequest request){ 57 | ${dtoClass} result = ${serviceClass?uncap_first}.getDetail(request); 58 | return BaseResponse.getSuccessResponse(result); 59 | } 60 | 61 | // @SubmitToken 62 | @Override 63 | @ApiOperation(value = "新增" , notes="权限编码:${projectModuleCode}:${apiClass}:add") 64 | <#if isRequestAddOrUpdateEnable> 65 | public BaseResponse add(${requestAddClass} request){ 66 | <#else> 67 | public BaseResponse add(${requestClass} request){ 68 | 69 | ${serviceClass?uncap_first}.add(request); 70 | return BaseResponse.getSuccessResponse(); 71 | } 72 | 73 | // @SubmitToken 74 | @Override 75 | @ApiOperation(value = "修改" , notes="权限编码:${projectModuleCode}:${apiClass}:edit") 76 | <#if isRequestAddOrUpdateEnable> 77 | public BaseResponse edit(${requestUpdateClass} request){ 78 | <#else> 79 | public BaseResponse edit(${requestClass} request){ 80 | 81 | ${serviceClass?uncap_first}.update(request); 82 | return BaseResponse.getSuccessResponse(); 83 | } 84 | 85 | // @SubmitToken 86 | @Override 87 | @ApiOperation(value = "删除" , notes="权限编码:${projectModuleCode}:${apiClass}:delete") 88 | public BaseResponse delete(IdRequest request){ 89 | ${serviceClass?uncap_first}.delete(request); 90 | return BaseResponse.getSuccessResponse(); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/main/resources/templates/request.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.request}; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.validation.constraints.NotBlank; 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | 9 | import io.swagger.annotations.ApiModel; 10 | import io.swagger.annotations.ApiModelProperty; 11 | 12 | <#list fieldsImportPackage as importPackage> 13 | import ${importPackage}; 14 | 15 | 16 | import ${superRequestClassPackage}; 17 | 18 | 19 | /** 20 | *

21 | * ${tableComment} 22 | *

23 | * 24 | * @author ${author} 25 | * @since ${date} 26 | */ 27 | @ApiModel 28 | public class ${requestClass} extends ${superRequestClass} implements Serializable { 29 | 30 | private static final long serialVersionUID = 1L; 31 | 32 | <#--属性遍历--> 33 | <#list columns as pro> 34 | <#if pro.propertyName != 'isDelete' 35 | && pro.propertyName != 'createUserid' 36 | && pro.propertyName != 'createUserName' 37 | && pro.propertyName != 'createTime' 38 | && pro.propertyName != 'createtime' 39 | && pro.propertyName != 'updateUserid' 40 | && pro.propertyName != 'updateUserName' 41 | && pro.propertyName != 'updateTime' 42 | && pro.propertyName != 'lastupdatetime' 43 | > 44 | 45 | /** 46 | * ${pro.comment} 47 | */ 48 | @ApiModelProperty(value = "${pro.comment}") 49 | <#if pro.propertyName != primaryId && pro.propertyType == 'String'> 50 | @NotBlank(message = "${pro.comment}" + notBlankMsg) 51 | <#if (pro.fieldLength < 1000)> 52 | @Size(max = ${pro.fieldLength}, message = "${pro.propertyName}最大长度不得超过${pro.fieldLength}") 53 | 54 | <#elseif pro.propertyName != primaryId && pro.propertyType != 'String'> 55 | @NotNull(message = "${pro.comment}" + notBlankMsg) 56 | <#else> 57 | 58 | private ${pro.propertyType} ${pro.propertyName}; 59 | 60 | 61 | 62 | <#--属性get||set方法--> 63 | <#list columns as pro> 64 | <#if pro.propertyName != 'isDelete' 65 | && pro.propertyName != 'createUserid' 66 | && pro.propertyName != 'createUserName' 67 | && pro.propertyName != 'createTime' 68 | && pro.propertyName != 'createtime' 69 | && pro.propertyName != 'updateUserid' 70 | && pro.propertyName != 'updateUserName' 71 | && pro.propertyName != 'updateTime' 72 | && pro.propertyName != 'lastupdatetime' 73 | > 74 | public ${pro.propertyType} get${pro.propertyName?cap_first}() { 75 | return this.${pro.propertyName}; 76 | } 77 | 78 | public ${requestClass} set${pro.propertyName?cap_first}(${pro.propertyType} ${pro.propertyName}) { 79 | <#if pro.propertyType == 'String'> 80 | this.${pro.propertyName} = ${pro.propertyName} == null ? null : ${pro.propertyName}.trim(); 81 | <#else> 82 | this.${pro.propertyName} = ${pro.propertyName}; 83 | 84 | return this; 85 | } 86 | 87 | 88 | } -------------------------------------------------------------------------------- /src/main/resources/templates/requestAdd.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.request}; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.validation.constraints.NotBlank; 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | 9 | import io.swagger.annotations.ApiModel; 10 | import io.swagger.annotations.ApiModelProperty; 11 | 12 | <#list fieldsImportPackage as importPackage> 13 | import ${importPackage}; 14 | 15 | 16 | import ${superRequestClassPackage}; 17 | 18 | 19 | /** 20 | *

21 | * ${tableComment} 22 | *

23 | * 24 | * @author ${author} 25 | * @since ${date} 26 | */ 27 | @ApiModel 28 | public class ${requestAddClass} extends ${superRequestClass} implements Serializable { 29 | 30 | private static final long serialVersionUID = 1L; 31 | 32 | <#--属性遍历--> 33 | <#list columns as pro> 34 | <#if pro.propertyName != 'isDelete' 35 | && pro.propertyName != 'createUserid' 36 | && pro.propertyName != 'createUserName' 37 | && pro.propertyName != 'createTime' 38 | && pro.propertyName != 'createtime' 39 | && pro.propertyName != 'updateUserid' 40 | && pro.propertyName != 'updateUserName' 41 | && pro.propertyName != 'updateTime' 42 | && pro.propertyName != 'lastupdatetime' 43 | && pro.propertyName != primaryId 44 | > 45 | 46 | /** 47 | * ${pro.comment} 48 | */ 49 | @ApiModelProperty(value = "${pro.comment}") 50 | <#if pro.propertyType == 'String'> 51 | @NotBlank(message = "${pro.comment}" + notBlankMsg) 52 | <#if (pro.fieldLength < 1000)> 53 | @Size(max = ${pro.fieldLength}, message = "${pro.propertyName}最大长度不得超过${pro.fieldLength}") 54 | 55 | <#else> 56 | @NotNull(message = "${pro.comment}" + notBlankMsg) 57 | 58 | private ${pro.propertyType} ${pro.propertyName}; 59 | 60 | 61 | 62 | <#--属性get||set方法--> 63 | <#list columns as pro> 64 | <#if pro.propertyName != 'isDelete' 65 | && pro.propertyName != 'createUserid' 66 | && pro.propertyName != 'createUserName' 67 | && pro.propertyName != 'createTime' 68 | && pro.propertyName != 'createtime' 69 | && pro.propertyName != 'updateUserid' 70 | && pro.propertyName != 'updateUserName' 71 | && pro.propertyName != 'updateTime' 72 | && pro.propertyName != 'lastupdatetime' 73 | && pro.propertyName != primaryId 74 | > 75 | public ${pro.propertyType} get${pro.propertyName?cap_first}() { 76 | return this.${pro.propertyName}; 77 | } 78 | 79 | public ${requestAddClass} set${pro.propertyName?cap_first}(${pro.propertyType} ${pro.propertyName}) { 80 | <#if pro.propertyType == 'String'> 81 | this.${pro.propertyName} = ${pro.propertyName} == null ? null : ${pro.propertyName}.trim(); 82 | <#else> 83 | this.${pro.propertyName} = ${pro.propertyName}; 84 | 85 | return this; 86 | } 87 | 88 | 89 | } -------------------------------------------------------------------------------- /src/main/resources/templates/requestSearch.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.request}; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.validation.constraints.NotBlank; 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | 9 | import io.swagger.annotations.ApiModel; 10 | import io.swagger.annotations.ApiModelProperty; 11 | 12 | <#list fieldsImportPackage as importPackage> 13 | import ${importPackage}; 14 | 15 | 16 | import ${superRequestClassPackage}; 17 | 18 | 19 | /** 20 | *

21 | * ${tableComment} 22 | *

23 | * 24 | * @author ${author} 25 | * @since ${date} 26 | */ 27 | @ApiModel 28 | public class ${requestSearchClass} extends ${superRequestClass} implements Serializable { 29 | 30 | private static final long serialVersionUID = 1L; 31 | 32 | <#--属性遍历--> 33 | <#list columns as pro> 34 | <#if pro.propertyName != 'isDelete' 35 | && pro.propertyName != 'createUserid' 36 | && pro.propertyName != 'createUserName' 37 | && pro.propertyName != 'createTime' 38 | && pro.propertyName != 'createtime' 39 | && pro.propertyName != 'updateUserid' 40 | && pro.propertyName != 'updateUserName' 41 | && pro.propertyName != 'updateTime' 42 | && pro.propertyName != 'lastupdatetime' 43 | && pro.propertyName != primaryId 44 | > 45 | 46 | /** 47 | * ${pro.comment} 48 | */ 49 | @ApiModelProperty(value = "${pro.comment}") 50 | private ${pro.propertyType} ${pro.propertyName}; 51 | 52 | 53 | 54 | <#--属性get||set方法--> 55 | <#list columns as pro> 56 | <#if pro.propertyName != 'isDelete' 57 | && pro.propertyName != 'createUserid' 58 | && pro.propertyName != 'createUserName' 59 | && pro.propertyName != 'createTime' 60 | && pro.propertyName != 'createtime' 61 | && pro.propertyName != 'updateUserid' 62 | && pro.propertyName != 'updateUserName' 63 | && pro.propertyName != 'updateTime' 64 | && pro.propertyName != 'lastupdatetime' 65 | && pro.propertyName != primaryId 66 | > 67 | public ${pro.propertyType} get${pro.propertyName?cap_first}() { 68 | return this.${pro.propertyName}; 69 | } 70 | 71 | public ${requestSearchClass} set${pro.propertyName?cap_first}(${pro.propertyType} ${pro.propertyName}) { 72 | <#if pro.propertyType == 'String'> 73 | this.${pro.propertyName} = ${pro.propertyName} == null ? null : ${pro.propertyName}.trim(); 74 | <#else> 75 | this.${pro.propertyName} = ${pro.propertyName}; 76 | 77 | return this; 78 | } 79 | 80 | 81 | } -------------------------------------------------------------------------------- /src/main/resources/templates/requestUpd.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.request}; 2 | 3 | import java.io.Serializable; 4 | 5 | import javax.validation.constraints.NotBlank; 6 | import javax.validation.constraints.NotNull; 7 | import javax.validation.constraints.Size; 8 | 9 | import io.swagger.annotations.ApiModel; 10 | import io.swagger.annotations.ApiModelProperty; 11 | 12 | <#list fieldsImportPackage as importPackage> 13 | import ${importPackage}; 14 | 15 | 16 | import ${superRequestClassPackage}; 17 | 18 | 19 | /** 20 | *

21 | * ${tableComment} 22 | *

23 | * 24 | * @author ${author} 25 | * @since ${date} 26 | */ 27 | @ApiModel 28 | public class ${requestUpdateClass} extends ${superRequestClass} implements Serializable { 29 | 30 | private static final long serialVersionUID = 1L; 31 | 32 | <#--属性遍历--> 33 | <#list columns as pro> 34 | <#if pro.propertyName != 'isDelete' 35 | && pro.propertyName != 'createUserid' 36 | && pro.propertyName != 'createUserName' 37 | && pro.propertyName != 'createTime' 38 | && pro.propertyName != 'createtime' 39 | && pro.propertyName != 'updateUserid' 40 | && pro.propertyName != 'updateUserName' 41 | && pro.propertyName != 'updateTime' 42 | && pro.propertyName != 'lastupdatetime' 43 | > 44 | 45 | /** 46 | * ${pro.comment} 47 | */ 48 | @ApiModelProperty(value = "${pro.comment}") 49 | <#if pro.propertyType == 'String'> 50 | @NotBlank(message = "${pro.comment}" + notBlankMsg) 51 | <#if (pro.fieldLength < 1000)> 52 | @Size(max = ${pro.fieldLength}, message = "${pro.propertyName}最大长度不得超过${pro.fieldLength}") 53 | 54 | <#else> 55 | @NotNull(message = "${pro.comment}" + notBlankMsg) 56 | 57 | private ${pro.propertyType} ${pro.propertyName}; 58 | 59 | 60 | 61 | <#--属性get||set方法--> 62 | <#list columns as pro> 63 | <#if pro.propertyName != 'isDelete' 64 | && pro.propertyName != 'createUserid' 65 | && pro.propertyName != 'createUserName' 66 | && pro.propertyName != 'createTime' 67 | && pro.propertyName != 'createtime' 68 | && pro.propertyName != 'updateUserid' 69 | && pro.propertyName != 'updateUserName' 70 | && pro.propertyName != 'updateTime' 71 | && pro.propertyName != 'lastupdatetime' 72 | > 73 | public ${pro.propertyType} get${pro.propertyName?cap_first}() { 74 | return this.${pro.propertyName}; 75 | } 76 | 77 | public ${requestUpdateClass} set${pro.propertyName?cap_first}(${pro.propertyType} ${pro.propertyName}) { 78 | <#if pro.propertyType == 'String'> 79 | this.${pro.propertyName} = ${pro.propertyName} == null ? null : ${pro.propertyName}.trim(); 80 | <#else> 81 | this.${pro.propertyName} = ${pro.propertyName}; 82 | 83 | return this; 84 | } 85 | 86 | 87 | } -------------------------------------------------------------------------------- /src/main/resources/templates/service.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.service}; 2 | 3 | import com.google.common.collect.Lists; 4 | import org.apache.commons.collections.CollectionUtils; 5 | import org.springframework.beans.BeanUtils; 6 | import org.springframework.stereotype.Service; 7 | import org.springframework.transaction.annotation.Transactional; 8 | import org.slf4j.Logger; 9 | import org.slf4j.LoggerFactory; 10 | import java.util.List; 11 | import java.util.Objects; 12 | import com.yijiupi.basic.utils.IdGenerator; 13 | import com.yijiupi.basic.utils.PageUtils; 14 | import com.yijiupi.basic.request.IdRequest; 15 | 16 | import ${superServiceClassPackage}; 17 | import ${package.dao}.${daoClass}; 18 | import ${package.entity}.${entityClass}; 19 | import ${package.request}.${requestSearchClass}; 20 | <#if isRequestAddOrUpdateEnable> 21 | import ${package.request}.${requestAddClass}; 22 | import ${package.request}.${requestUpdateClass}; 23 | <#else> 24 | import ${package.request}.${requestClass}; 25 | 26 | import ${package.dto}.${dtoClass}; 27 | 28 | 29 | /** 30 | *

31 | * ${entityClass} 服务层 32 | *

33 | * 34 | * @author ${author} 35 | * @since ${date} 36 | */ 37 | @Service 38 | public class ${serviceClass} extends ${superServiceClass}<${daoClass}, ${entityClass}> { 39 | 40 | private static final Logger log = LoggerFactory.getLogger(${serviceClass}.class); 41 | 42 | /** 43 | * 分页列表查询 44 | * @param request 45 | */ 46 | public PageUtils<${dtoClass}> selectPage(${requestSearchClass} request) { 47 | List<${dtoClass}> resultList = Lists.newArrayList(); 48 | int count = super.baseMapper.countPage(request); 49 | List<${entityClass}> dbList = count > 0 ? super.baseMapper.selectPage(request) : Lists.newArrayList(); 50 | if(CollectionUtils.isNotEmpty(dbList)){ 51 | dbList.forEach(source->{ 52 | ${dtoClass} target = new ${dtoClass}(); 53 | BeanUtils.copyProperties(source, target); 54 | resultList.add(target); 55 | }); 56 | } 57 | return new PageUtils<>(resultList, count, request); 58 | } 59 | 60 | /** 61 | * 获取数据集(下拉框) 62 | * @param request 63 | */ 64 | public List<${dtoClass}> selectAll(${requestSearchClass} request){ 65 | List<${dtoClass}> resultList = Lists.newArrayList(); 66 | List<${entityClass}> dbList = super.baseMapper.selectPage(request); 67 | if(CollectionUtils.isNotEmpty(dbList)){ 68 | dbList.forEach(source->{ 69 | ${dtoClass} result = new ${dtoClass}(); 70 | BeanUtils.copyProperties(source, result); 71 | resultList.add(result); 72 | }); 73 | } 74 | return resultList; 75 | } 76 | 77 | /** 78 | * 获取详情 79 | * @param request 80 | */ 81 | public ${dtoClass} getDetail(IdRequest request){ 82 | ${entityClass} entity = baseMapper.selectByPrimaryKey(request.getId()); 83 | if(Objects.nonNull(entity)){ 84 | ${dtoClass} result = new ${dtoClass}(); 85 | BeanUtils.copyProperties(entity, result); 86 | return result; 87 | } 88 | return null; 89 | } 90 | 91 | 92 | 93 | /** 94 | * 新增操作 95 | * @param request 96 | */ 97 | @Transactional(rollbackFor = Exception.class) 98 | <#if isRequestAddOrUpdateEnable> 99 | public void add(${requestAddClass} request){ 100 | <#else> 101 | public void add(${requestClass} request){ 102 | 103 | ${entityClass} entity = new ${entityClass}(); 104 | BeanUtils.copyProperties(request, entity); 105 | entity.setId(IdGenerator.getId(${entityClass}.class.getName())); 106 | baseMapper.insertPrimaryKeySelective(entity); 107 | } 108 | 109 | /** 110 | * 编辑操作 111 | * @param request 112 | */ 113 | @Transactional(rollbackFor = Exception.class) 114 | <#if isRequestAddOrUpdateEnable> 115 | public void update(${requestUpdateClass} request){ 116 | <#else> 117 | public void update(${requestClass} request){ 118 | 119 | ${entityClass} entity = new ${entityClass}(); 120 | BeanUtils.copyProperties(request, entity); 121 | baseMapper.updatePrimaryKeySelective(entity); 122 | } 123 | 124 | /** 125 | * 删除操作 126 | * @param request 127 | */ 128 | @Transactional(rollbackFor = Exception.class) 129 | public void delete(IdRequest request){ 130 | baseMapper.deleteByPrimaryKey(request.getId()); 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /src/main/resources/templates/web.java.ftl: -------------------------------------------------------------------------------- 1 | package ${package.provider}; 2 | 3 | import com.alibaba.dubbo.config.annotation.Service; 4 | import org.springframework.beans.factory.annotation.Autowired; 5 | import com.yijiupi.basic.acpect.DubboConfiguration; 6 | import com.yijiupi.basic.request.BaseResponse; 7 | import com.yijiupi.basic.request.IdRequest; 8 | import com.yijiupi.basic.utils.PageUtils; 9 | import java.util.List; 10 | import io.swagger.annotations.Api; 11 | import io.swagger.annotations.ApiOperation; 12 | 13 | import ${package.api}.${apiClass}; 14 | import ${package.service}.${serviceClass}; 15 | import ${package.request}.${requestSearchClass}; 16 | <#if isRequestAddOrUpdateEnable> 17 | import ${package.request}.${requestAddClass}; 18 | import ${package.request}.${requestUpdateClass}; 19 | <#else> 20 | import ${package.request}.${requestClass}; 21 | 22 | import ${package.dto}.${dtoClass}; 23 | 24 | /** 25 | *

26 | * ${entityClass} Api实现层 27 | *

28 | * 29 | * @author ${author} 30 | * @since ${date} 31 | */ 32 | @DubboConfiguration 33 | @Service 34 | @Api(value = "${tableComment}") 35 | public class ${providerClass} implements ${apiClass} { 36 | 37 | @Autowired 38 | private ${serviceClass} ${serviceClass?uncap_first}; 39 | 40 | @Override 41 | @ApiOperation(value = "查询分页" , notes="权限编码:${projectModuleCode}:${apiClass}:selectPage") 42 | public BaseResponse> selectPage(${requestSearchClass} request){ 43 | PageUtils<${dtoClass}> pageUtils = ${serviceClass?uncap_first}.selectPage(request); 44 | return BaseResponse.getSuccessResponse(pageUtils); 45 | } 46 | 47 | @Override 48 | @ApiOperation(value = "下拉框查询" , notes="权限编码:${projectModuleCode}:${apiClass}:selectAll") 49 | public BaseResponse> selectAll(${requestSearchClass} request){ 50 | List<${dtoClass}> resultList = ${serviceClass?uncap_first}.selectAll(request); 51 | return BaseResponse.getSuccessResponse(resultList); 52 | } 53 | 54 | @Override 55 | @ApiOperation(value = "获取详情" , notes="权限编码:${projectModuleCode}:${apiClass}:getDetail") 56 | public BaseResponse<${dtoClass}> getDetail(IdRequest request){ 57 | ${dtoClass} result = ${serviceClass?uncap_first}.getDetail(request); 58 | return BaseResponse.getSuccessResponse(result); 59 | } 60 | 61 | // @SubmitToken 62 | @Override 63 | @ApiOperation(value = "新增" , notes="权限编码:${projectModuleCode}:${apiClass}:add") 64 | <#if isRequestAddOrUpdateEnable> 65 | public BaseResponse add(${requestAddClass} request){ 66 | <#else> 67 | public BaseResponse add(${requestClass} request){ 68 | 69 | ${serviceClass?uncap_first}.add(request); 70 | return BaseResponse.getSuccessResponse(); 71 | } 72 | 73 | // @SubmitToken 74 | @Override 75 | @ApiOperation(value = "修改" , notes="权限编码:${projectModuleCode}:${apiClass}:edit") 76 | <#if isRequestAddOrUpdateEnable> 77 | public BaseResponse edit(${requestUpdateClass} request){ 78 | <#else> 79 | public BaseResponse edit(${requestClass} request){ 80 | 81 | ${serviceClass?uncap_first}.update(request); 82 | return BaseResponse.getSuccessResponse(); 83 | } 84 | 85 | // @SubmitToken 86 | @Override 87 | @ApiOperation(value = "删除" , notes="权限编码:${projectModuleCode}:${apiClass}:delete") 88 | public BaseResponse delete(IdRequest request){ 89 | ${serviceClass?uncap_first}.delete(request); 90 | return BaseResponse.getSuccessResponse(); 91 | } 92 | } 93 | --------------------------------------------------------------------------------