优化代码
This commit is contained in:
parent
4004ffe444
commit
41b9d57c9e
|
@ -1,6 +1,6 @@
|
|||
package com.zheng.cms.dao;
|
||||
|
||||
import com.zheng.common.util.MybatisGeneratorConfigUtil;
|
||||
import com.zheng.common.util.MybatisGeneratorUtil;
|
||||
import com.zheng.common.util.PropertiesFileUtil;
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,7 @@ public class Generator {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
MybatisGeneratorConfigUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
package com.zheng.common.util;
|
||||
|
||||
/**
|
||||
* 代码生成工具类
|
||||
* Created by ZhangShuzheng on 2017/1/10.
|
||||
*/
|
||||
public class GeneratorUtil {
|
||||
|
||||
}
|
|
@ -10,12 +10,6 @@ import java.util.jar.JarFile;
|
|||
*/
|
||||
public class JarUtil {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("start");
|
||||
decompress("F:\\GitHub\\zhengAdmin\\target\\zhengAdmin.jar", "F:\\GitHub\\zhengAdmin\\target\\test");
|
||||
System.out.println("end");
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压jar文件到指定目录
|
||||
* @param fileName
|
||||
|
@ -36,7 +30,7 @@ public class JarUtil {
|
|||
try {
|
||||
jf = new JarFile(fileName);
|
||||
for (Enumeration<JarEntry> e = jf.entries(); e.hasMoreElements(); ) {
|
||||
JarEntry je = (JarEntry) e.nextElement();
|
||||
JarEntry je = e.nextElement();
|
||||
String outFileName = outputPath + je.getName();
|
||||
File f = new File(outFileName);
|
||||
if (je.isDirectory()) {
|
||||
|
@ -52,7 +46,7 @@ public class JarUtil {
|
|||
OutputStream out = new BufferedOutputStream(
|
||||
new FileOutputStream(f));
|
||||
byte[] buffer = new byte[2048];
|
||||
int nBytes = 0;
|
||||
int nBytes;
|
||||
while ((nBytes = in.read(buffer)) > 0) {
|
||||
out.write(buffer, 0, nBytes);
|
||||
}
|
||||
|
@ -62,7 +56,7 @@ public class JarUtil {
|
|||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.println("解压" + fileName + "出错---" + e.getMessage());
|
||||
System.out.println("解压" + fileName + "出错!" + e.getMessage());
|
||||
} finally {
|
||||
if (jf != null) {
|
||||
try {
|
||||
|
|
|
@ -32,7 +32,7 @@ public class JmsUtil {
|
|||
* @param destination
|
||||
* @param objectMessage
|
||||
*/
|
||||
public void sendMessage(JmsTemplate jmsTemplate, Destination destination, final Serializable objectMessage) {
|
||||
public static void sendMessage(JmsTemplate jmsTemplate, Destination destination, final Serializable objectMessage) {
|
||||
jmsTemplate.send(destination, new MessageCreator() {
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
return session.createObjectMessage(objectMessage);
|
||||
|
@ -47,7 +47,7 @@ public class JmsUtil {
|
|||
* @param objectMessage
|
||||
* @param delay
|
||||
*/
|
||||
public void sendMessageDelay(JmsTemplate jmsTemplate, Destination destination, final Serializable objectMessage, final long delay) {
|
||||
public static void sendMessageDelay(JmsTemplate jmsTemplate, Destination destination, final Serializable objectMessage, final long delay) {
|
||||
jmsTemplate.send(destination, new MessageCreator() {
|
||||
public Message createMessage(Session session) throws JMSException {
|
||||
ObjectMessage om = session.createObjectMessage(objectMessage);
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
package com.zheng.common.util;
|
||||
|
||||
import org.apache.commons.lang.ObjectUtils;
|
||||
import org.apache.velocity.VelocityContext;
|
||||
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.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 代码生成类
|
||||
* Created by ZhangShuzheng on 2017/1/10.
|
||||
*/
|
||||
public class MybatisGeneratorUtil {
|
||||
|
||||
// 模板路径
|
||||
private static String VM_PATH = "zheng-common/src/main/resources/generatorConfig.vm";
|
||||
// 项目名称
|
||||
private static String PROJECT_NAME = "zheng";
|
||||
// 数据库名称
|
||||
private static String DATABASE_NAME = "zheng";
|
||||
|
||||
/**
|
||||
* 根据模板生成generatorConfig.xml文件
|
||||
* @param module_prefix_name
|
||||
*/
|
||||
public static void generator(
|
||||
String jdbc_driver,
|
||||
String jdbc_url,
|
||||
String jdbc_username,
|
||||
String jdbc_password,
|
||||
String module_prefix_name) {
|
||||
String module_path = PROJECT_NAME + "-" + module_prefix_name.replaceAll("\\.", "-") + "/" + PROJECT_NAME + "-" + module_prefix_name.replaceAll("\\.", "-") + "-dao/src/main/resources/generatorConfig.xml";
|
||||
String sql = "SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE table_schema = '" + DATABASE_NAME + "' AND table_name LIKE '" + module_prefix_name.replaceAll("\\.", "_") + "_%';";
|
||||
System.out.println("========== 开始生成generatorConfig.xml文件 ==========");
|
||||
try {
|
||||
VelocityContext context= new VelocityContext();
|
||||
List<Map<String, Object>> tables = new ArrayList<>();
|
||||
Map<String, Object> table = null;
|
||||
|
||||
// 查询定制前缀项目的所有表
|
||||
JdbcUtil jdbcUtil = new JdbcUtil(jdbc_driver, jdbc_url, jdbc_username, jdbc_password);
|
||||
List<Map> result = jdbcUtil.selectByParams(sql, null);
|
||||
for (Map map : result) {
|
||||
System.out.println(map.get("TABLE_NAME"));
|
||||
table = new HashMap<>();
|
||||
table.put("table_name", map.get("TABLE_NAME"));
|
||||
table.put("model_name", StringUtil.lineToHump(ObjectUtils.toString(map.get("TABLE_NAME"))));
|
||||
tables.add(table);
|
||||
}
|
||||
jdbcUtil.release();
|
||||
|
||||
String targetProject = PROJECT_NAME + "-" + module_prefix_name.replaceAll("\\.", "-") + "/" + PROJECT_NAME + "-" + module_prefix_name.replaceAll("\\.", "-") + "-dao";
|
||||
context.put("tables", tables);
|
||||
context.put("generator_javaModelGenerator_targetPackage", "com." + PROJECT_NAME + "." + module_prefix_name + ".dao.model");
|
||||
context.put("generator_sqlMapGenerator_targetPackage", "com." + PROJECT_NAME + "." + module_prefix_name + ".dao.mapper");
|
||||
context.put("generator_javaClientGenerator_targetPackage", "com." + PROJECT_NAME + "." + module_prefix_name + ".dao.mapper");
|
||||
context.put("targetProject", targetProject);
|
||||
VelocityUtil.generate(VM_PATH, module_path, context);
|
||||
// 删除旧代码
|
||||
deleteDir(new File(targetProject + "/src/main/java/com/" + PROJECT_NAME + "/" + module_prefix_name.replaceAll("\\.", "/") + "/dao/model"));
|
||||
deleteDir(new File(targetProject + "/src/main/java/com/" + PROJECT_NAME + "/" + module_prefix_name.replaceAll("\\.", "/") + "/dao/mapper"));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("========== 结束生成generatorConfig.xml文件 ==========");
|
||||
System.out.println("========== 开始运行MybatisGenerator ==========");
|
||||
// 生成代码
|
||||
try {
|
||||
List<String> warnings = new ArrayList<>();
|
||||
File configFile = new File(MybatisGeneratorUtil.class.getResource("/generatorConfig.xml").getFile());
|
||||
ConfigurationParser cp = new ConfigurationParser(warnings);
|
||||
Configuration config = cp.parseConfiguration(configFile);
|
||||
DefaultShellCallback callback = new DefaultShellCallback(true);
|
||||
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
|
||||
myBatisGenerator.generate(null);
|
||||
for (String warning : warnings) {
|
||||
System.out.println(warning);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
System.out.println("========== 结束运行MybatisGenerator ==========");
|
||||
}
|
||||
|
||||
// 递归删除非空文件夹
|
||||
public static void deleteDir(File dir) {
|
||||
if (dir.isDirectory()) {
|
||||
File[] files = dir.listFiles();
|
||||
for (int i = 0; i < files.length; i ++) {
|
||||
deleteDir(files[i]);
|
||||
}
|
||||
}
|
||||
dir.delete();
|
||||
}
|
||||
|
||||
}
|
|
@ -7,7 +7,6 @@ import java.util.ResourceBundle;
|
|||
|
||||
/**
|
||||
* 资源文件读取工具
|
||||
*
|
||||
* @author shuzheng
|
||||
* @date 2016年10月15日
|
||||
*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.zheng.pay.dao;
|
||||
|
||||
import com.zheng.common.util.MybatisGeneratorConfigUtil;
|
||||
import com.zheng.common.util.MybatisGeneratorUtil;
|
||||
import com.zheng.common.util.PropertiesFileUtil;
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,7 @@ public class Generator {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
MybatisGeneratorConfigUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.zheng.cms.dao;
|
||||
|
||||
import com.zheng.common.util.MybatisGeneratorConfigUtil;
|
||||
import com.zheng.common.util.MybatisGeneratorUtil;
|
||||
import com.zheng.common.util.PropertiesFileUtil;
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,7 @@ public class Generator {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
MybatisGeneratorConfigUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.zheng.upms.dao;
|
||||
|
||||
import com.zheng.common.util.MybatisGeneratorConfigUtil;
|
||||
import com.zheng.common.util.MybatisGeneratorUtil;
|
||||
import com.zheng.common.util.PropertiesFileUtil;
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,7 @@ public class Generator {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
MybatisGeneratorConfigUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package com.zheng.upms.dao;
|
||||
|
||||
import com.zheng.common.util.MybatisGeneratorConfigUtil;
|
||||
import com.zheng.common.util.MybatisGeneratorUtil;
|
||||
import com.zheng.common.util.PropertiesFileUtil;
|
||||
|
||||
/**
|
||||
|
@ -21,7 +21,7 @@ public class Generator {
|
|||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
MybatisGeneratorConfigUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
MybatisGeneratorUtil.generator(JDBC_DRIVER, JDBC_URL, JDBC_USERNAME, JDBC_PASSWORD, MODULE_PREFIX_NAME);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue