Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
M
mybatis-generator-plugin
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
mybatis-generator-plugin
Commits
9d0068df
Commit
9d0068df
authored
Mar 22, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
useGeneratedKeys:使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。抽取成一个工具方法
parent
38093075
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
38 deletions
+66
-38
src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java
...itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java
+5
-25
src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java
...om/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java
+6
-13
src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommTools.java
.../com/itfsw/mybatis/generator/plugins/utils/CommTools.java
+55
-0
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertOldPlugin.java
View file @
9d0068df
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.CommTools
;
import
com.itfsw.mybatis.generator.plugins.utils.CommentTools
;
import
com.itfsw.mybatis.generator.plugins.utils.CommentTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
...
@@ -28,7 +29,6 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
...
@@ -28,7 +29,6 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
import
org.mybatis.generator.codegen.mybatis3.ListUtilities
;
import
org.mybatis.generator.codegen.mybatis3.ListUtilities
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.GeneratedKey
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -170,18 +170,8 @@ public class BatchInsertOldPlugin extends PluginAdapter {
...
@@ -170,18 +170,8 @@ public class BatchInsertOldPlugin extends PluginAdapter {
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools
.
addComment
(
element
);
CommentTools
.
addComment
(
element
);
GeneratedKey
gk
=
introspectedTable
.
getGeneratedKey
();
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
if
(
gk
!=
null
)
{
CommTools
.
useGeneratedKeys
(
element
,
introspectedTable
);
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getColumn
(
gk
.
getColumn
());
// if the column is null, then it's a configuration error. The
// warning has already been reported
if
(
introspectedColumn
!=
null
)
{
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element
.
addAttribute
(
new
Attribute
(
"useGeneratedKeys"
,
"true"
));
//$NON-NLS-1$ //$NON-NLS-2$
element
.
addAttribute
(
new
Attribute
(
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
//$NON-NLS-1$
element
.
addAttribute
(
new
Attribute
(
"keyColumn"
,
introspectedColumn
.
getActualColumnName
()));
//$NON-NLS-1$
}
}
// choose 节点
// choose 节点
XmlElement
choose
=
new
XmlElement
(
"choose"
);
XmlElement
choose
=
new
XmlElement
(
"choose"
);
...
@@ -255,18 +245,8 @@ public class BatchInsertOldPlugin extends PluginAdapter {
...
@@ -255,18 +245,8 @@ public class BatchInsertOldPlugin extends PluginAdapter {
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools
.
addComment
(
element
);
CommentTools
.
addComment
(
element
);
GeneratedKey
gk
=
introspectedTable
.
getGeneratedKey
();
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
if
(
gk
!=
null
)
{
CommTools
.
useGeneratedKeys
(
element
,
introspectedTable
);
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getColumn
(
gk
.
getColumn
());
// if the column is null, then it's a configuration error. The
// warning has already been reported
if
(
introspectedColumn
!=
null
)
{
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element
.
addAttribute
(
new
Attribute
(
"useGeneratedKeys"
,
"true"
));
//$NON-NLS-1$ //$NON-NLS-2$
element
.
addAttribute
(
new
Attribute
(
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
//$NON-NLS-1$
element
.
addAttribute
(
new
Attribute
(
"keyColumn"
,
introspectedColumn
.
getActualColumnName
()));
//$NON-NLS-1$
}
}
// 普通插入语句
// 普通插入语句
this
.
addNormalBatchInsertXml
(
element
,
introspectedTable
);
this
.
addNormalBatchInsertXml
(
element
,
introspectedTable
);
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java
View file @
9d0068df
...
@@ -16,6 +16,7 @@
...
@@ -16,6 +16,7 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.CommTools
;
import
com.itfsw.mybatis.generator.plugins.utils.CommentTools
;
import
com.itfsw.mybatis.generator.plugins.utils.CommentTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.IntrospectedTable
;
...
@@ -28,7 +29,6 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
...
@@ -28,7 +29,6 @@ import org.mybatis.generator.api.dom.xml.XmlElement;
import
org.mybatis.generator.codegen.mybatis3.ListUtilities
;
import
org.mybatis.generator.codegen.mybatis3.ListUtilities
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.codegen.mybatis3.MyBatis3FormattingUtilities
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.Context
;
import
org.mybatis.generator.config.GeneratedKey
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.mybatis.generator.config.PluginConfiguration
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
org.mybatis.generator.internal.util.StringUtility
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
...
@@ -160,6 +160,9 @@ public class BatchInsertPlugin extends PluginAdapter {
...
@@ -160,6 +160,9 @@ public class BatchInsertPlugin extends PluginAdapter {
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools
.
addComment
(
batchInsertEle
);
CommentTools
.
addComment
(
batchInsertEle
);
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
CommTools
.
useGeneratedKeys
(
batchInsertEle
,
introspectedTable
);
StringBuilder
insertClause
=
new
StringBuilder
();
StringBuilder
insertClause
=
new
StringBuilder
();
StringBuilder
valuesClause
=
new
StringBuilder
();
StringBuilder
valuesClause
=
new
StringBuilder
();
...
@@ -217,18 +220,8 @@ public class BatchInsertPlugin extends PluginAdapter {
...
@@ -217,18 +220,8 @@ public class BatchInsertPlugin extends PluginAdapter {
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
// 添加注释(!!!必须添加注释,overwrite覆盖生成时,@see XmlFileMergerJaxp.isGeneratedNode会去判断注释中是否存在OLD_ELEMENT_TAGS中的一点,例子:@mbg.generated)
CommentTools
.
addComment
(
element
);
CommentTools
.
addComment
(
element
);
GeneratedKey
gk
=
introspectedTable
.
getGeneratedKey
();
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
if
(
gk
!=
null
)
{
CommTools
.
useGeneratedKeys
(
element
,
introspectedTable
);
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getColumn
(
gk
.
getColumn
());
// if the column is null, then it's a configuration error. The
// warning has already been reported
if
(
introspectedColumn
!=
null
)
{
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element
.
addAttribute
(
new
Attribute
(
"useGeneratedKeys"
,
"true"
));
//$NON-NLS-1$ //$NON-NLS-2$
element
.
addAttribute
(
new
Attribute
(
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
//$NON-NLS-1$
element
.
addAttribute
(
new
Attribute
(
"keyColumn"
,
introspectedColumn
.
getActualColumnName
()));
//$NON-NLS-1$
}
}
element
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()+
" ("
));
element
.
addElement
(
new
TextElement
(
"insert into "
+
introspectedTable
.
getFullyQualifiedTableNameAtRuntime
()+
" ("
));
...
...
src/main/java/com/itfsw/mybatis/generator/plugins/utils/CommTools.java
0 → 100644
View file @
9d0068df
/*
* Copyright (c) 2017.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
.
utils
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.XmlElement
;
import
org.mybatis.generator.config.GeneratedKey
;
/**
* ---------------------------------------------------------------------------
* 一些通用工具集
* ---------------------------------------------------------------------------
* @author: hewei
* @time:2017/3/22 14:03
* ---------------------------------------------------------------------------
*/
public
class
CommTools
{
/**
* 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
*
* @param element
* @param introspectedTable
*/
public
static
void
useGeneratedKeys
(
XmlElement
element
,
IntrospectedTable
introspectedTable
){
GeneratedKey
gk
=
introspectedTable
.
getGeneratedKey
();
if
(
gk
!=
null
)
{
IntrospectedColumn
introspectedColumn
=
introspectedTable
.
getColumn
(
gk
.
getColumn
());
// if the column is null, then it's a configuration error. The
// warning has already been reported
if
(
introspectedColumn
!=
null
)
{
// 使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型属性中。所以只支持MYSQL和SQLServer
element
.
addAttribute
(
new
Attribute
(
"useGeneratedKeys"
,
"true"
));
//$NON-NLS-1$ //$NON-NLS-2$
element
.
addAttribute
(
new
Attribute
(
"keyProperty"
,
introspectedColumn
.
getJavaProperty
()));
//$NON-NLS-1$
element
.
addAttribute
(
new
Attribute
(
"keyColumn"
,
introspectedColumn
.
getActualColumnName
()));
//$NON-NLS-1$
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment