|
@@ -0,0 +1,249 @@
|
|
|
+package gencode;
|
|
|
+
|
|
|
+import org.apache.velocity.Template;
|
|
|
+import org.apache.velocity.VelocityContext;
|
|
|
+import org.apache.velocity.app.VelocityEngine;
|
|
|
+import org.mybatis.generator.api.MyBatisGenerator;
|
|
|
+import org.mybatis.generator.config.Configuration;
|
|
|
+import org.mybatis.generator.config.xml.ConfigurationParser;
|
|
|
+import org.mybatis.generator.internal.DefaultShellCallback;
|
|
|
+
|
|
|
+import java.io.*;
|
|
|
+import java.sql.*;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class GeneratorCode {
|
|
|
+ //数据驱动
|
|
|
+ final static String DRIVER_CLASS = "com.mysql.jdbc.Driver";
|
|
|
+ //数据库名
|
|
|
+ final static String DATABASE_NAME = "future";
|
|
|
+ //数据连接地址
|
|
|
+ final static String CONNECTION_URL = "jdbc:mysql://49.235.127.212:3306/" + DATABASE_NAME + "?characterEncoding=UTF-8";
|
|
|
+ //数据库登录名
|
|
|
+ final static String USER_ID = "root";
|
|
|
+ //密码
|
|
|
+ final static String PASS_WORD = "123456";
|
|
|
+ //数据库表名
|
|
|
+ final static String TABLE_NAME = "pms_base_position";
|
|
|
+ //实体名称首字母小写
|
|
|
+ final static String ENTITY_NAME = "position";
|
|
|
+ //基础目录(不允许修改)
|
|
|
+ final static String BASE_PACKAGE_NAME = "src.main.java.";
|
|
|
+ //controller模版文件包结构
|
|
|
+ final static String PACKAGE_NAME = BASE_PACKAGE_NAME + "com.activiti6";
|
|
|
+ //controller模版文件包结构
|
|
|
+ final static String CONTROLLER_PACKAGE_NAME = "com.activiti6.controller";
|
|
|
+
|
|
|
+ //service模版文件包结构
|
|
|
+ final static String SERVICE_PACKAGE_NAME = "com.activiti6.service";
|
|
|
+ //service模版文件包结构
|
|
|
+ final static String SERVICE_IMPL_PACKAGE_NAME = "com.activiti6.service.impl";
|
|
|
+ //实体生产包名
|
|
|
+ final static String ENTITY_PACKAGE_NAME = "com.activiti6.model.entity";
|
|
|
+ //接口mybatis mapper
|
|
|
+ final static String MAPPER_PACKAGE_NAME = "com.activiti6.mapper";
|
|
|
+ //mybatis xml文件
|
|
|
+ final static String XMLMAPPER_PACKAGE_NAME = "com.activiti6.mapper";
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 不能修改
|
|
|
+ */
|
|
|
+ //controller模版文件目录
|
|
|
+ final static String CONTROLLER_DIR = "src/main/resources/vm/java/controller/ModelController.java.vm";
|
|
|
+ //service 模版文件目录
|
|
|
+ final static String SERVICE_DIR = "src/main/resources/vm/java/service/ModelSeevice.java.vm";
|
|
|
+ //serviceImpl模版文件目录
|
|
|
+ final static String SERVICEIMPL_DIR = "src/main/resources/vm/java/service/impl/ModelServiceImpl.java.vm";
|
|
|
+ //my_batis_config模版文件目录
|
|
|
+ final static String MY_BATIS_CONFIG_DIR = "src/main/resources/vm/mybatis/generatorConfig.xml.vm";
|
|
|
+
|
|
|
+
|
|
|
+ private static void productionJavaFiles() throws Exception {
|
|
|
+ VelocityEngine ve = new VelocityEngine();
|
|
|
+ ve.init();
|
|
|
+
|
|
|
+ VelocityContext ctx = new VelocityContext();
|
|
|
+ ctx.put("controller_package_name", CONTROLLER_PACKAGE_NAME);
|
|
|
+ ctx.put("service_package_name", SERVICE_PACKAGE_NAME);
|
|
|
+ ctx.put("service_impl_package_name", SERVICE_IMPL_PACKAGE_NAME);
|
|
|
+ ctx.put("ENTITY_PACKAGE_NAME", ENTITY_PACKAGE_NAME);
|
|
|
+ ctx.put("XMLMAPPER_PACKAGE_NAME", XMLMAPPER_PACKAGE_NAME);
|
|
|
+ ctx.put("MAPPER_PACKAGE_NAME", MAPPER_PACKAGE_NAME);
|
|
|
+ ctx.put("entity", ENTITY_NAME);
|
|
|
+ //获取主键类型
|
|
|
+ MysqlDataType mysqlDataType = getIdType();
|
|
|
+ if (null == mysqlDataType) {
|
|
|
+ throw new Exception("数据库连接错误");
|
|
|
+ }
|
|
|
+ ctx.put("mysqlDataType", mysqlDataType);
|
|
|
+ String entityName = ENTITY_NAME.substring(0, 1).toUpperCase() + ENTITY_NAME.substring(1);
|
|
|
+ ctx.put("Entity", entityName);
|
|
|
+ saveController(ve, ctx, entityName);
|
|
|
+ saveService(ve, ctx, entityName);
|
|
|
+ saveServiceImpl(ve, ctx, entityName);
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void saveController(VelocityEngine ve, VelocityContext ctx, String entityName) throws IOException {
|
|
|
+ Template t = ve.getTemplate(CONTROLLER_DIR);
|
|
|
+ //创建文件
|
|
|
+ String substring = entityName + "Controller.java";
|
|
|
+ //输出文件
|
|
|
+ String replace = PACKAGE_NAME.replace('.', '/') + "/controller/";
|
|
|
+ String s = replace + substring;
|
|
|
+ File controllerFile = new File(s);
|
|
|
+ saveFile(t, ctx, controllerFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void saveService(VelocityEngine ve, VelocityContext ctx, String entityName) throws IOException {
|
|
|
+ Template t = ve.getTemplate(SERVICE_DIR);
|
|
|
+ //创建文件
|
|
|
+ String substring = entityName + "Service.java";
|
|
|
+ //输出文件
|
|
|
+ String replace = PACKAGE_NAME.replace('.', '/') + "/service/";
|
|
|
+ String s = replace + substring;
|
|
|
+ File controllerFile = new File(s);
|
|
|
+ saveFile(t, ctx, controllerFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void saveServiceImpl(VelocityEngine ve, VelocityContext ctx, String entityName) throws IOException {
|
|
|
+ Template t = ve.getTemplate(SERVICEIMPL_DIR);
|
|
|
+ //创建文件
|
|
|
+ String substring = entityName + "ServiceImpl.java";
|
|
|
+ //输出文件
|
|
|
+ String replace = PACKAGE_NAME.replace('.', '/') + "/service/impl/";
|
|
|
+ String s = replace + substring;
|
|
|
+ File controllerFile = new File(s);
|
|
|
+ saveFile(t, ctx, controllerFile);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void saveFile(Template t, VelocityContext ctx, File saveFile) throws IOException {
|
|
|
+ //获得它的父类文件,如果不存在,就创建
|
|
|
+ if (!saveFile.getParentFile().exists()) {
|
|
|
+
|
|
|
+ saveFile.getParentFile().mkdirs();
|
|
|
+ }
|
|
|
+ //创建文件输出流
|
|
|
+ FileOutputStream outStream = new FileOutputStream(saveFile);
|
|
|
+
|
|
|
+ //因为模板整合的时候,需要提供一个Writer,所以创建一个Writer
|
|
|
+ OutputStreamWriter writer = new OutputStreamWriter(outStream);
|
|
|
+
|
|
|
+ //创建一个缓冲流
|
|
|
+ BufferedWriter bufferWriter = new BufferedWriter(writer);
|
|
|
+
|
|
|
+ //5 Merge the template and you data toproduce the output
|
|
|
+ t.merge(ctx, bufferWriter);
|
|
|
+
|
|
|
+ bufferWriter.flush();//强制刷新
|
|
|
+
|
|
|
+ outStream.close();
|
|
|
+
|
|
|
+ bufferWriter.close();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void saveMybatisFile() throws IOException {
|
|
|
+
|
|
|
+ VelocityEngine ve = new VelocityEngine();
|
|
|
+ ve.init();
|
|
|
+
|
|
|
+ VelocityContext ctx = new VelocityContext();
|
|
|
+ ctx.put("DRIVER_CLASS", DRIVER_CLASS);
|
|
|
+ ctx.put("CONNECTION_URL", CONNECTION_URL);
|
|
|
+ ctx.put("USER_ID", USER_ID);
|
|
|
+ ctx.put("PASS_WORD", PASS_WORD);
|
|
|
+ ctx.put("ENTITY_NAME", ENTITY_NAME);
|
|
|
+ ctx.put("entity", ENTITY_NAME);
|
|
|
+ String entityName = ENTITY_NAME.substring(0, 1).toUpperCase() + ENTITY_NAME.substring(1);
|
|
|
+ ctx.put("Entity", entityName);
|
|
|
+ ctx.put("TABLE_NAME", TABLE_NAME);
|
|
|
+ ctx.put("ENTITY_PACKAGE_NAME", ENTITY_PACKAGE_NAME);
|
|
|
+ ctx.put("XMLMAPPER_PACKAGE_NAME", XMLMAPPER_PACKAGE_NAME);
|
|
|
+ ctx.put("MAPPER_PACKAGE_NAME", MAPPER_PACKAGE_NAME);
|
|
|
+ Template t = ve.getTemplate(MY_BATIS_CONFIG_DIR);
|
|
|
+ String s = "src/main/resources/mybatis/generator/generatorConfig.fxml";
|
|
|
+ File controllerFile = new File(s);
|
|
|
+ saveFile(t, ctx, controllerFile);
|
|
|
+ try {
|
|
|
+ GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
|
|
|
+ generatorSqlmap.generator();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * mybatis文件生成
|
|
|
+ *
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ public void generator() throws Exception {
|
|
|
+
|
|
|
+ List<String> warnings = new ArrayList<String>();
|
|
|
+ boolean overwrite = true;
|
|
|
+ //指定 逆向工程配置文件
|
|
|
+ File configFile = new File("src/mybatis/generator/generatorConfig.xml");
|
|
|
+ ConfigurationParser cp = new ConfigurationParser(warnings);
|
|
|
+ Configuration config = cp.parseConfiguration(configFile);
|
|
|
+ DefaultShellCallback callback = new DefaultShellCallback(overwrite);
|
|
|
+ MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
|
|
|
+ callback, warnings);
|
|
|
+ myBatisGenerator.generate(null);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static MysqlDataType getIdType() {
|
|
|
+ MysqlDataType enumType = null;
|
|
|
+ try {
|
|
|
+ //1、注册JDBC驱动
|
|
|
+ Class.forName(DRIVER_CLASS);
|
|
|
+ //2、获取数据库连接
|
|
|
+ Connection connection = DriverManager.getConnection(CONNECTION_URL, USER_ID, PASS_WORD);
|
|
|
+ //3、操作数据库
|
|
|
+ Statement statement = connection.createStatement();//获取操作数据库的对象
|
|
|
+ String sql = "SELECT COLUMN_NAME,COLUMN_COMMENT,DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,COLUMN_KEY " +
|
|
|
+ "FROM information_schema. COLUMNS WHERE table_schema = '" + DATABASE_NAME + "' AND table_name = '" + TABLE_NAME + "'";
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);//执行sql,获取结果集
|
|
|
+
|
|
|
+ while (resultSet.next()) { //遍历结果集,取出数据
|
|
|
+ //输出数据
|
|
|
+ String key = resultSet.getString("COLUMN_KEY");
|
|
|
+ if ("PRI".equals(key)) {
|
|
|
+ String column_name = resultSet.getString("COLUMN_NAME");
|
|
|
+ System.out.println("主键名称:" + column_name);
|
|
|
+ String data_type = resultSet.getString("DATA_TYPE");
|
|
|
+ System.out.println("主键类型:" + data_type);
|
|
|
+ enumType = MysqlDataType.getEnumType(data_type);
|
|
|
+ if (null != enumType) {
|
|
|
+ enumType.setColumn(column_name);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //4、关闭结果集、数据库操作对象、数据库连接
|
|
|
+ resultSet.close();
|
|
|
+ statement.close();
|
|
|
+ connection.close();
|
|
|
+
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return enumType;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ //使用mybatis文件
|
|
|
+ saveMybatisFile();
|
|
|
+ //生产java文件
|
|
|
+ productionJavaFiles();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|