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
571ba826
Commit
571ba826
authored
Mar 22, 2017
by
hewei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed:[issues#2]
增加存在即更新插件
parent
9d0068df
Changes
2
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
370 additions
and
1 deletion
+370
-1
README.md
README.md
+36
-1
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
...ava/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
+334
-0
No files found.
README.md
View file @
571ba826
...
@@ -12,6 +12,7 @@
...
@@ -12,6 +12,7 @@
*
批量插入插件(BatchInsertPlugin)
*
批量插入插件(BatchInsertPlugin)
*
逻辑删除插件(LogicalDeletePlugin)
*
逻辑删除插件(LogicalDeletePlugin)
*
数据Model属性对应Column获取插件(ModelColumnPlugin)
*
数据Model属性对应Column获取插件(ModelColumnPlugin)
*
存在即更新插件(UpsertPlugin)
---------------------------------------
---------------------------------------
Maven引用:
Maven引用:
...
@@ -19,7 +20,7 @@ Maven引用:
...
@@ -19,7 +20,7 @@ Maven引用:
<dependency>
<dependency>
<groupId>
com.itfsw
</groupId>
<groupId>
com.itfsw
</groupId>
<artifactId>
mybatis-generator-plugin
</artifactId>
<artifactId>
mybatis-generator-plugin
</artifactId>
<version>
1.0.
5
</version>
<version>
1.0.
6
</version>
</dependency>
</dependency>
```
```
---------------------------------------
---------------------------------------
...
@@ -401,3 +402,37 @@ public class Test {
...
@@ -401,3 +402,37 @@ public class Test {
}
}
}
}
```
```
### 9. 存在即更新插件
使用MySQL的
[
“insert ... on duplicate key update”
](
https://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
)
实现存在即更新操作,简化数据入库操作(
[
[issues#2
]
](https://github.com/itfsw/mybatis-generator-plugin/issues/2))。
插件:
```
xml
<!-- 存在即更新插件 -->
<plugin
type=
"com.itfsw.mybatis.generator.plugins.UpsertPlugin"
/>
```
使用:
```
java
public
class
Test
{
public
static
void
main
(
String
[]
args
)
{
// 1. 未入库数据入库,执行insert
Tb
tb
=
new
Tb
.
Builder
()
.
field1
(
1
)
.
field2
(
"xx0"
)
.
delFlag
((
short
)
0
)
.
build
();
int
k0
=
this
.
tbMapper
.
upsert
(
tb
);
// 2. 已入库数据再次入库,执行update(!!需要注意如触发update其返回的受影响行数为2)
tb
.
setField2
(
"xx1"
);
int
k1
=
this
.
tbMapper
.
upsert
(
tb
);
// 3. 类似insertSelective实现选择入库
Tb
tb1
=
new
Tb
.
Builder
()
.
field1
(
1
)
.
field2
(
"xx0"
)
.
build
();
int
k2
=
this
.
tbMapper
.
upsertSelective
(
tb1
);
tb1
.
setField2
(
"xx1"
);
int
k3
=
this
.
tbMapper
.
upsertSelective
(
tb1
);
}
}
```
\ No newline at end of file
src/main/java/com/itfsw/mybatis/generator/plugins/UpsertPlugin.java
0 → 100644
View file @
571ba826
This diff is collapsed.
Click to expand it.
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