瀏覽代碼

代码生成工具类

my-csc-config-file.git 5 年之前
當前提交
c783d0e3e5

+ 19 - 0
.gitignore

@@ -0,0 +1,19 @@
1
+*.class
2
+
3
+# Mobile Tools for Java (J2ME)
4
+.mtj.tmp/
5
+# Package Files #
6
+*.jar
7
+*.war
8
+*.ear
9
+
10
+# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
11
+hs_err_pid*
12
+
13
+
14
+
15
+log
16
+.idea
17
+*.iml
18
+/target/
19
+/log/

+ 3 - 0
README.md

@@ -0,0 +1,3 @@
1
+# csc-szls-generation
2
+
3
+代码生成

+ 105 - 0
pom.xml

@@ -0,0 +1,105 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4
+    <modelVersion>4.0.0</modelVersion>
5
+    <parent>
6
+        <groupId>org.springframework.boot</groupId>
7
+        <artifactId>spring-boot-starter-parent</artifactId>
8
+        <version>2.1.2.RELEASE</version>
9
+        <relativePath/> <!-- lookup parent from repository -->
10
+    </parent>
11
+    <groupId>com.unissoft</groupId>
12
+    <artifactId>csc-szls-generation</artifactId>
13
+    <version>0.0.1-SNAPSHOT</version>
14
+    <name>csc-szls-generation</name>
15
+    <description>Demo project for Spring Boot</description>
16
+
17
+    <properties>
18
+        <!-- 构建时编码 -->
19
+        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
20
+        <!-- 输出时编码 -->
21
+        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
22
+        <!-- JDK版本 -->
23
+        <java.version>1.8</java.version>
24
+        <mybatisPlus.version>3.4.1</mybatisPlus.version>
25
+        <shardingsphere.version>4.1.1</shardingsphere.version>
26
+    </properties>
27
+
28
+    <dependencies>
29
+        <dependency>
30
+            <groupId>org.springframework.boot</groupId>
31
+            <artifactId>spring-boot-starter-web</artifactId>
32
+        </dependency>
33
+        <dependency>
34
+            <groupId>org.springframework.boot</groupId>
35
+            <artifactId>spring-boot-starter-test</artifactId>
36
+            <scope>test</scope>
37
+        </dependency>
38
+        <dependency>
39
+            <groupId>mysql</groupId>
40
+            <artifactId>mysql-connector-java</artifactId>
41
+        </dependency>
42
+
43
+        <dependency>
44
+            <groupId>org.mybatis.spring.boot</groupId>
45
+            <artifactId>mybatis-spring-boot-starter</artifactId>
46
+            <version>2.0.1</version>
47
+        </dependency>
48
+
49
+        <dependency>
50
+            <groupId>org.springframework.boot</groupId>
51
+            <artifactId>spring-boot-starter-freemarker</artifactId>
52
+        </dependency>
53
+
54
+      <!--  <dependency>
55
+            <groupId>com.github.pagehelper</groupId>
56
+            <artifactId>pagehelper-spring-boot-starter</artifactId>
57
+        </dependency>-->
58
+
59
+        <dependency>
60
+            <groupId>com.baomidou</groupId>
61
+            <artifactId>mybatis-plus-boot-starter</artifactId>
62
+            <version>${mybatisPlus.version}</version>
63
+        </dependency>
64
+
65
+        <!--mybatis-plus 码生成器 添加 模板引擎依赖 这个需要增加模板引擎依赖,如freemarker-->
66
+        <dependency>
67
+            <groupId>com.baomidou</groupId>
68
+            <artifactId>mybatis-plus-generator</artifactId>
69
+            <version>${mybatisPlus.version}</version>
70
+        </dependency>
71
+
72
+
73
+        <dependency>
74
+            <groupId>org.projectlombok</groupId>
75
+            <artifactId>lombok</artifactId>
76
+            <scope>provided</scope>
77
+        </dependency>
78
+        <dependency>
79
+            <groupId>io.swagger</groupId>
80
+            <artifactId>swagger-annotations</artifactId>
81
+            <version>1.5.21</version>
82
+            <scope>compile</scope>
83
+        </dependency>
84
+
85
+        <!--swagger-->
86
+       <!-- <dependency>
87
+            <groupId>io.springfox</groupId>
88
+            <artifactId>springfox-swagger2</artifactId>
89
+        </dependency>
90
+        <dependency>
91
+            <groupId>io.springfox</groupId>
92
+            <artifactId>springfox-swagger-ui</artifactId>
93
+        </dependency>-->
94
+    </dependencies>
95
+
96
+    <build>
97
+        <plugins>
98
+            <plugin>
99
+                <groupId>org.springframework.boot</groupId>
100
+                <artifactId>spring-boot-maven-plugin</artifactId>
101
+            </plugin>
102
+        </plugins>
103
+    </build>
104
+
105
+</project>

+ 167 - 0
src/main/java/com/unissoft/CodeGenerator.java

@@ -0,0 +1,167 @@
1
+package com.unissoft;
2
+
3
+
4
+import com.baomidou.mybatisplus.annotation.DbType;
5
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
6
+import com.baomidou.mybatisplus.generator.AutoGenerator;
7
+import com.baomidou.mybatisplus.generator.InjectionConfig;
8
+import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
9
+import com.baomidou.mybatisplus.generator.config.GlobalConfig;
10
+import com.baomidou.mybatisplus.generator.config.PackageConfig;
11
+import com.baomidou.mybatisplus.generator.config.StrategyConfig;
12
+import com.baomidou.mybatisplus.generator.config.rules.DateType;
13
+import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
14
+import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
15
+import freemarker.log.Logger;
16
+
17
+import java.util.Date;
18
+import java.util.HashMap;
19
+import java.util.Map;
20
+
21
+/**
22
+ * 文件描述
23
+ *
24
+ * @author tongxi.xia
25
+ * @date 2020年11月26日 15:27
26
+ */
27
+public class
28
+CodeGenerator {
29
+
30
+    private static Logger log = Logger.getLogger(CodeGenerator.class.toString());
31
+
32
+    //用户名:SUPLIS 密码:plansuplis
33
+    //用户:SUPPLAN 密码:plansup
34
+    //用户:CSGX 密码:gxcs
35
+    //用户:CSGXUSER 密码:usercsgx
36
+    //用户:PLATFORM 密码:
37
+    private static final String jdbcUserName = "root";
38
+    private static final String jdbcPassword = "123456";
39
+
40
+    //表名,可以设置多个,通过英文逗号分隔
41
+    //数据库用户密码,必须使用表的所有者(Owner)
42
+    private static final String tables = "sys_user";
43
+    private static final String moduleName = "";//模块名称,分业务,不要漏最后面一个点
44
+
45
+    private static final String tablePrefix = "";//表名前缀
46
+
47
+    /**
48
+     * 代码生成器的配置常量
49
+     */
50
+    private static final String outPutDir = "/csc-szls-generation/src/main/java";
51
+    //Oracle:jdbc:oracle:thin:@192.168.110.2:1521:ORAPLAN
52
+    //mysql:jdbc:mysql://localhost:3306/mydbone?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&pinGlobalTxToPhysicalConnection=true&autoReconnect=true
53
+    private static final String jdbcUrl = "jdbc:mysql://localhost:3306/csc-szls?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&pinGlobalTxToPhysicalConnection=true&autoReconnect=true";
54
+
55
+    //oracle.jdbc.OracleDriver
56
+    //oracle.jdbc.driver.OracleDriver
57
+    //com.mysql.cj.jdbc.Driver
58
+    private static final String jdbcDriverClassName = "com.mysql.jdbc.Driver";
59
+
60
+    private static final String parentPackage = "com.unissoft";
61
+
62
+    private static final String authorName = jdbcUserName;//作者
63
+
64
+    private static final String mapperPattern = "%sMapper";//dao文件命名格式
65
+    private static final String mapperName = moduleName + "mapper";
66
+    private static final String xmlName = mapperName;
67
+
68
+    private static final String serviceNamePattern = "%sService";//Service文件命名格式
69
+    private static final String serviceName = moduleName + "service";
70
+    private static final String implName = moduleName + "service.impl";
71
+    private static final String pojoName = moduleName + "model";
72
+    private static final String controllerName = moduleName + "controller";
73
+
74
+
75
+    // 当前工程路径   配合outPutDir使用,例如多模块开发 Demo/test1,Demo/test2
76
+    // projectPath拿到的是Demo路径,把outPutDir设置成/test1即可
77
+    private static final String projectPath = System.getProperty("user.dir");
78
+
79
+    public static void generator() {
80
+
81
+        // 全局配置
82
+        GlobalConfig gc = new GlobalConfig();
83
+        gc.setOutputDir(projectPath + outPutDir);
84
+        gc.setAuthor(authorName);
85
+        gc.setSwagger2(true);
86
+        gc.setBaseResultMap(true);
87
+        gc.setBaseColumnList(true);
88
+        // 覆盖生成的文件
89
+        gc.setFileOverride(true);
90
+        gc.setServiceName(serviceNamePattern);
91
+        gc.setMapperName(mapperPattern);
92
+        gc.setEnableCache(false);//是否在xml中添加二级缓存配置:false不生成
93
+        gc.setOpen(false);//true:生成文件后,自动打开文件夹
94
+        gc.setDateType(DateType.ONLY_DATE);//设置时间类型
95
+        gc.setSwagger2(true);//设置生成Swagger2 Api注解
96
+
97
+        // 数据源配置
98
+        DataSourceConfig dsc = new DataSourceConfig();
99
+        dsc.setDbType(DbType.MYSQL);
100
+
101
+
102
+        dsc.setUrl(jdbcUrl);
103
+        dsc.setDriverName(jdbcDriverClassName);
104
+        dsc.setUsername(jdbcUserName);
105
+        dsc.setPassword(jdbcPassword);
106
+        // dsc.setSchemaName("public");
107
+
108
+
109
+        // 包配置
110
+        PackageConfig pc = new PackageConfig();
111
+        //pc.setModuleName(scanner("模块名"));
112
+        pc.setParent(parentPackage);
113
+
114
+        pc.setParent(parentPackage);
115
+        pc.setMapper(mapperName);
116
+        pc.setEntity(pojoName);
117
+        pc.setService(serviceName);
118
+        pc.setController(controllerName);
119
+        pc.setServiceImpl(implName);
120
+        pc.setXml(xmlName);
121
+
122
+
123
+        // 策略配置
124
+        StrategyConfig strategy = new StrategyConfig();
125
+        strategy.setNaming(NamingStrategy.underline_to_camel);
126
+        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
127
+        //strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
128
+        strategy.setEntityLombokModel(true);//设置Lombok模式
129
+        strategy.setRestControllerStyle(true);
130
+        strategy.setControllerMappingHyphenStyle(false);
131
+        // 公共父类
132
+        //strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
133
+        // 写于父类中的公共字段
134
+        //strategy.setSuperEntityColumns("id");
135
+        strategy.setControllerMappingHyphenStyle(true);
136
+        if (!StringUtils.isBlank(tablePrefix)) {
137
+            strategy.setTablePrefix(tablePrefix);
138
+        }
139
+        //strategy.entityTableFieldAnnotationEnable(true);
140
+        strategy.setEntityTableFieldAnnotationEnable(true);
141
+        strategy.setInclude(tables.toUpperCase().split(","));//Oracle数据,表名一定要大写,且表名要对应创建的用户,否则不生成代码
142
+        InjectionConfig injectionConfig = new InjectionConfig() {
143
+            @Override
144
+            public void initMap() {
145
+                Map<String, Object> map = new HashMap<String, Object>();
146
+                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
147
+                this.setMap(map);
148
+            }
149
+        };
150
+        // 代码生成器
151
+        AutoGenerator mpg = new AutoGenerator();
152
+        mpg.setGlobalConfig(gc);
153
+        mpg.setDataSource(dsc);
154
+        mpg.setPackageInfo(pc);
155
+        mpg.setStrategy(strategy);
156
+        mpg.setCfg(injectionConfig);
157
+        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
158
+        mpg.execute();
159
+        log.info("代码生成已经完成。" + new Date());
160
+    }
161
+
162
+
163
+    public static void main(String[] args) {
164
+        generator();
165
+    }
166
+
167
+}

+ 13 - 0
src/main/java/com/unissoft/CscSzlsGenerationApplication.java

@@ -0,0 +1,13 @@
1
+package com.unissoft;
2
+
3
+import org.springframework.boot.SpringApplication;
4
+import org.springframework.boot.autoconfigure.SpringBootApplication;
5
+
6
+@SpringBootApplication
7
+public class CscSzlsGenerationApplication {
8
+
9
+    public static void main(String[] args) {
10
+        SpringApplication.run(CscSzlsGenerationApplication.class, args);
11
+    }
12
+
13
+}

+ 1 - 0
src/main/resources/application.properties

@@ -0,0 +1 @@
1
+

+ 13 - 0
src/test/java/com/unissoft/CscSzlsGenerationApplicationTests.java

@@ -0,0 +1,13 @@
1
+package com.unissoft;
2
+
3
+import org.junit.Test;
4
+import org.springframework.boot.test.context.SpringBootTest;
5
+
6
+@SpringBootTest
7
+class CscSzlsGenerationApplicationTests {
8
+
9
+    @Test
10
+    void contextLoads() {
11
+    }
12
+
13
+}