Skip to content

Commit 3ebc9ba

Browse files
committed
feat: add LastInsertIdSqlStatement option
1 parent 36949a5 commit 3ebc9ba

File tree

5 files changed

+65
-2
lines changed

5 files changed

+65
-2
lines changed

src/main/java/com/spawpaw/mybatis/generator/gui/ProjectConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,8 @@ public class ProjectConfig {
204204
@ExportToPlugin(plugin = DeclaredPlugins.SCVXGeneratorPlugin)
205205
@Config(bundle = "project.primaryKey", type = ConfigType.CheckableTextField)
206206
public SimpleStringProperty primaryKey = new SimpleStringProperty("");
207+
@Config(bundle = "project.lastInsertIdSqlStatement", testRegex = "\nCloudscape\nDB2\nDB2_MF\nDB2_MF\nHSQLDB\nInformix\nMySql\nSqlServer\nSYBASE\nJDBC", type = ConfigType.ComboBox)
208+
public SimpleStringProperty lastInsertIdSqlStatement = new SimpleStringProperty("");
207209

208210
@EnablePlugin(DeclaredPlugins.VirtualPrimaryKeyPlugin)
209211
@ExportToPlugin(plugin = DeclaredPlugins.VirtualPrimaryKeyPlugin, key = "virtualKeyColumns")

src/main/java/com/spawpaw/mybatis/generator/gui/util/MBGRunner.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,12 @@ else if (projectConfig.defaultModelType.getValue().equalsIgnoreCase("FLAT"))
154154
tableConfiguration.addProperty("virtualKeyColumns", projectConfig.enableVirtualPrimaryKeyPlugin.getValue());
155155

156156
//see http://www.mybatis.org/generator/configreference/generatedKey.html ,JDBC is a database independent method of obtaining the value from identity columns,only for Mybatis3+
157-
if (!projectConfig.primaryKey.getValue().isEmpty())
158-
tableConfiguration.setGeneratedKey(new GeneratedKey(projectConfig.primaryKey.getValue(), DatabaseType.valueOf(databaseConfig.databaseType.getValue()).getSqlStatement(), true, null));
157+
if (!projectConfig.primaryKey.getValue().isEmpty()) {
158+
String sqlStatement = DatabaseType.valueOf(databaseConfig.databaseType.getValue()).getSqlStatement();
159+
if (!projectConfig.lastInsertIdSqlStatement.getValue().trim().isEmpty())//如果指定了获取自增主键的sql,则覆盖默认的配置
160+
sqlStatement = projectConfig.lastInsertIdSqlStatement.getValue();
161+
tableConfiguration.setGeneratedKey(new GeneratedKey(projectConfig.primaryKey.getValue(), sqlStatement, true, null));
162+
}
159163

160164
//添加忽略列/列覆写
161165
for (TableColumnMetaData column : databaseConfig.tableConfigs.get(projectConfig.selectedTable.getValue())) {

src/main/resources/i18n/locale.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,22 @@ project.autoDelimitKeywords.helpText=If not empty, then MBG will delimit SQL key
413413
if you want to customize both of it ,please change the sourcecode in `com.spawpaw.mybatis.generator.gui.util.MBGRunner`
414414
project.autoDelimitKeywords.promptText=disabled if empty
415415
##
416+
417+
project.lastInsertId.labelText=LastInsertIdSqlStatement
418+
project.lastInsertId.helpText=The SQL statement that will return the new value. \n\
419+
If this is an identity column, then you can use one of the predefined special values, \n\
420+
or substitute the proper statement for your database. The predefined special values are as follows:\n\
421+
Cloudscape This will translate to: VALUES IDENTITY_VAL_LOCAL()\n\
422+
DB2\t\t This will translate to: VALUES IDENTITY_VAL_LOCAL()\n\
423+
DB2_MF\t This will translate to:SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1\n\
424+
\t\t\tUse this value for DB2 on zOS (Main Frames) and, in some cases, iSeries (AS/400)\n\
425+
Derby\t This will translate to: VALUES IDENTITY_VAL_LOCAL()\n\
426+
HSQLDB\t This will translate to: CALL IDENTITY()\n\
427+
Informix\t This will translate to: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1\n\
428+
MySql\t This will translate to: SELECT LAST_INSERT_ID()\n\
429+
SqlServer\t This will translate to: SELECT SCOPE_IDENTITY()\n\
430+
SYBASE\t This will translate to: SELECT @@IDENTITY\n\
431+
JDBC\t This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.
432+
433+
project.lastInsertId.promptText=use default if absent.
434+
##

src/main/resources/i18n/locale_en.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,3 +413,22 @@ project.autoDelimitKeywords.helpText=If not empty, then MBG will delimit SQL key
413413
if you want to customize both of it ,please change the sourcecode in `com.spawpaw.mybatis.generator.gui.util.MBGRunner`
414414
project.autoDelimitKeywords.promptText=disabled if empty
415415
##
416+
417+
project.lastInsertIdSqlStatement.labelText=LastInsertIdSqlStatement
418+
project.lastInsertIdSqlStatement.helpText=The SQL statement that will return the new value. \n\
419+
If this is an identity column, then you can use one of the predefined special values, \n\
420+
or substitute the proper statement for your database. The predefined special values are as follows:\n\
421+
Cloudscape This will translate to: VALUES IDENTITY_VAL_LOCAL()\n\
422+
DB2\t\t This will translate to: VALUES IDENTITY_VAL_LOCAL()\n\
423+
DB2_MF\t This will translate to:SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1\n\
424+
\t\t\tUse this value for DB2 on zOS (Main Frames) and, in some cases, iSeries (AS/400)\n\
425+
Derby\t This will translate to: VALUES IDENTITY_VAL_LOCAL()\n\
426+
HSQLDB\t This will translate to: CALL IDENTITY()\n\
427+
Informix\t This will translate to: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1\n\
428+
MySql\t This will translate to: SELECT LAST_INSERT_ID()\n\
429+
SqlServer\t This will translate to: SELECT SCOPE_IDENTITY()\n\
430+
SYBASE\t This will translate to: SELECT @@IDENTITY\n\
431+
JDBC\t This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.
432+
433+
project.lastInsertIdSqlStatement.promptText=use default if absent.
434+
##

src/main/resources/i18n/locale_zh_CN.properties

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,22 @@ project.autoDelimitKeywords.helpText=\u5982\u679C\u8BE5\u9879\u4E0D\u4E3A\u7A7A\
454454
\u5982\u679C\u4F60\u60F3\u8BA9\u5B83\u4EEC\u4E0D\u540C\uFF0C\u8BF7\u81EA\u884C\u66F4\u6539`com.spawpaw.mybatis.generator.gui.util.MBGRunner`\u4E2D\u7684\u4EE3\u7801
455455
project.autoDelimitKeywords.promptText=disabled if empty
456456
##
457+
458+
project.lastInsertIdSqlStatement.labelText=\u83B7\u53D6\u81EA\u589E\u4E3B\u952ESQL
459+
project.lastInsertIdSqlStatement.helpText=\u7528\u4E8E\u83B7\u53D6\u81EA\u589E\u4E3B\u952E\u7684SQL\u8BED\u53E5\uFF0C\u5982\u679C\u4E0D\u6307\u5B9A\uFF0C\u5219\u4F7F\u7528\u9ED8\u8BA4\u7684\u914D\u7F6E\uFF08\u5982\u679C\u60A8\u7684\u6570\u636E\u5E93\u5728\u4EE5\u4E0B\u9884\u5B9A\u4E49\u7684\u5217\u8868\u4E2D\uFF0C\u5219\u4F7F\u7528\u9884\u5B9A\u4E49\u7684\u503C\uFF0C\u5426\u5219\u4F7F\u7528`JDBC`\uFF09 \n\
460+
\u4F60\u53EF\u4EE5\u81EA\u5DF1\u6307\u5B9A\u83B7\u53D6\u81EA\u589E\u4E3B\u952E\u7684SQL\u8BED\u53E5\u6216\u8005\u4F7F\u7528\u9884\u5B9A\u4E49\u7684\u503C\n\
461+
\u4EE5\u4E0B\u662F\u9884\u5B9A\u4E49\u7684\u503C\uFF1A\n\
462+
Cloudscape \u5C06\u88AB\u8F6C\u6362\u6210: VALUES IDENTITY_VAL_LOCAL()\n\
463+
DB2\t\t \u5C06\u88AB\u8F6C\u6362\u6210: VALUES IDENTITY_VAL_LOCAL()\n\
464+
DB2_MF\t \u5C06\u88AB\u8F6C\u6362\u6210:SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1\n\
465+
\t\t\tUse this value for DB2 on zOS (Main Frames) and, in some cases, iSeries (AS/400)\n\
466+
Derby\t \u5C06\u88AB\u8F6C\u6362\u6210: VALUES IDENTITY_VAL_LOCAL()\n\
467+
HSQLDB\t \u5C06\u88AB\u8F6C\u6362\u6210: CALL IDENTITY()\n\
468+
Informix\t \u5C06\u88AB\u8F6C\u6362\u6210: select dbinfo('sqlca.sqlerrd1') from systables where tabid=1\n\
469+
MySql\t \u5C06\u88AB\u8F6C\u6362\u6210: SELECT LAST_INSERT_ID()\n\
470+
SqlServer\t \u5C06\u88AB\u8F6C\u6362\u6210: SELECT SCOPE_IDENTITY()\n\
471+
SYBASE\t \u5C06\u88AB\u8F6C\u6362\u6210: SELECT @@IDENTITY\n\
472+
JDBC\t This will configure MBG to generate code for MyBatis3 suport of JDBC standard generated keys. This is a database independent method of obtaining the value from identity columns.
473+
474+
project.lastInsertIdSqlStatement.promptText=\u5982\u4E3A\u7A7A\uFF0C\u5219\u4F7F\u7528\u9ED8\u8BA4\u7684\u8BBE\u7F6E\u3002
475+
##

0 commit comments

Comments
 (0)