init: 导入项目
This commit is contained in:
40
mybatis-generator/pom.xml
Normal file
40
mybatis-generator/pom.xml
Normal file
@@ -0,0 +1,40 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.example</groupId>
|
||||
<artifactId>mybatis-generator</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.mybatis.generator</groupId>
|
||||
<artifactId>mybatis-generator-core</artifactId>
|
||||
<version>1.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>31.1-jre</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>1.18.24</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>8.0.30</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.generator;
|
||||
|
||||
import org.mybatis.generator.exception.InvalidConfigurationException;
|
||||
import org.mybatis.generator.exception.XMLParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @program: ShSecpSystem
|
||||
* @description: 生成器启动器
|
||||
* @author: fengjun
|
||||
* @create: 2021-10-30
|
||||
*/
|
||||
public class GeneratorMain {
|
||||
//模块包(xml)
|
||||
public static final String MODULE_PACKAGE = "mapper";
|
||||
private static final JdbcParams jdbc = new JdbcParams();
|
||||
static {
|
||||
jdbc.setDriveClass("com.mysql.cj.jdbc.Driver");
|
||||
jdbc.setUrl("jdbc:mysql://122.5.19.222:3909/ag_devmysql");
|
||||
jdbc.setUsername("root");
|
||||
jdbc.setPassword("agmysqlroot");
|
||||
}
|
||||
|
||||
//不考虑用户
|
||||
static class Generator{
|
||||
public static void main(String[] args) throws XMLParserException, SQLException, IOException, InterruptedException, InvalidConfigurationException {
|
||||
String tableNames = "teacher";
|
||||
String[] tableNameArray = tableNames.split(",");
|
||||
for (String tableName : tableNameArray) {
|
||||
GeneratorStarter.generate(tableName,jdbc);
|
||||
}
|
||||
}
|
||||
}
|
||||
//考虑用户 ,同一个用户
|
||||
static class GeneratorMultiple{
|
||||
public static void main(String[] args) throws XMLParserException, SQLException, IOException, InterruptedException, InvalidConfigurationException {
|
||||
String tableNames = "FA_VBILL_TRUCK,FA_VBILL_CUSTOM";
|
||||
String tableSchema = "AG_SECP_SHHH";
|
||||
String[] tableNameArray = tableNames.split(",");
|
||||
for (String tableName : tableNameArray) {
|
||||
GeneratorStarter.generate(tableName,tableSchema,jdbc);
|
||||
}
|
||||
}
|
||||
}
|
||||
// //考虑用户 ,不同用户
|
||||
// static class GeneratorMultipleDiff{
|
||||
// public static void main(String[] args) throws XMLParserException, SQLException, IOException, InterruptedException, InvalidConfigurationException {
|
||||
// String tableNames = "CSBK_BILL";
|
||||
// String[] tableNameArray = tableNames.split(",");
|
||||
// for (String tableName : tableNameArray) {
|
||||
// GeneratorStarter.generate(tableName);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
//基础路径
|
||||
public static final String BASE_PATH = "E:\\IdeaProject\\AG_MYSQL_PLATFORM3.0\\IlogDynamicModule\\src\\main\\";
|
||||
//domain包路径
|
||||
public static final String DOMAIN_PACKAGE = "com.ag.example.domain";
|
||||
//pojo包路径
|
||||
public static final String POJO_PACKAGE = "com.ag.example.pojo";
|
||||
//xml 项目位置
|
||||
public static final String SQL_PROJECT = BASE_PATH+"resources\\"+MODULE_PACKAGE;
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package org.generator;
|
||||
|
||||
import com.google.common.base.CaseFormat;
|
||||
import org.mybatis.generator.api.MyBatisGenerator;
|
||||
import org.mybatis.generator.config.*;
|
||||
import org.mybatis.generator.config.xml.ConfigurationParser;
|
||||
import org.mybatis.generator.exception.InvalidConfigurationException;
|
||||
import org.mybatis.generator.exception.XMLParserException;
|
||||
import org.mybatis.generator.internal.DefaultCommentGenerator;
|
||||
import org.mybatis.generator.internal.DefaultShellCallback;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* @program: ShSecpSystem
|
||||
* @description: 生成器程序
|
||||
* @author: fengjun
|
||||
* @create: 2021-10-30
|
||||
*/
|
||||
public class GeneratorStarter {
|
||||
public static void generate(String tableName, JdbcParams jdbc) throws XMLParserException, SQLException, IOException, InterruptedException, InvalidConfigurationException {
|
||||
HashMap<String, String> tableConfig = new HashMap<String, String>();
|
||||
String upperBeanName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName);
|
||||
tableConfig.put("tableName", tableName);
|
||||
tableConfig.put("domainObjectName", upperBeanName);
|
||||
stater(tableConfig, jdbc);
|
||||
}
|
||||
|
||||
public static void generate(String tableName, String schema, JdbcParams jdbc) throws XMLParserException, SQLException, IOException, InterruptedException, InvalidConfigurationException {
|
||||
HashMap<String, String> tableConfig = new HashMap<String, String>();
|
||||
String upperBeanName = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, tableName);
|
||||
tableConfig.put("tableName", tableName);
|
||||
tableConfig.put("domainObjectName", upperBeanName);
|
||||
tableConfig.put("schema", schema);
|
||||
stater(tableConfig, jdbc);
|
||||
}
|
||||
|
||||
private static void stater(HashMap<String, String> tableConfig, JdbcParams jdbc) throws XMLParserException, IOException, SQLException, InterruptedException, InvalidConfigurationException {
|
||||
String configFileName = "mybatisGen.xml";
|
||||
List<String> warnings = new ArrayList<>();
|
||||
// System.out.println(configFileName);
|
||||
// String rfp = GeneratorMain.class.getResource("/" + configFileName).getFile();
|
||||
// File configFile = new File(rfp);
|
||||
// ConfigurationParser cp = new ConfigurationParser(warnings);
|
||||
// Configuration config = cp.parseConfiguration(configFile);
|
||||
Configuration config = new Configuration();
|
||||
DefaultShellCallback callback = new DefaultShellCallback(true);
|
||||
MyBatisGenerator myBatisGenerator;
|
||||
//hashmap
|
||||
setConfiguration(config, tableConfig, jdbc);
|
||||
myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
|
||||
myBatisGenerator.generate(null);
|
||||
}
|
||||
|
||||
private static void setConfiguration(Configuration cft, HashMap<String, String> tableConfig, JdbcParams jdbc) {
|
||||
if (cft != null) {
|
||||
|
||||
Context context = new Context(ModelType.CONDITIONAL);
|
||||
cft.addContext(context);
|
||||
context.setId("mbg");
|
||||
context.setTargetRuntime("MyBatis3");
|
||||
|
||||
TableConfiguration tableConf = new TableConfiguration(context);
|
||||
//设置表名
|
||||
String tableName = tableConfig.get("tableName");
|
||||
if (tableName != null && !"".equals(tableName)) {
|
||||
tableConf.setTableName(tableName);
|
||||
}
|
||||
//如果有用户名 需要设置用户名
|
||||
if (tableConfig.containsKey("schema")) {
|
||||
tableConf.setSchema(tableConfig.get("schema"));
|
||||
}
|
||||
//设置生成的实体类的名称
|
||||
String domainObjectName = tableConfig.get("domainObjectName");
|
||||
if (domainObjectName != null && !"".equals(domainObjectName)) {
|
||||
tableConf.setDomainObjectName(domainObjectName);
|
||||
}
|
||||
tableConf.setCountByExampleStatementEnabled(false);
|
||||
tableConf.setDeleteByExampleStatementEnabled(false);
|
||||
tableConf.setSelectByExampleStatementEnabled(false);
|
||||
tableConf.setUpdateByExampleStatementEnabled(false);
|
||||
context.addTableConfiguration(tableConf);
|
||||
|
||||
//数据库
|
||||
|
||||
JDBCConnectionConfiguration jdbcConnection = new JDBCConnectionConfiguration();
|
||||
jdbcConnection.setDriverClass(jdbc.getDriveClass());
|
||||
jdbcConnection.setConnectionURL(jdbc.getUrl());
|
||||
jdbcConnection.setUserId(jdbc.getUsername());
|
||||
jdbcConnection.setPassword(jdbc.getPassword());
|
||||
// https://mybatis.org/generator/usage/mysql.html
|
||||
jdbcConnection.getProperties().setProperty("nullCatalogMeansCurrent", "true");
|
||||
context.setJdbcConnectionConfiguration(jdbcConnection);
|
||||
|
||||
//java实体类配置
|
||||
JavaModelGeneratorConfiguration javaMode = new JavaModelGeneratorConfiguration();
|
||||
//设置model实体类对应的包路径,以及文件存放路径 targetProject可以指定具体的路径,如./src/main/java
|
||||
javaMode.setTargetPackage(GeneratorMain.POJO_PACKAGE);
|
||||
javaMode.setTargetProject(GeneratorMain.BASE_PATH + "java");
|
||||
javaMode.getProperties().setProperty("enableSubPackages", "false");
|
||||
javaMode.getProperties().setProperty("trimStrings", "true");
|
||||
// javaMode.getProperties().setProperty("rootClass", "com.ag.dynamicmodule.service.BaseEntiy");
|
||||
context.setJavaModelGeneratorConfiguration(javaMode);
|
||||
|
||||
//对应的mapper.xml文件
|
||||
SqlMapGeneratorConfiguration sqlMap = new SqlMapGeneratorConfiguration();
|
||||
sqlMap.setTargetPackage(".");
|
||||
sqlMap.setTargetProject(GeneratorMain.SQL_PROJECT);
|
||||
sqlMap.getProperties().setProperty("enableSubPackages", "false");
|
||||
context.setSqlMapGeneratorConfiguration(sqlMap);
|
||||
|
||||
//对应mapper接口
|
||||
JavaClientGeneratorConfiguration javaClient = new JavaClientGeneratorConfiguration();
|
||||
javaClient.setTargetPackage(GeneratorMain.DOMAIN_PACKAGE + ".mapper");
|
||||
javaClient.setTargetProject(GeneratorMain.BASE_PATH + "java");
|
||||
javaClient.setConfigurationType("XMLMAPPER");
|
||||
context.setJavaClientGeneratorConfiguration(javaClient);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.generator;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @description:
|
||||
* @author: fengjun
|
||||
* @time: 2022/12/5 10:33
|
||||
*/
|
||||
@Data
|
||||
public class JdbcParams {
|
||||
String driveClass;
|
||||
String url;
|
||||
String username;
|
||||
String password;
|
||||
}
|
||||
Reference in New Issue
Block a user