Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c1ef418

Browse files
committed
增加一个InsertUseGeneratedKeysMapper
1 parent 7e7474c commit c1ef418

File tree

3 files changed

+102
-1
lines changed

3 files changed

+102
-1
lines changed

src/main/java/com/github/abel533/mapper/MySqlMapper.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@
2525
package com.github.abel533.mapper;
2626

2727
import com.github.abel533.mapper.special.InsertListMapper;
28+
import com.github.abel533.mapper.special.InsertUseGeneratedKeysMapper;
2829

2930
/**
3031
* 通用Mapper接口,MySql独有的通用方法
3132
*
3233
* @param <T> 不能为空
3334
* @author liuzh
3435
*/
35-
public interface MySqlMapper<T> extends InsertListMapper<T> {
36+
public interface MySqlMapper<T> extends
37+
InsertListMapper<T>,
38+
InsertUseGeneratedKeysMapper<T> {
3639

3740
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2014 [email protected]
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
* THE SOFTWARE.
23+
*/
24+
25+
package com.github.abel533.mapper.special;
26+
27+
import com.github.abel533.provider.SpecialProvider;
28+
import org.apache.ibatis.annotations.InsertProvider;
29+
import org.apache.ibatis.annotations.Options;
30+
31+
/**
32+
* 通用Mapper接口,特殊方法,批量插入,支持批量插入的数据库都可以使用,例如mysql,h2等
33+
*
34+
* @param <T> 不能为空
35+
* @author liuzh
36+
*/
37+
public interface InsertUseGeneratedKeysMapper<T> {
38+
39+
/**
40+
* 插入,支持数据库自增字段必须是id,支持回写
41+
*
42+
* @param record
43+
* @return
44+
*/
45+
@Options(useGeneratedKeys = true, keyProperty = "id")
46+
@InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
47+
int InsertUseGeneratedKeysMapper(T record);
48+
49+
/**
50+
* ======如果主键不是id怎么用?==========
51+
* 假设主键的属性名是uid,那么新建一个Mapper接口如下
52+
* <pre>
53+
public interface InsertUidMapper<T> {
54+
@Options(useGeneratedKeys = true, keyProperty = "id")
55+
@InsertProvider(type = SpecialProvider.class, method = "dynamicSQL")
56+
int InsertUseGeneratedKeysMapper(T record);
57+
}
58+
* 只要修改keyProperty = "uid"就可以
59+
*
60+
* 然后让你自己的Mapper继承InsertUidListMapper<T>即可
61+
*
62+
* </pre>
63+
*/
64+
}

src/main/java/com/github/abel533/provider/SpecialProvider.java

+34
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,38 @@ public String insertList(MappedStatement ms) {
7676
sql.append("</foreach>");
7777
return sql.toString();
7878
}
79+
80+
/**
81+
* 插入,主键id,自增
82+
*
83+
* @param ms
84+
*/
85+
public String InsertUseGeneratedKeysMapper(MappedStatement ms) {
86+
final Class<?> entityClass = getSelectReturnType(ms);
87+
EntityHelper.EntityTable table = EntityHelper.getEntityTable(entityClass);
88+
//开始拼sql
89+
StringBuilder sql = new StringBuilder();
90+
sql.append("insert into ");
91+
sql.append(table.getName());
92+
sql.append("(");
93+
boolean first = true;
94+
for (EntityHelper.EntityColumn column : table.getEntityClassColumns()) {
95+
if(!first) {
96+
sql.append(",");
97+
}
98+
sql.append(column.getColumn());
99+
first = false;
100+
}
101+
sql.append(") values(");
102+
first = true;
103+
for (EntityHelper.EntityColumn column : table.getEntityClassColumns()) {
104+
if(!first) {
105+
sql.append(",");
106+
}
107+
sql.append("#{").append(column.getProperty()).append("}");
108+
first = false;
109+
}
110+
sql.append(")");
111+
return sql.toString();
112+
}
79113
}

0 commit comments

Comments
 (0)