package com.gsafety.yujing.wechat.util;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.ShellCallback;
import org.mybatis.generator.config.*;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
/**
* 生成mysql数据库po和mapper工具类
*
* @author ly 20200413
*
*/
public class CommonMybatisGenerator {
private static final Logger LOGGER = LoggerFactory.getLogger(CommonMybatisGenerator.class);
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static void main(String[] args) {
// 本地mysql的jar包路径
String jdbcJarPath = "D:\\repositoryLocal/repository-aliyun/mysql/mysql-connector-java/5.1.46/mysql-connector-java-5.1.46.jar";
// 连接
String dbUrl = "jdbc:MYSQL://172.17.38.196:3306/";
String dbName = "jiangxiyjnew";
String user = "root";
String pass = "Intel@123456";
// 项目所在目录
String projectSrc = "E:\\workSpace/JiangxiMis/newjiangxi_mis/otherSystem/jiangxiyj-terminal";// 本地项目所在目录
String parentProjectName = "";
String projectName = "WECHAT";// 本地项目名
String packageName = "com.gsafety.yujing.wechat";
boolean createInterfaces = true;// 第一次执行时设置为true
String[] tableNames = {"statistics"};
generate(dbUrl + dbName, dbName, user, pass, projectSrc, parentProjectName, projectName, packageName,
createInterfaces, jdbcJarPath, tableNames);
}
public static void generate(String dbUrl, String dbName, String user, String pass, String projectSrc,
String parentProjectName, String projectName, String packageName, boolean createInterfaces,
String jdbcJarPath, String... tableNames) {
long start = System.currentTimeMillis();
Configuration configuration = new Configuration();
for (String tbName : tableNames) {
String seperator = "\\";
if (System.getProperties().getProperty("os.name").toLowerCase().contains("mac")) {
seperator = "/";
}
String base = projectSrc + seperator + parentProjectName + seperator + projectName;
String suffix = seperator + "src" + seperator + "main" + seperator + "java";
String suffixMap = seperator + "src" + seperator + "main" + seperator + "resources";
configuration.addContext(getContext(configuration, dbUrl, dbName, user, pass, tbName, createInterfaces,
jdbcJarPath, base + suffix, packageName, base + suffixMap));
}
generate(configuration);
if (LOGGER.isInfoEnabled()) {
LOGGER.info("generate success! excute time is {} millis!", System.currentTimeMillis() - start);
}
}
private static void generate(Configuration configuration) {
ShellCallback shellCallback = new DefaultShellCallback(true);
List<String> warnings = new ArrayList<String>();
try {
MyBatisGenerator generator = new MyBatisGenerator(configuration, shellCallback, warnings);
generator.generate(null);
} catch (InvalidConfigurationException | SQLException | IOException | InterruptedException e) {
e.printStackTrace();
}
}
/**
* java model configuration
*
* @param targetModelProject
* @param packageName
* @return
*/
private static JavaModelGeneratorConfiguration getJavaModelGeneratorConfiguration(String targetModelProject,
String packageName) {
JavaModelGeneratorConfiguration javaModelGeneratorConfiguration = new JavaModelGeneratorConfiguration();
javaModelGeneratorConfiguration.setTargetProject(targetModelProject);
javaModelGeneratorConfiguration.getProperties().setProperty("constructorBased", "true");
javaModelGeneratorConfiguration.setTargetPackage(packageName);
return javaModelGeneratorConfiguration;
}
private static SqlMapGeneratorConfiguration getSqlMapGeneratorConfiguration(String targetDalProject,
String packageName) {
SqlMapGeneratorConfiguration sqlMapGeneratorConfiguration = new SqlMapGeneratorConfiguration();
sqlMapGeneratorConfiguration.setTargetProject(targetDalProject);
sqlMapGeneratorConfiguration.setTargetPackage(packageName);
return sqlMapGeneratorConfiguration;
}
/**
* 生成mybatis dao接口
*
* @param targetDalProject
* @param packageName
* @param createIntefaces
* 是否生成接口,if true,新生成的接口不包括后来手动添加的接口方法
* @return
*/
private static JavaClientGeneratorConfiguration getJavaClientGeneratorConfiguration(String targetDalProject,
String packageName, boolean createIntefaces) {
if (!createIntefaces)
return null;
JavaClientGeneratorConfiguration javaClientGeneratorConfiguration = new JavaClientGeneratorConfiguration();
javaClientGeneratorConfiguration.setConfigurationType("XMLMAPPER");
javaClientGeneratorConfiguration.setTargetProject(targetDalProject);
javaClientGeneratorConfiguration.setTargetPackage(packageName);
return javaClientGeneratorConfiguration;
}
private static TableConfiguration getTableConfiguration(String dbName, String tbName, Context context) {
TableConfiguration tc = new TableConfiguration(context);
tc.setSchema(dbName);
tc.setTableName(tbName);
tc.setCountByExampleStatementEnabled(true);
tc.setUpdateByExampleStatementEnabled(true);
tc.setDeleteByExampleStatementEnabled(true);
tc.setSelectByExampleStatementEnabled(true);
tc.setSelectByExampleQueryId("true");
tc.setWildcardEscapingEnabled(false);
Properties properties = tc.getProperties();
properties.setProperty("useActualColumnNames", "false");
properties.setProperty("ignoreQualifiersAtRuntime", "true");
return tc;
}
private static JDBCConnectionConfiguration getJDBCConnectionConfiguration(String url, String user, String pass) {
JDBCConnectionConfiguration jdbcConnectionConfiguration = new JDBCConnectionConfiguration();
jdbcConnectionConfiguration.setDriverClass(DRIVER_CLASS);
jdbcConnectionConfiguration.setConnectionURL(url);
jdbcConnectionConfiguration.setUserId(user);
jdbcConnectionConfiguration.setPassword(pass);
return jdbcConnectionConfiguration;
}
/**
*
* @param configuration
* @param tbName
* @param createInterfaces
* @param jdbcJarPath
* @param storePath
* @return
*/
private static Context getContext(Configuration configuration, String url, String dbName, String name, String pass,
String tbName, boolean createInterfaces, String jdbcJarPath, String storePath, String packageName,
String mapStorePath) {
Context context = new Context(ModelType.FLAT);
context.setId("DB2Tables");
context.setTargetRuntime("MyBatis3");
configuration.addClasspathEntry(jdbcJarPath);
context.setJdbcConnectionConfiguration(getJDBCConnectionConfiguration(url, name, pass));
context.setJavaModelGeneratorConfiguration(getJavaModelGeneratorConfiguration(storePath, packageName + ".po"));
context.setSqlMapGeneratorConfiguration(getSqlMapGeneratorConfiguration(mapStorePath, ".mapping"));
context.setJavaClientGeneratorConfiguration(
getJavaClientGeneratorConfiguration(storePath, packageName + ".mapper", createInterfaces));
context.addTableConfiguration(getTableConfiguration(dbName, tbName, context));
return context;
}
}
其中的路径问题 自己修改一下即可。会生成po和mapper、xml