Skip to content

Commit 2eab92f

Browse files
committed
解决SpecialProvider不支持insertable的bug#77
1 parent c47c87e commit 2eab92f

File tree

1 file changed

+10
-12
lines changed

1 file changed

+10
-12
lines changed

src/main/java/tk/mybatis/mapper/provider/SpecialProvider.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import tk.mybatis.mapper.mapperhelper.MapperTemplate;
3333
import tk.mybatis.mapper.mapperhelper.SqlHelper;
3434

35+
import java.util.Set;
36+
3537
/**
3638
* SpecialProvider实现类,特殊方法实现类
3739
*
@@ -50,26 +52,22 @@ public SpecialProvider(Class<?> mapperClass, MapperHelper mapperHelper) {
5052
*/
5153
public String insertList(MappedStatement ms) {
5254
final Class<?> entityClass = getEntityClass(ms);
53-
EntityTable table = EntityHelper.getEntityTable(entityClass);
5455
//开始拼sql
5556
StringBuilder sql = new StringBuilder();
5657
sql.append(SqlHelper.insertIntoTable(entityClass, tableName(entityClass)));
5758
sql.append(SqlHelper.insertColumns(entityClass, true, false, false));
5859
sql.append(" VALUES ");
5960
sql.append("<foreach collection=\"list\" item=\"record\" separator=\",\" >");
60-
sql.append("(");
61-
boolean first = true;
62-
for (EntityColumn column : table.getEntityClassColumns()) {
63-
if (column.isId()) {
64-
continue;
65-
}
66-
if (!first) {
67-
sql.append(",");
61+
sql.append("<trim prefix=\"VALUES (\" suffix=\")\" suffixOverrides=\",\">");
62+
//获取全部列
63+
Set<EntityColumn> columnList = EntityHelper.getColumns(entityClass);
64+
//当某个列有主键策略时,不需要考虑他的属性是否为空,因为如果为空,一定会根据主键策略给他生成一个值
65+
for (EntityColumn column : columnList) {
66+
if (!column.isId() && column.isInsertable()) {
67+
sql.append(column.getColumnHolder("record") + ",");
6868
}
69-
sql.append(column.getColumnHolder("record"));
70-
first = false;
7169
}
72-
sql.append(")");
70+
sql.append("</trim>");
7371
sql.append("</foreach>");
7472
return sql.toString();
7573
}

0 commit comments

Comments
 (0)