From 4d1c16756d684452e9aabf38a81c24ec4f063690 Mon Sep 17 00:00:00 2001 From: SimonSun Date: Sat, 11 Aug 2018 23:46:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8=E5=A2=9E=E5=BC=BA=EF=BC=8C=E8=8E=B7=E5=8F=96=E8=A1=A8?= =?UTF-8?q?=E6=A0=87=E6=B3=A8=EF=BC=9B=E4=BD=BF=E7=94=A8code-gen.propertie?= =?UTF-8?q?s=E9=85=8D=E7=BD=AE=E4=BB=A3=E7=A0=81=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8=EF=BC=9Bmysql=E5=BB=BA=E8=A1=A8sql=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/simon/common/code/CodeGenerator.java | 72 ++++++++++++++----- .../simon/common/code/EntityDataModel.java | 5 ++ .../common/code/FreeMarkerGeneratorUtil.java | 41 +++++++++-- src/main/resources/code-gen.properties | 15 ++++ src/main/resources/schema-mysql.sql | 7 +- src/main/resources/templates/code/entity.ftl | 6 +- src/test/java/com/simon/UtilTests.java | 23 ++++++ src/test/resources/test.properties | 3 + 8 files changed, 142 insertions(+), 30 deletions(-) create mode 100644 src/main/resources/code-gen.properties create mode 100644 src/test/resources/test.properties diff --git a/src/main/java/com/simon/common/code/CodeGenerator.java b/src/main/java/com/simon/common/code/CodeGenerator.java index 5e27fc6..55b9bd6 100644 --- a/src/main/java/com/simon/common/code/CodeGenerator.java +++ b/src/main/java/com/simon/common/code/CodeGenerator.java @@ -18,42 +18,76 @@ import java.util.*; */ public class CodeGenerator { - private static final String BASE_PACKAGE = "com.simon";//项目基础包名称,根据自己的项目修改 + private static String BASE_PACKAGE;//项目基础包名称,根据自己的项目修改 /*生成文件地址配置*/ - private static final String MODEL_PACKAGE = BASE_PACKAGE + ".model";//生成的Model类所在包 - private static final String MAPPER_PACKAGE = BASE_PACKAGE + ".mapper";//生成的Mapper所在包 - private static final String REPOSITORY_PACKAGE = BASE_PACKAGE + ".repository";//生成Repository所在包 - private static final String SERVICE_PACKAGE = BASE_PACKAGE + ".service";//生成的Service所在包 - private static final String SERVICE_IMPL_PACKAGE = BASE_PACKAGE + ".serviceImpl";//生成的ServiceImpl所在包 - private static final String CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller";//生成的Controller所在包 + private static String MODEL_PACKAGE;//生成的Model类所在包 + private static String MAPPER_PACKAGE;//生成的Mapper所在包 + private static String REPOSITORY_PACKAGE;//生成Repository所在包 + private static String SERVICE_PACKAGE;//生成的Service所在包 + private static String SERVICE_IMPL_PACKAGE;//生成的ServiceImpl所在包 + private static String CONTROLLER_PACKAGE;//生成的Controller所在包 - private static final String MAPPER_INTERFACE_REFERENCE = BASE_PACKAGE + ".common.mapper.MyMapper";//Mapper插件基础接口的完全限定名(第二步提到的核心继承接口Mapper) + private static String MAPPER_INTERFACE_REFERENCE;//Mapper插件基础接口的完全限定名(第二步提到的核心继承接口Mapper) /*数据库配置*/ /*private static final String JDBC_URL = "jdbc:postgresql://127.0.0.1:5432/thymeltetest?useUnicode=true&characterEncoding=UTF-8";//数据库url private static final String JDBC_USERNAME = "postgres"; private static final String JDBC_PASSWORD = "19961120"; private static final String JDBC_DIVER_CLASS_NAME = "org.postgresql.Driver";*/ - private static final String JDBC_URL = "jdbc:mysql://127.0.0.1:3306/thymelte?useUnicode=true&characterEncoding=utf-8&useSSL=false";//数据库url - private static final String JDBC_USERNAME = "root"; - private static final String JDBC_PASSWORD = "19941017"; - private static final String JDBC_DIVER_CLASS_NAME = "com.mysql.jdbc.Driver"; + private static String JDBC_URL;//数据库url + private static String JDBC_USERNAME; + private static String JDBC_PASSWORD; + private static String JDBC_DIVER_CLASS_NAME; private static final String PROJECT_PATH = System.getProperty("user.dir");//项目在硬盘上的基础路径 protected static final String TEMPLATE_FILE_PATH = PROJECT_PATH + "/src/main/resources/templates/code";//模板位置 - private static final String JAVA_PATH = "/src/test/java"; //java文件路径 - private static final String RESOURCES_PATH = "/src/test/resources";//资源文件路径 + private static String JAVA_PATH;//java文件路径 + private static String RESOURCES_PATH;//资源文件路径 - private static final String PACKAGE_PATH_REPOSITORY = packageConvertPath(REPOSITORY_PACKAGE); - private static final String PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE);//生成的Service存放路径 - private static final String PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE);//生成的Service实现存放路径 - private static final String PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE);//生成的Controller存放路径 + private static String PACKAGE_PATH_REPOSITORY; + private static String PACKAGE_PATH_SERVICE;//生成的Service存放路径 + private static String PACKAGE_PATH_SERVICE_IMPL;//生成的Service实现存放路径 + private static String PACKAGE_PATH_CONTROLLER;//生成的Controller存放路径 - protected static final String AUTHOR = "SimonSun";//@author + protected static String AUTHOR;//@author protected static final String CREATE = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());//@date + static { + Properties prop = new Properties(); + try { + prop.load(CodeGenerator.class.getResourceAsStream("/code-gen.properties")); + JDBC_URL = prop.getProperty("jdbc_url"); + JDBC_USERNAME = prop.getProperty("jdbc_username"); + JDBC_PASSWORD = prop.getProperty("jdbc_password"); + JDBC_DIVER_CLASS_NAME = prop.getProperty("jdbc_driver_class_name"); + AUTHOR = prop.getProperty("author"); + JAVA_PATH = prop.getProperty("java_path"); + RESOURCES_PATH = prop.getProperty("resources_path"); + BASE_PACKAGE = prop.getProperty("base_package"); + + MODEL_PACKAGE = BASE_PACKAGE + ".model"; + MAPPER_PACKAGE = BASE_PACKAGE + ".mapper"; + REPOSITORY_PACKAGE = BASE_PACKAGE + ".repository"; + SERVICE_PACKAGE = BASE_PACKAGE + ".service"; + SERVICE_IMPL_PACKAGE = BASE_PACKAGE + ".serviceImpl"; + CONTROLLER_PACKAGE = BASE_PACKAGE + ".controller"; + MAPPER_INTERFACE_REFERENCE = prop.getProperty("mapper_interface_reference"); + + PACKAGE_PATH_REPOSITORY = packageConvertPath(REPOSITORY_PACKAGE); + PACKAGE_PATH_SERVICE = packageConvertPath(SERVICE_PACKAGE); + PACKAGE_PATH_SERVICE_IMPL = packageConvertPath(SERVICE_IMPL_PACKAGE); + PACKAGE_PATH_CONTROLLER = packageConvertPath(CONTROLLER_PACKAGE); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public CodeGenerator(){ + + } + /*main函数入口,放入表名运行即可生成代码*/ public static void main(String[] args) { //genCode("users", "news_info"); diff --git a/src/main/java/com/simon/common/code/EntityDataModel.java b/src/main/java/com/simon/common/code/EntityDataModel.java index 10a9107..a98cb8e 100644 --- a/src/main/java/com/simon/common/code/EntityDataModel.java +++ b/src/main/java/com/simon/common/code/EntityDataModel.java @@ -46,6 +46,11 @@ public class EntityDataModel implements Serializable { */ private String tableName; + /** + * 表标注 + */ + private String tableComment; + /** * 字段集合 */ diff --git a/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java b/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java index 0616837..0c0088d 100644 --- a/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java +++ b/src/main/java/com/simon/common/code/FreeMarkerGeneratorUtil.java @@ -91,9 +91,36 @@ public class FreeMarkerGeneratorUtil { private static EntityDataModel getEntityModel(Connection con, String tableName, String basePackage, String modelName) throws Exception { + EntityDataModel dataModel = new EntityDataModel(); - //查询表属性,格式化生成实体所需属性 String sql = ""; + PreparedStatement ps; + ResultSet rs; + + //查询表标注 + if(getDataBaseType(con) == 1){ + sql = "SELECT TABLE_NAME,TABLE_COMMENT FROM information_schema.TABLES WHERE table_schema='" + con.getCatalog() + "' AND TABLE_NAME='" + tableName + "'"; + }else if(getDataBaseType(con) == 2){ + sql = "select obj_description('public." + tableName + "'::regclass)"; + }else{ + throw new Exception("暂不支持其他数据库"); + } + ps = con.prepareStatement(sql); + rs = ps.executeQuery(); + while (rs.next()){ + String tableComment = rs.getString("TABLE_COMMENT"); + if(StringUtils.isEmpty(tableComment)){ + tableComment = tableName; + }else{ + if(tableComment.lastIndexOf("表") == (tableComment.length() - 1)){ + tableComment = tableComment.substring(0, tableComment.length() - 1); + } + } + dataModel.setTableComment(tableComment); + } + + + //查询表属性,格式化生成实体所需属性 if(getDataBaseType(con) == 1){ //log.info(con.getCatalog()); sql = "SELECT table_name, column_name, column_comment, column_type, data_type, column_default, is_nullable " @@ -104,8 +131,8 @@ public class FreeMarkerGeneratorUtil { } - PreparedStatement ps = con.prepareStatement(sql); - ResultSet rs = ps.executeQuery(); + ps = con.prepareStatement(sql); + rs = ps.executeQuery(); List columns = new ArrayList<>(); while (rs.next()) { @@ -148,9 +175,11 @@ public class FreeMarkerGeneratorUtil { } }else{ if(isNullable.equalsIgnoreCase("NO")){ - annotation = "@Column(name = \"" + name + "\", nullable = false)"; + annotation = "@ApiModelProperty(value = \"" + comment + "\")\n" + + " @Column(name = \"" + name + "\", nullable = false)"; }else{ - annotation = "@Column(name = \"" + name + "\")"; + annotation = "@ApiModelProperty(value = \"" + comment + "\")\n" + + " @Column(name = \"" + name + "\")"; } } @@ -160,7 +189,7 @@ public class FreeMarkerGeneratorUtil { col.setComment(comment); columns.add(col); } - EntityDataModel dataModel = new EntityDataModel(); + dataModel.setEntityPackage(basePackage); if (StringUtils.isNotEmpty(modelName)) { dataModel.setEntityName(modelName); diff --git a/src/main/resources/code-gen.properties b/src/main/resources/code-gen.properties new file mode 100644 index 0000000..6f17f0c --- /dev/null +++ b/src/main/resources/code-gen.properties @@ -0,0 +1,15 @@ +# +jdbc_url=jdbc:mysql://127.0.0.1:3306/thymelte?useUnicode=true&characterEncoding=utf-8&useSSL=false +jdbc_username=root +jdbc_password=19941017 +jdbc_driver_class_name=com.mysql.jdbc.Driver +# ߣɵĴ@authorע +author=SimonSun +# ɵjavaļ洢· +java_path=/src/test/java +# ɵԴļ洢· +resources_path=/src/test/resources +# ĿƣԼĿ޸ +base_package=com.simon +# Mapperӿڵȫ޶(ڶᵽĺļ̳нӿMapper) +mapper_interface_reference=com.simon.common.mapper.MyMapper \ No newline at end of file diff --git a/src/main/resources/schema-mysql.sql b/src/main/resources/schema-mysql.sql index 85f09e3..520776b 100644 --- a/src/main/resources/schema-mysql.sql +++ b/src/main/resources/schema-mysql.sql @@ -117,13 +117,16 @@ tag_id bigint ); -- 两列唯一索引 -CREATE UNIQUE INDEX IF NOT EXISTS ix_auth_username ON authorities (user_id, authority); +set @x := (select count(*) from information_schema.statistics where table_name = 'authorities' and index_name = 'ix_auth_username' and table_schema = database()); +set @sql := if( @x > 0, 'select ''Index exists.''', 'CREATE UNIQUE INDEX ix_auth_username ON authorities(user_id, authority)'); +PREPARE stmt FROM @sql; +EXECUTE stmt; -- 添加外键 ALTER TABLE authorities ADD FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE NO ACTION ON UPDATE NO ACTION; -- 密码经过了加密,是secret ---INSERT INTO oauth_client_details VALUES ('clientIdPassword', 'oauth2-resource', '$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6', 'read,write,trust', 'password,authorization_code,refresh_token', null, 'ROLE_ADMIN,ROLE_USER', 7200, 5184000, null, 'false'); +-- INSERT INTO oauth_client_details VALUES ('clientIdPassword', 'oauth2-resource', '$2a$11$uBcjOC6qWFpxkQJtPyMhPOweH.8gP3Ig1mt27mGDpBncR7gErOuF6', 'read,write,trust', 'password,authorization_code,refresh_token', null, 'ROLE_ADMIN,ROLE_USER', 7200, 5184000, null, 'false'); -- 密码经过了加密,全都是1234567890c INSERT INTO users (id, username, password, enabled, email, phone) VALUES (1, 'jeesun', '$2a$11$t4akVchfgOv00XxB/ZKLlOmweUoL/Aed4CiJqQjaiRLZpBU3AWfxu', true, 'simon.sun.dev@hotmail.com', '18362102427'); diff --git a/src/main/resources/templates/code/entity.ftl b/src/main/resources/templates/code/entity.ftl index 67a176d..8f7a7fd 100644 --- a/src/main/resources/templates/code/entity.ftl +++ b/src/main/resources/templates/code/entity.ftl @@ -1,5 +1,7 @@ package ${entityPackage}; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; import org.hibernate.annotations.GenericGenerator; import lombok.Data; @@ -12,6 +14,7 @@ import java.math.BigDecimal; * @author ${AUTHOR} * @create ${CREATE} **/ +@ApiModel(description = "${entityName}") @Data @Entity @Table(name="${tableName}") @@ -19,9 +22,6 @@ public class ${entityName} implements Serializable{ private static final long serialVersionUID = 1L; <#list columns as column> - /** - * ${(column.comment)} - */ ${(column.annotation)} private ${column.type} ${column.name}; diff --git a/src/test/java/com/simon/UtilTests.java b/src/test/java/com/simon/UtilTests.java index 7fe8c57..6cfdeb4 100644 --- a/src/test/java/com/simon/UtilTests.java +++ b/src/test/java/com/simon/UtilTests.java @@ -6,8 +6,10 @@ import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.yaml.snakeyaml.Yaml; import java.io.FileInputStream; +import java.io.IOException; import java.net.URL; import java.util.Map; +import java.util.Properties; public class UtilTests { @Test @@ -52,4 +54,25 @@ public class UtilTests { e.printStackTrace(); } } + + @Test + public void strIndexTest(){ + String tableComment = "用户表"; + System.out.println(tableComment.lastIndexOf("表")); + System.out.println(tableComment.length()); + } + + @Test + public void propertiesReadTest(){ + Properties prop = new Properties(); + try { + prop.load(UtilTests.class.getResourceAsStream("/test.properties")); + for (Object key : prop.keySet()) { + System.out.println(key + "=" + prop.get(key)); + } + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } } diff --git a/src/test/resources/test.properties b/src/test/resources/test.properties new file mode 100644 index 0000000..a4957e0 --- /dev/null +++ b/src/test/resources/test.properties @@ -0,0 +1,3 @@ +com.alibaba.dayu.sms-type=normal +com.alibaba.dayu.sms-free-sign-name= +com.alibaba.dayu.sms-template-code=SMS_50225027 \ No newline at end of file -- Gitee From c1e486320b0ae93f36a339330eee64a9911f72dc Mon Sep 17 00:00:00 2001 From: SimonSun Date: Sat, 11 Aug 2018 23:55:32 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +++++-- pom.xml | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0b7efc8..fcfe504 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Oauth2 Client通常是要被保护的资源,例如app接口。配套的Oauth2 5. 集成swagger2,并配置非全局、无需重复输入的header参数(token),访问[http://localhost:8182/swagger-ui.html](http://localhost:8182/swagger-ui.html),全局Authorize的值为"Bearer "+"access_token",注意"Bearer"和"access_token"之间有一个空格; 6. 集成Redis缓存,默认使用Ehcache缓存,若要切换成Redis缓存,请查看`application.yml`缓存配置注释; 7. 集成阿里大鱼(需要安装阿里大鱼jar,安装方法:运行src/main/resources/jars/install.bat); -8. 代码生成器,运行`com/simon/common/code/CodeGenerator.java`的main方法进行代码生成,代码生成位置默认是当前项目的test文件夹。可生成Model、Mapper、Repository、Service、ServiceImpl、Controller。~~需要注意的是,生成的Model需要手动添加`@Entity`注解,生成的Mapper需要手动添加`@Repository`注解。~~ +8. 代码生成器,运行`com/simon/common/code/CodeGenerator.java`的main方法进行代码生成,代码生成器的配置在`code-gen.properties`。可生成Model、Mapper、Repository、Service、ServiceImpl、Controller。~~需要注意的是,生成的Model需要手动添加`@Entity`注解,生成的Mapper需要手动添加`@Repository`注解。~~ 请下载与Spring Boot对应版本的oauthserver: @@ -51,8 +51,11 @@ Oauth2 Client通常是要被保护的资源,例如app接口。配套的Oauth2 2. oauthserver v1.2.0添加了阿里大鱼的发送验证码功能,需要阿里大鱼的jar,安装方法:运行src/main/resources/jars/install.bat。 ## 更新日志 +### v1.2.4.alpha(2018-08-11) +- 代码生成器配置文件`code-gen.properties`; +- 修复mysql建表语句bug。 -### v1.2.3(2018-08-05)(当前版本) +### v1.2.3(2018-08-05) - 内置代码生成器; - 移动config目录到common目录下; - 缓存UserDetails到Ehcache,若`spring.cache.type=redis`,则自动缓存UserDetails到Redis; diff --git a/pom.xml b/pom.xml index 9cca35b..b62194e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.simon oauthserver - 1.2.2 + 1.2.4.alpha jar oauthserver -- Gitee