Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mybatis-generator-gui是基于[mybatis generator](http://www.mybatis.org/generat
* 把数据库中表列的注释生成为Java实体的注释,生成的实体清晰明了
* 可选的去除掉对版本管理不友好的注释,这样新增或删除字段重新生成的文件比较过来清楚
* 目前已经支持Mysql、Mysql8、Oracle、PostgreSQL与SQL Server,暂不对其他非主流数据库提供支持。(MySQL支持的比较好,其他数据库有什么问题可以在issue中反馈)

* 已支持oracle分页,批量 增、删、改 操作
### 要求
本工具由于使用了Java 8的众多特性,所以要求JDK <strong>1.8.0.60</strong>以上版本,另外<strong>JDK 1.9</strong>暂时还不支持。

Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<artifactId>jsch</artifactId>
<version>0.1.54</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.misterchangray.mybatis.generator.plugins/myBatisGeneratorPlugins -->
<dependency>
<groupId>com.github.misterchangray.mybatis.generator.plugins</groupId>
<artifactId>myBatisGeneratorPlugins</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -103,4 +109,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.zzg.mybatis.generator.bridge;

import com.jcraft.jsch.Session;
import com.zzg.mybatis.generator.controller.PictureProcessStateController;
import com.zzg.mybatis.generator.model.DatabaseConfig;
import com.zzg.mybatis.generator.model.DbType;
import com.zzg.mybatis.generator.model.GeneratorConfig;
Expand Down Expand Up @@ -31,7 +29,7 @@
*/
public class MybatisGeneratorBridge {

private static final Logger _LOG = LoggerFactory.getLogger(MybatisGeneratorBridge.class);
private static final Logger _LOG = LoggerFactory.getLogger(MybatisGeneratorBridge.class);

private GeneratorConfig generatorConfig;

Expand All @@ -58,13 +56,13 @@ public void generate() throws Exception {
Configuration configuration = new Configuration();
Context context = new Context(ModelType.CONDITIONAL);
configuration.addContext(context);

context.addProperty("javaFileEncoding", "UTF-8");
String dbType = selectedDatabaseConfig.getDbType();
String connectorLibPath = ConfigHelper.findConnectorLibPath(dbType);
_LOG.info("connectorLibPath: {}", connectorLibPath);
configuration.addClasspathEntry(connectorLibPath);

String dbType = selectedDatabaseConfig.getDbType();
String connectorLibPath = ConfigHelper.findConnectorLibPath(dbType);
_LOG.info("connectorLibPath: {}", connectorLibPath);
configuration.addClasspathEntry(connectorLibPath);
// Table configuration
TableConfiguration tableConfig = new TableConfiguration(context);
tableConfig.setTableName(generatorConfig.getTableName());
Expand All @@ -76,15 +74,15 @@ public void generate() throws Exception {
tableConfig.setSelectByExampleStatementEnabled(false);
}

context.addProperty("autoDelimitKeywords", "true");
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
tableConfig.setSchema(selectedDatabaseConfig.getSchema());
// 由于beginningDelimiter和endingDelimiter的默认值为双引号("),在Mysql中不能这么写,所以还要将这两个默认值改为`
context.addProperty("beginningDelimiter", "`");
context.addProperty("endingDelimiter", "`");
} else {
context.addProperty("autoDelimitKeywords", "true");
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
tableConfig.setSchema(selectedDatabaseConfig.getSchema());
// 由于beginningDelimiter和endingDelimiter的默认值为双引号("),在Mysql中不能这么写,所以还要将这两个默认值改为`
context.addProperty("beginningDelimiter", "`");
context.addProperty("endingDelimiter", "`");
} else {
tableConfig.setCatalog(selectedDatabaseConfig.getSchema());
}
}
if (generatorConfig.isUseSchemaPrefix()) {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
tableConfig.setSchema(selectedDatabaseConfig.getSchema());
Expand All @@ -96,12 +94,12 @@ public void generate() throws Exception {
}
}
// 针对 postgresql 单独配置
if (DbType.PostgreSQL.name().equals(dbType)) {
if (DbType.PostgreSQL.name().equals(dbType)) {
tableConfig.setDelimitIdentifiers(true);
}

//添加GeneratedKey主键生成
if (StringUtils.isNotEmpty(generatorConfig.getGenerateKeys())) {
if (StringUtils.isNotEmpty(generatorConfig.getGenerateKeys())) {
String dbType2 = dbType;
if (DbType.MySQL.name().equals(dbType2) || DbType.MySQL_8.name().equals(dbType)) {
dbType2 = "JDBC";
Expand All @@ -113,8 +111,8 @@ public void generate() throws Exception {
//当使用SelectKey时,Mybatis会使用SelectKeyGenerator,INSERT之后,多发送一次查询语句,获得主键值
//在上述读写分离被代理的情况下,会得不到正确的主键
}
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), dbType2, true, null));
}
tableConfig.setGeneratedKey(new GeneratedKey(generatorConfig.getGenerateKeys(), dbType2, true, null));
}

if (generatorConfig.getMapperName() != null) {
tableConfig.setMapperName(generatorConfig.getMapperName());
Expand All @@ -131,16 +129,16 @@ public void generate() throws Exception {
});
}
if (generatorConfig.isUseActualColumnNames()) {
tableConfig.addProperty("useActualColumnNames", "true");
tableConfig.addProperty("useActualColumnNames", "true");
}

if(generatorConfig.isUseTableNameAlias()){
if(generatorConfig.isUseTableNameAlias()){
tableConfig.setAlias(generatorConfig.getTableName());
}

JDBCConnectionConfiguration jdbcConfig = new JDBCConnectionConfiguration();
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)) {
jdbcConfig.addProperty("nullCatalogMeansCurrent", "true");
jdbcConfig.addProperty("nullCatalogMeansCurrent", "true");
}
jdbcConfig.setDriverClass(DbType.valueOf(dbType).getDriverClass());
jdbcConfig.setConnectionURL(DbUtil.getConnectionUrlWithSchema(selectedDatabaseConfig));
Expand Down Expand Up @@ -200,13 +198,27 @@ public void generate() throws Exception {
context.addPluginConfiguration(pluginConfiguration2);
}
// limit/offset插件

if (generatorConfig.isOffsetLimit()) {

if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
|| DbType.PostgreSQL.name().equals(dbType)) {
PluginConfiguration pluginConfiguration = new PluginConfiguration();
//亦可用 com.zzg.mybatis.generator.plugins.MySQLPaginationPlugin -- panda 2019年7月10日 17:02:57
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.MySQLLimitPlugin");
pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.MySQLLimitPlugin");
context.addPluginConfiguration(pluginConfiguration);
//selectByPage
PluginConfiguration pluginConfiguration1 = new PluginConfiguration();
pluginConfiguration1.addProperty("type", "com.zzg.mybatis.generator.plugins.PagePlugin");
pluginConfiguration1.setConfigurationType("com.zzg.mybatis.generator.plugins.PagePlugin");
context.addPluginConfiguration(pluginConfiguration1);
}else if (DbType.Oracle.name().equals(dbType)) {
//oracle 分页 -- panda
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.OraclePaginationPlugin");
pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.OraclePaginationPlugin");
context.addPluginConfiguration(pluginConfiguration);
}
}
//for JSR310
Expand Down Expand Up @@ -239,12 +251,57 @@ public void generate() throws Exception {
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
PluginConfiguration pluginConfiguration = new PluginConfiguration();
pluginConfiguration.addProperty("useExample", String.valueOf(generatorConfig.isUseExample()));
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
pluginConfiguration.addProperty("useExample", String.valueOf(generatorConfig.isUseExample()));
pluginConfiguration.addProperty("type", "com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
pluginConfiguration.setConfigurationType("com.zzg.mybatis.generator.plugins.CommonDAOInterfacePlugin");
context.addPluginConfiguration(pluginConfiguration);
}
}
//批量操作
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)
|| DbType.PostgreSQL.name().equals(dbType)) {
//批插
PluginConfiguration BatchInsertPlugin = new PluginConfiguration();
BatchInsertPlugin.addProperty("type", "com.zzg.mybatis.generator.plugins.BatchInsertPlugin");
BatchInsertPlugin.setConfigurationType("com.zzg.mybatis.generator.plugins.BatchInsertPlugin");
context.addPluginConfiguration(BatchInsertPlugin);
//批更
if (DbType.MySQL.name().equals(dbType) || DbType.MySQL_8.name().equals(dbType)){
PluginConfiguration MysqlBatchUpdatePlugin = new PluginConfiguration();
MysqlBatchUpdatePlugin.addProperty("type", "com.zzg.mybatis.generator.plugins.MysqlBatchUpdatePlugin");
MysqlBatchUpdatePlugin.setConfigurationType("com.zzg.mybatis.generator.plugins.MysqlBatchUpdatePlugin");
context.addPluginConfiguration(MysqlBatchUpdatePlugin);
}else {
PluginConfiguration PostgreBatchUpdatePlugin = new PluginConfiguration();
PostgreBatchUpdatePlugin.addProperty("type", "com.zzg.mybatis.generator.plugins.PostgreBatchUpdatePlugin");
PostgreBatchUpdatePlugin.setConfigurationType("com.zzg.mybatis.generator.plugins.PostgreBatchUpdatePlugin");
context.addPluginConfiguration(PostgreBatchUpdatePlugin);
}
}else if (DbType.Oracle.name().equals(dbType)) {
//oracle
//批量插入
PluginConfiguration OracleBatchInsertPlugin = new PluginConfiguration();
OracleBatchInsertPlugin.addProperty("type", "com.zzg.mybatis.generator.plugins.OracleBatchInsertPlugin");
OracleBatchInsertPlugin.setConfigurationType("com.zzg.mybatis.generator.plugins.OracleBatchInsertPlugin");
context.addPluginConfiguration(OracleBatchInsertPlugin);
//批更
PluginConfiguration OracleBatchUpdatePlugin = new PluginConfiguration();
OracleBatchUpdatePlugin.addProperty("type", "com.zzg.mybatis.generator.plugins.OracleBatchUpdatePlugin");
OracleBatchUpdatePlugin.setConfigurationType("com.zzg.mybatis.generator.plugins.OracleBatchUpdatePlugin");
context.addPluginConfiguration(OracleBatchUpdatePlugin);
}
//批删
PluginConfiguration BatchDeletePlugin = new PluginConfiguration();
BatchDeletePlugin.addProperty("type", "com.zzg.mybatis.generator.plugins.BatchDeletePlugin");
BatchDeletePlugin.setConfigurationType("com.zzg.mybatis.generator.plugins.BatchDeletePlugin");
context.addPluginConfiguration(BatchDeletePlugin);
//Set方法增强 扩展set方法,返回this实例;方便链式调用
PluginConfiguration ExtendEntitySetter = new PluginConfiguration();
ExtendEntitySetter.addProperty("type", "mybatis.generator.plugins.ExtendEntitySetter");
ExtendEntitySetter.setConfigurationType("mybatis.generator.plugins.ExtendEntitySetter");
context.addPluginConfiguration(ExtendEntitySetter);



context.setTargetRuntime("MyBatis3");

Expand All @@ -254,34 +311,34 @@ public void generate() throws Exception {
ShellCallback shellCallback = new DefaultShellCallback(true); // override=true
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(configuration, shellCallback, warnings);
// if overrideXML selected, delete oldXML ang generate new one
if (generatorConfig.isOverrideXML()) {
String mappingXMLFilePath = getMappingXMLFilePath(generatorConfig);
File mappingXMLFile = new File(mappingXMLFilePath);
if (mappingXMLFile.exists()) {
mappingXMLFile.delete();
}
}
if (generatorConfig.isOverrideXML()) {
String mappingXMLFilePath = getMappingXMLFilePath(generatorConfig);
File mappingXMLFile = new File(mappingXMLFilePath);
if (mappingXMLFile.exists()) {
mappingXMLFile.delete();
}
}
myBatisGenerator.generate(progressCallback, contexts, fullyqualifiedTables);
}

private String getMappingXMLFilePath(GeneratorConfig generatorConfig) {
StringBuilder sb = new StringBuilder();
sb.append(generatorConfig.getProjectFolder()).append("/");
sb.append(generatorConfig.getMappingXMLTargetFolder()).append("/");
String mappingXMLPackage = generatorConfig.getMappingXMLPackage();
if (StringUtils.isNotEmpty(mappingXMLPackage)) {
sb.append(mappingXMLPackage.replace(".", "/")).append("/");
}
if (StringUtils.isNotEmpty(generatorConfig.getMapperName())) {
sb.append(generatorConfig.getMapperName()).append(".xml");
} else {
sb.append(generatorConfig.getDomainObjectName()).append("Mapper.xml");
}

return sb.toString();
}

public void setProgressCallback(ProgressCallback progressCallback) {
StringBuilder sb = new StringBuilder();
sb.append(generatorConfig.getProjectFolder()).append("/");
sb.append(generatorConfig.getMappingXMLTargetFolder()).append("/");
String mappingXMLPackage = generatorConfig.getMappingXMLPackage();
if (StringUtils.isNotEmpty(mappingXMLPackage)) {
sb.append(mappingXMLPackage.replace(".", "/")).append("/");
}
if (StringUtils.isNotEmpty(generatorConfig.getMapperName())) {
sb.append(generatorConfig.getMapperName()).append(".xml");
} else {
sb.append(generatorConfig.getDomainObjectName()).append("Mapper.xml");
}

return sb.toString();
}

public void setProgressCallback(ProgressCallback progressCallback) {
this.progressCallback = progressCallback;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.zzg.mybatis.generator.plugins;

import com.zzg.mybatis.generator.util.BaseGenTool;
import com.zzg.mybatis.generator.util.MethodGeneratorTool;
import com.zzg.mybatis.generator.util.SqlMapperGeneratorTool;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Attribute;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.api.dom.xml.TextElement;
import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**
* <b><code>BatchDeletePlugin</code></b>
* <p/>
* Description:
* <p/>
* <b>Creation Time:</b> 2018/12/10 0:48.
*
* @author HuWeihui
*/
public class BatchDeletePlugin extends PluginAdapter {

private final static String BATCH_DELETE = "batchDelete";

private final static String PARAMETER_NAME = "ids";

@Override
public boolean validate(List<String> list) {
return true;
}


@Override
public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
if (BaseGenTool.isMybatisMode(introspectedTable)) {
MethodGeneratorTool.defaultBatchDeleteMethodGen(interfaze,introspectedTable,context);
}
return super.clientGenerated(interfaze, topLevelClass, introspectedTable);
}

@Override
public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
if (introspectedTable.getTargetRuntime().equals(IntrospectedTable.TargetRuntime.MYBATIS3)){
addSqlMapper(document, introspectedTable);
}
return super.sqlMapDocumentGenerated(document, introspectedTable);
}


/**
* 批量删除的xml方法生成
* @param document
* @param introspectedTable
*/
private void addSqlMapper(Document document, IntrospectedTable introspectedTable){
String tableName = introspectedTable.getFullyQualifiedTableNameAtRuntime();

String key = introspectedTable.getPrimaryKeyColumns().get(0).getActualColumnName();

String baseSql = String.format("delete from %s where %s in (",tableName,key);

FullyQualifiedJavaType paramType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType();

XmlElement deleteElement = SqlMapperGeneratorTool.baseElementGenerator(SqlMapperGeneratorTool.DELETE, BATCH_DELETE,paramType);

XmlElement foreachElement = SqlMapperGeneratorTool.baseForeachElementGenerator(PARAMETER_NAME,"item","index",null);

deleteElement.addElement(new TextElement(baseSql));

foreachElement.addAttribute(new Attribute("separator", ","));

foreachElement.addElement(new TextElement("#{item}"));

deleteElement.addElement(foreachElement);

deleteElement.addElement(new TextElement(")"));

document.getRootElement().addElement(deleteElement);
}
}

Loading