-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pr#711 修订批量插入,支持设置逻辑删除默认值;扩展支持批量更新;支持PostgreSQL UPSERT语句
- Loading branch information
Showing
8 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
extra/src/main/java/tk/mybatis/mapper/additional/update/batch/BatchUpdateMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tk.mybatis.mapper.additional.update.batch; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.UpdateProvider; | ||
import tk.mybatis.mapper.annotation.RegisterMapper; | ||
|
||
import java.util.List; | ||
|
||
@RegisterMapper | ||
public interface BatchUpdateMapper<T> { | ||
|
||
@UpdateProvider( | ||
type = BatchUpdateProvider.class, | ||
method = "dynamicSQL" | ||
) | ||
void batchUpdate(@Param("list") List<? extends T> recordList); | ||
} |
35 changes: 35 additions & 0 deletions
35
extra/src/main/java/tk/mybatis/mapper/additional/update/batch/BatchUpdateProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package tk.mybatis.mapper.additional.update.batch; | ||
|
||
import org.apache.ibatis.mapping.MappedStatement; | ||
import tk.mybatis.mapper.mapperhelper.MapperHelper; | ||
import tk.mybatis.mapper.mapperhelper.MapperTemplate; | ||
import tk.mybatis.mapper.mapperhelper.SqlHelper; | ||
|
||
public class BatchUpdateProvider extends MapperTemplate { | ||
|
||
public BatchUpdateProvider(Class<?> mapperClass, MapperHelper mapperHelper) { | ||
super(mapperClass, mapperHelper); | ||
} | ||
|
||
public String batchUpdate(MappedStatement ms) { | ||
final Class<?> entityClass = getEntityClass(ms); | ||
StringBuilder sql = new StringBuilder(); | ||
sql.append("<foreach collection=\"list\" item=\"record\" separator=\";\" >"); | ||
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass))); | ||
sql.append(SqlHelper.updateSetColumns(entityClass, "record", false, false)); | ||
sql.append(SqlHelper.wherePKColumns(entityClass, "record", true)); | ||
sql.append("</foreach>"); | ||
return sql.toString(); | ||
} | ||
|
||
public String batchUpdateSelective(MappedStatement ms) { | ||
final Class<?> entityClass = getEntityClass(ms); | ||
StringBuilder sql = new StringBuilder(); | ||
sql.append("<foreach collection=\"list\" item=\"record\" separator=\";\" >"); | ||
sql.append(SqlHelper.updateTable(entityClass, tableName(entityClass))); | ||
sql.append(SqlHelper.updateSetColumns(entityClass, "record", true, isNotEmpty())); | ||
sql.append(SqlHelper.wherePKColumns(entityClass, "record", true)); | ||
sql.append("</foreach>"); | ||
return sql.toString(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...a/src/main/java/tk/mybatis/mapper/additional/update/batch/BatchUpdateSelectiveMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tk.mybatis.mapper.additional.update.batch; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.UpdateProvider; | ||
import tk.mybatis.mapper.annotation.RegisterMapper; | ||
|
||
import java.util.List; | ||
|
||
@RegisterMapper | ||
public interface BatchUpdateSelectiveMapper<T> { | ||
|
||
@UpdateProvider( | ||
type = BatchUpdateProvider.class, | ||
method = "dynamicSQL" | ||
) | ||
void batchUpdateSelective(@Param("list") List<? extends T> recordList); | ||
} |
17 changes: 17 additions & 0 deletions
17
extra/src/main/java/tk/mybatis/mapper/additional/upsert/BatchUpsertMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package tk.mybatis.mapper.additional.upsert; | ||
|
||
import org.apache.ibatis.annotations.Param; | ||
import org.apache.ibatis.annotations.UpdateProvider; | ||
import tk.mybatis.mapper.annotation.RegisterMapper; | ||
|
||
import java.util.List; | ||
|
||
@RegisterMapper | ||
public interface BatchUpsertMapper<T> { | ||
|
||
@UpdateProvider( | ||
type = BatchUpsertProvider.class, | ||
method = "dynamicSQL" | ||
) | ||
void batchUpsert(@Param("list") List<? extends T> recordList); | ||
} |
65 changes: 65 additions & 0 deletions
65
extra/src/main/java/tk/mybatis/mapper/additional/upsert/BatchUpsertProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package tk.mybatis.mapper.additional.upsert; | ||
|
||
import org.apache.ibatis.mapping.MappedStatement; | ||
import tk.mybatis.mapper.entity.EntityColumn; | ||
import tk.mybatis.mapper.mapperhelper.EntityHelper; | ||
import tk.mybatis.mapper.mapperhelper.MapperHelper; | ||
import tk.mybatis.mapper.mapperhelper.MapperTemplate; | ||
import tk.mybatis.mapper.mapperhelper.SqlHelper; | ||
|
||
import java.util.Set; | ||
|
||
public class BatchUpsertProvider extends MapperTemplate { | ||
|
||
public BatchUpsertProvider(Class<?> mapperClass, MapperHelper mapperHelper) { | ||
super(mapperClass, mapperHelper); | ||
} | ||
|
||
public String batchUpsert(MappedStatement ms) { | ||
final Class<?> entityClass = getEntityClass(ms); | ||
StringBuilder sql = new StringBuilder(); | ||
sql.append("<foreach collection=\"list\" item=\"record\" separator=\";\" >"); | ||
sql.append("INSERT INTO "); | ||
sql.append(tableName(entityClass)); | ||
Set<EntityColumn> columns = EntityHelper.getColumns(entityClass); | ||
String primaryKeyColumn = null; | ||
EntityColumn logicDeleteColumn = SqlHelper.getLogicDeleteColumn(entityClass); | ||
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"); | ||
for (EntityColumn column : columns) { | ||
if (column.isId()) { | ||
primaryKeyColumn = column.getColumn(); | ||
} | ||
if (column.isInsertable()) { | ||
sql.append(column.getColumn() + ","); | ||
} | ||
} | ||
sql.append("</trim>"); | ||
sql.append(" VALUES "); | ||
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"); | ||
for (EntityColumn column : columns) { | ||
if (column.getGenIdClass() != null) { | ||
sql.append("<bind name=\"").append(column.getColumn()).append("GenIdBind\" value=\"@tk.mybatis.mapper.genid.GenIdUtil@genId("); | ||
sql.append("record").append(", '").append(column.getProperty()).append("'"); | ||
sql.append(", @").append(column.getGenIdClass().getCanonicalName()).append("@class"); | ||
sql.append(", '").append(tableName(entityClass)).append("'"); | ||
sql.append(", '").append(column.getColumn()).append("')"); | ||
sql.append("\"/>"); | ||
} | ||
} | ||
for (EntityColumn column : columns) { | ||
if (!column.isInsertable()) { | ||
continue; | ||
} | ||
if (logicDeleteColumn != null && logicDeleteColumn == column) { | ||
sql.append(SqlHelper.getLogicDeletedValue(column, false)).append(","); | ||
continue; | ||
} | ||
sql.append(column.getColumnHolder("record") + ","); | ||
} | ||
sql.append("</trim>"); | ||
sql.append(" ON CONFLICT (" + primaryKeyColumn + ") DO UPDATE "); | ||
sql.append(SqlHelper.updateSetColumns(entityClass, "record", true, isNotEmpty())); | ||
sql.append("</foreach>"); | ||
return sql.toString(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
extra/src/main/java/tk/mybatis/mapper/additional/upsert/UpsertMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package tk.mybatis.mapper.additional.upsert; | ||
|
||
import org.apache.ibatis.annotations.UpdateProvider; | ||
|
||
public interface UpsertMapper<T> { | ||
|
||
@UpdateProvider( | ||
type = UpsertProvider.class, | ||
method = "dynamicSQL" | ||
) | ||
void upsert(T record); | ||
} |
63 changes: 63 additions & 0 deletions
63
extra/src/main/java/tk/mybatis/mapper/additional/upsert/UpsertProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package tk.mybatis.mapper.additional.upsert; | ||
|
||
import org.apache.ibatis.mapping.MappedStatement; | ||
import tk.mybatis.mapper.entity.EntityColumn; | ||
import tk.mybatis.mapper.mapperhelper.EntityHelper; | ||
import tk.mybatis.mapper.mapperhelper.MapperHelper; | ||
import tk.mybatis.mapper.mapperhelper.MapperTemplate; | ||
import tk.mybatis.mapper.mapperhelper.SqlHelper; | ||
|
||
import java.util.Set; | ||
|
||
public class UpsertProvider extends MapperTemplate { | ||
|
||
public UpsertProvider(Class<?> mapperClass, MapperHelper mapperHelper) { | ||
super(mapperClass, mapperHelper); | ||
} | ||
|
||
public String upsert(MappedStatement ms) { | ||
final Class<?> entityClass = getEntityClass(ms); | ||
StringBuilder sql = new StringBuilder(); | ||
sql.append("INSERT INTO "); | ||
sql.append(tableName(entityClass)); | ||
Set<EntityColumn> columns = EntityHelper.getColumns(entityClass); | ||
String primaryKeyColumn = null; | ||
EntityColumn logicDeleteColumn = SqlHelper.getLogicDeleteColumn(entityClass); | ||
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"); | ||
for (EntityColumn column : columns) { | ||
if (column.isId()) { | ||
primaryKeyColumn = column.getColumn(); | ||
} | ||
if (column.isInsertable()) { | ||
sql.append(column.getColumn() + ","); | ||
} | ||
} | ||
sql.append("</trim>"); | ||
sql.append(" VALUES "); | ||
sql.append("<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"); | ||
for (EntityColumn column : columns) { | ||
if (column.getGenIdClass() != null) { | ||
sql.append("<bind name=\"").append(column.getColumn()).append("GenIdBind\" value=\"@tk.mybatis.mapper.genid.GenIdUtil@genId("); | ||
sql.append("record").append(", '").append(column.getProperty()).append("'"); | ||
sql.append(", @").append(column.getGenIdClass().getCanonicalName()).append("@class"); | ||
sql.append(", '").append(tableName(entityClass)).append("'"); | ||
sql.append(", '").append(column.getColumn()).append("')"); | ||
sql.append("\"/>"); | ||
} | ||
} | ||
for (EntityColumn column : columns) { | ||
if (!column.isInsertable()) { | ||
continue; | ||
} | ||
if (logicDeleteColumn != null && logicDeleteColumn == column) { | ||
sql.append(SqlHelper.getLogicDeletedValue(column, false)).append(","); | ||
continue; | ||
} | ||
sql.append(column.getColumnHolder() + ","); | ||
} | ||
sql.append("</trim>"); | ||
sql.append(" ON CONFLICT (" + primaryKeyColumn + ") DO UPDATE "); | ||
sql.append(SqlHelper.updateSetColumns(entityClass, null, true, isNotEmpty())); | ||
return sql.toString(); | ||
} | ||
} |