需求

  • 需求、想法和框架都有了,接下来我们就是实干阶段,首先们要考虑的是这是一个通用的代码生成器,这时我们想到的就是通过配轩置文件来实现对数据库及生成目录名称等的配置,这里需求就是要有配置

实现

  • 在程序起动时就要加载我们自定义的配置,这时就需要配置文件config.properties,还需要注意的是我们打完包后是走包外的配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DB_SERVER = 127.0.0.1
DB_PORT = 3306
DB_USER = root
DB_PASSWORD = abc@123
DB_NAME = testCode
PACKAGE_ROOT_NAME=com.hantianwei.testcode
PACKAGE_ROOT_PATH = com.hantianwei.testcode
PACKAGE_MODEL_NAME = domian
PACKAGE_MODEL_PATH = \\testcode\\testcode-domain\\src/main/java/com/hantianwei/testcode/domain/
PACKAGE_DAO_NAME = dao
PACKAGE_DAO_PATH = /testcode/testcode-dao/src/main/java/com/hantianwei/testcode/dao/
PACKAGE_SERVICE_NAME = service
PACKAGE_SERVICE_PATH = /testcode/testcode-service/src/main/java/com/hantianwei/testcode/service/impl/
PACKAGE_ISERVICE_NAME = service
PACKAGE_ISERVICE_PATH = /testcode/testcode-service/src/main/java/com/hantianwei/testcode/service/
PACKAGE_MAPPER_PATH = /testcode/testcode-dao/src/main/resources/mapping/
TRIM_STRINGS = true
  • 需要在model里面建一个Config
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.hantianwei.generator.model;

/**
* Created by tianwei on 2017/6/27.
*/
public class Config {
public static String DB_SERVER = "";
public static int DB_PORT = 0;
public static String DB_USER = "";
public static String DB_PASSWORD = "";
public static String DB_NAME = "";
public static String PACKAGE_ROOT_NAME="";
public static String PACKAGE_ROOT_PATH = "";
public static String PACKAGE_MODEL_NAME = "";
public static String PACKAGE_MODEL_PATH = "";
public static String PACKAGE_DAO_NAME = "";
public static String PACKAGE_DAO_PATH = "";
public static String PACKAGE_SERVICE_NAME = "";
public static String PACKAGE_SERVICE_PATH = "";
public static String PACKAGE_ISERVICE_NAME = "";
public static String PACKAGE_ISERVICE_PATH = "";
public static String PACKAGE_MAPPER_PATH = "";
public static boolean TRIM_STRINGS = false;

public static String CONFIG_LOCAL_PATH ="config.properties";
public static boolean CONFIG_IS_LOCAL = true;
public static String CONFIG_PATH ="";
}
  • 在util里面新建PropUtil进行对配置的装载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.hantianwei.generator.util;

import com.hantianwei.generator.model.Config;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

/**
* Created by tianwei on 2017/6/27.
*/
public class PropUtil {

public static void loadConf(String[] args){
if (args == null || args.length == 0) {
Config.CONFIG_IS_LOCAL = true;
PropUtil.loadConf(Config.CONFIG_LOCAL_PATH);
System.out.println(String.format("加载默认配置完成:数据库[%s]",Config.DB_NAME));
} else {
Config.CONFIG_IS_LOCAL = false;
Config.CONFIG_PATH = args[0];
PropUtil.loadConf(Config.CONFIG_PATH);
System.out.println(String.format("加载自定义配置完成:数据库[%s]",Config.DB_NAME));
}
}

public static void loadConf(String path) {
try {
Properties props = new Properties();
InputStream in;
if (Config.CONFIG_IS_LOCAL) {
in = PropUtil.class.getClassLoader().getResourceAsStream(path);
} else {
in = new FileInputStream(path);
}
props.load(in);
Config.DB_SERVER = props.getProperty("DB_SERVER");
Config.DB_PORT = Integer.parseInt(props.getProperty("DB_PORT"));
Config.DB_USER = props.getProperty("DB_USER");
Config.DB_PASSWORD = props.getProperty("DB_PASSWORD");
Config.DB_NAME = props.getProperty("DB_NAME");
Config.PACKAGE_ROOT_NAME = props.getProperty("PACKAGE_ROOT_NAME");
Config.PACKAGE_ROOT_PATH = props.getProperty("PACKAGE_ROOT_PATH");
Config.PACKAGE_MODEL_NAME = props.getProperty("PACKAGE_MODEL_NAME");
Config.PACKAGE_MODEL_PATH = props.getProperty("PACKAGE_MODEL_PATH");
Config.PACKAGE_DAO_NAME = props.getProperty("PACKAGE_DAO_NAME");
Config.PACKAGE_DAO_PATH = props.getProperty("PACKAGE_DAO_PATH");
Config.PACKAGE_SERVICE_NAME = props.getProperty("PACKAGE_SERVICE_NAME");
Config.PACKAGE_SERVICE_PATH = props.getProperty("PACKAGE_SERVICE_PATH");
Config.PACKAGE_ISERVICE_NAME = props.getProperty("PACKAGE_ISERVICE_NAME");
Config.PACKAGE_ISERVICE_PATH = props.getProperty("PACKAGE_ISERVICE_PATH");
Config.PACKAGE_MAPPER_PATH = props.getProperty("PACKAGE_MAPPER_PATH");
Config.TRIM_STRINGS = Boolean.parseBoolean(props.getProperty("TRIM_STRINGS"));

} catch (Exception e) {
e.printStackTrace();
}
}
}
  • 对了忘了我们的Mian方法了,这里在main里新建RunGenerator
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.hantianwei.generator.main;

import com.hantianwei.generator.util.PropUtil;

/**
* Created by tianwei on 2017/6/27.
*/
public class RunGenerator {
public static void main(String[] args){
PropUtil.loadConf(args);
System.out.println("****** OK ******");
}
}
  • 到这里好奇的同学可以断电试一下,我们的配置可以加载成功了,如果想试打包后的效果,可以先打个包,再用下面命令,记得config.properties得是包外自定义的丁配置文件
1
java -jar jgenerator.jar config.properties