Skip to content

Commit d28695b

Browse files
committed
jdbc
1 parent 6da44f5 commit d28695b

File tree

7 files changed

+34
-38
lines changed

7 files changed

+34
-38
lines changed

generator/src/main/java/lazy/fast/code/generator/Config.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package lazy.fast.code.generator;
22

3-
import lazy.fast.code.generator.util.JdbcUtils;
4-
53
import java.util.HashMap;
64
import java.util.Map;
75

@@ -57,6 +55,11 @@ public class Config {
5755
*/
5856
public static String classDescription;
5957

58+
public static String jdbcClassName;
59+
public static String jdbcUrl;
60+
public static String jdbcUser;
61+
public static String jdbcPassword;
62+
6063
/**
6164
* 模板填充数据值
6265
*/
@@ -74,10 +77,10 @@ public static void initTemplateData() {
7477
DATA.put("className", className);
7578
DATA.put("classDescription", classDescription);
7679

77-
DATA.put("jdbcClassName", JdbcUtils.className);
78-
DATA.put("jdbcUrl", JdbcUtils.url);
79-
DATA.put("jdbcUser", JdbcUtils.user);
80-
DATA.put("jdbcPassword", JdbcUtils.password);
80+
DATA.put("jdbcClassName", jdbcClassName);
81+
DATA.put("jdbcUrl", jdbcUrl);
82+
DATA.put("jdbcUser", jdbcUser);
83+
DATA.put("jdbcPassword", jdbcPassword);
8184
}
8285

8386
}

generator/src/main/java/lazy/fast/code/generator/QuickStart.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ public static void main(String[] args) {
4848
// 项目骨架模板文件根路径
4949
Config.layoutTemplateRootPath = System.getProperty("template.layout", null);
5050

51+
Config.jdbcClassName = System.getProperty("jdbc.class", null);
52+
Config.jdbcUrl = System.getProperty("jdbc.url", null);
53+
Config.jdbcUser = System.getProperty("jdbc.user", null);
54+
Config.jdbcPassword = System.getProperty("jdbc.password", null);
55+
5156
// 生成项目骨架
5257
Generator.generateLayout();
5358
}

generator/src/main/java/lazy/fast/code/generator/util/JdbcUtils.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package lazy.fast.code.generator.util;
22

3-
import java.io.IOException;
4-
import java.io.InputStream;
3+
import lazy.fast.code.generator.Config;
4+
55
import java.sql.Connection;
66
import java.sql.DriverManager;
77
import java.sql.SQLException;
8-
import java.util.Properties;
98

109
/**
1110
* 数据库工具操作类
@@ -14,38 +13,19 @@
1413
*/
1514
public class JdbcUtils {
1615

17-
public static String className;
18-
public static String url;
19-
public static String user;
20-
public static String password;
21-
22-
static {
23-
InputStream is = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
24-
Properties props = new Properties();
25-
try {
26-
props.load(is);
27-
className = props.getProperty("className");
28-
url = props.getProperty("url");
29-
user = props.getProperty("user");
30-
password = props.getProperty("password");
31-
} catch (IOException e) {
32-
throw new RuntimeException();
33-
}
34-
}
35-
3616
private Connection con = null;
3717

3818
public JdbcUtils() {
3919
try {
40-
Class.forName(className);
20+
Class.forName(Config.jdbcClassName);
4121
} catch (ClassNotFoundException e) {
4222
throw new RuntimeException();
4323
}
4424
}
4525

4626
public Connection getConnection() {
4727
try {
48-
con = DriverManager.getConnection(url, user, password);
28+
con = DriverManager.getConnection(Config.jdbcUrl, Config.jdbcUser, Config.jdbcPassword);
4929
} catch (SQLException e) {
5030
throw new RuntimeException();
5131
}

generator/src/main/resources/jdbc.properties

Lines changed: 0 additions & 4 deletions
This file was deleted.

generator/src/main/resources/template/layout/generator.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public class GeneratorTest {
1616
// 模板文件根路径, 为空的时候就默认使用内置模板,如果有自定义模板就写模板绝对路径
1717
Config.moduleTemplateRootPath = null;
1818
Config.layoutTemplateRootPath = null;
19+
1920
// 作者签名, 如果不填写则按System.getProperty("user.name")取值
2021
Config.author = "${author}";
2122

@@ -38,6 +39,11 @@ public class GeneratorTest {
3839
// 【必填项】类描述
3940
Config.classDescription = "用户信息";
4041

42+
Config.jdbcClassName = "${jdbcClassName!'com.mysql.cj.jdbc.Driver'}";
43+
Config.jdbcUrl = "${jdbcUrl!'jdbc:mysql://localhost:3306/demo'}";
44+
Config.jdbcUser = "${jdbcUser!'root'}";
45+
Config.jdbcPassword = "${jdbcPassword!'123456'}";
46+
4147
// 生成所有模板类
4248
Generator.generateAll();
4349
}

generator/src/main/resources/template/layout/resource.ftl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ spring:
22
application:
33
name: ${projectName}
44
datasource:
5-
url: ${jdbcUrl}
6-
username: ${jdbcUser}
7-
password: ${jdbcPassword}
8-
driver-class-name: ${jdbcClassName}
5+
url: ${jdbcUrl!"jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&useSSL=false&autoReconnect=true&serverTimezone=Asia/Shanghai"}
6+
username: ${jdbcUser!"root"}
7+
password: ${jdbcPassword!"123456"}
8+
driver-class-name: ${jdbcClassName!"com.mysql.cj.jdbc.Driver"}
99
hikari:
1010
minimum-idle: 5
1111
maximum-pool-size: 15

generator/src/test/java/lazy/fast/code/generator/GeneratorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public static void main(String[] args) {
1111
// 模板文件根路径, 为空的时候就默认使用内置模板,如果有自定义模板就写模板绝对路径
1212
Config.moduleTemplateRootPath = null;
1313
Config.layoutTemplateRootPath = null;
14+
1415
// 作者签名, 如果不填写则按System.getProperty("user.name")取值
1516
Config.author = "wendell";
1617

@@ -33,6 +34,11 @@ public static void main(String[] args) {
3334
// 【必填项】类描述
3435
Config.classDescription = "用户信息";
3536

37+
Config.jdbcClassName = "com.mysql.cj.jdbc.Driver";
38+
Config.jdbcUrl = "jdbc:mysql://localhost:3306/demo";
39+
Config.jdbcUser = "root";
40+
Config.jdbcPassword = "123456";
41+
3642
// 生成所有模板类
3743
Generator.generateAll();
3844
}

0 commit comments

Comments
 (0)