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
2cfa1ddd
Commit
2cfa1ddd
authored
Jan 13, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修正批量插入插件
parent
d12454d3
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
21 deletions
+15
-21
src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java
...om/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java
+15
-21
No files found.
src/main/java/com/itfsw/mybatis/generator/plugins/BatchInsertPlugin.java
View file @
2cfa1ddd
...
...
@@ -17,11 +17,9 @@
package
com
.
itfsw
.
mybatis
.
generator
.
plugins
;
import
com.itfsw.mybatis.generator.plugins.utils.CommentTools
;
import
com.itfsw.mybatis.generator.plugins.utils.XmlElementGeneratorTools
;
import
org.mybatis.generator.api.IntrospectedColumn
;
import
org.mybatis.generator.api.IntrospectedTable
;
import
org.mybatis.generator.api.PluginAdapter
;
import
org.mybatis.generator.api.dom.OutputUtilities
;
import
org.mybatis.generator.api.dom.java.*
;
import
org.mybatis.generator.api.dom.xml.Attribute
;
import
org.mybatis.generator.api.dom.xml.Document
;
...
...
@@ -60,6 +58,13 @@ public class BatchInsertPlugin extends PluginAdapter {
logger
.
warn
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"要求运行targetRuntime必须为MyBatis3!"
);
return
false
;
}
// 插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键
if
(
"com.mysql.jdbc.Driver"
.
equalsIgnoreCase
(
this
.
getContext
().
getJdbcConnectionConfiguration
().
getDriverClass
())
==
false
&&
"com.microsoft.jdbc.sqlserver.SQLServer"
.
equalsIgnoreCase
(
this
.
getContext
().
getJdbcConnectionConfiguration
().
getDriverClass
())
==
false
&&
"com.microsoft.sqlserver.jdbc.SQLServerDriver"
.
equalsIgnoreCase
(
this
.
getContext
().
getJdbcConnectionConfiguration
().
getDriverClass
())
==
false
){
logger
.
warn
(
"itfsw:插件"
+
this
.
getClass
().
getTypeName
()
+
"插件使用前提是数据库为MySQL或者SQLserver,因为返回主键使用了JDBC的getGenereatedKeys方法获取主键!"
);
return
false
;
}
return
true
;
}
...
...
@@ -82,7 +87,7 @@ public class BatchInsertPlugin extends PluginAdapter {
method
.
setReturnType
(
FullyQualifiedJavaType
.
getIntInstance
());
// 添加参数
FullyQualifiedJavaType
type
=
FullyQualifiedJavaType
.
getNewListInstance
();
type
.
addTypeArgument
(
int
erfaze
.
getType
());
type
.
addTypeArgument
(
int
rospectedTable
.
getRules
().
calculateAllFieldsClass
());
method
.
addParameter
(
new
Parameter
(
type
,
"list"
,
"@Param(\"list\")"
));
// 添加方法说明
CommentTools
.
addGeneralMethodComment
(
method
,
introspectedTable
);
...
...
@@ -129,13 +134,10 @@ public class BatchInsertPlugin extends PluginAdapter {
// if the column is null, then it's a configuration error. The
// warning has already been reported
if
(
introspectedColumn
!=
null
)
{
if
(
gk
.
isJdbcStandard
())
{
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$
}
else
{
element
.
addElement
(
XmlElementGeneratorTools
.
getSelectKey
(
introspectedColumn
,
gk
));
}
// 使用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$
}
}
...
...
@@ -154,21 +156,13 @@ public class BatchInsertPlugin extends PluginAdapter {
IntrospectedColumn
introspectedColumn
=
columns
.
get
(
i
);
insertClause
.
append
(
MyBatis3FormattingUtilities
.
getEscapedColumnName
(
introspectedColumn
));
valuesClause
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
));
// 生成foreach下插入values
valuesClause
.
append
(
MyBatis3FormattingUtilities
.
getParameterClause
(
introspectedColumn
,
"item."
));
if
(
i
+
1
<
columns
.
size
())
{
insertClause
.
append
(
", "
);
//$NON-NLS-1$
valuesClause
.
append
(
", "
);
//$NON-NLS-1$
}
if
(
valuesClause
.
length
()
>
80
)
{
element
.
addElement
(
new
TextElement
(
insertClause
.
toString
()));
insertClause
.
setLength
(
0
);
OutputUtilities
.
xmlIndent
(
insertClause
,
1
);
valuesClauses
.
add
(
valuesClause
.
toString
());
valuesClause
.
setLength
(
0
);
OutputUtilities
.
xmlIndent
(
valuesClause
,
1
);
}
}
insertClause
.
append
(
')'
);
...
...
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