mybatis-sqlserver批量新增返回id方式
遇到的問題
公司最近接到項目需要使用SqlServer,在做SQL兼容的時候遇到問題,批量新增數(shù)據(jù)時只返回的第一條記錄的id
解決思路
1.參考mysql
<insert id="batchInsert" userGeneratedKeys="true" keyProperty="id">
insert into public_user (name,password)
values
<foreach collection="list" separator="," item="item">
(#{item.name}, #{itme.password})
</foreach>
</insert>具體mybatis封裝id的地方在org.apache.ibatis.executor.keygen.Jdbc3KeyGenerator
主要邏輯是通過Statement.getGeneratedKeys()獲取id結果集.
SqlServer只能獲取第一個
2.發(fā)現(xiàn)上述步驟的Jdbc3KeyGenerator是有接口的KeyGenerator,查看后發(fā)現(xiàn)有3個實現(xiàn)類了解到SelectKeyGenerator通過selectKeyt標簽可以返回id;
3.查看SelectKeyGenerator源碼了解到只支持返回單個id;

4.通過百度了解到SqlServer通過output inserted.id可以輸出id;
5.是否可以自己實現(xiàn)KeyGenerator來解決批量返回id的方法呢?
6.最后我沒找到配置自定義的實現(xiàn)類的方式,決定通過mybatis的攔截器解決
代碼
mybatis攔截器
@Component
@Intercepts(@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}))
public class SqlServerKeyGeneratorInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
// 正常執(zhí)行代碼 獲取返回結構集
List<Object> values = (List)invocation.proceed();
MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
// 判斷是否selectKey查詢語句
if (ms.getId().endsWith(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
Configuration configuration = ms.getConfiguration();
// 處理入?yún)ο?
Collection<Object> parameters = getParameters(invocation.getArgs()[1]);
// 封裝id
for (int i = 0; i < parameters.size(); i++) {
MetaObject metaObject = configuration.newMetaObject(parameters.toArray()[i]);
metaObject.setValue("id", Long.valueOf((Integer)(((Map)values.get(i)).get("id"))));
}
// 返回假數(shù)據(jù)防止異常
List<Long> ids = new ArrayList<>();
ids.add(1L);
return ids;
}
return values;
}
@Override
public Object plugin(Object target) {
return null;
}
@Override
public void setProperties(Properties properties) {
}
/** 來源 Jdbc3KeyGenerator */
private Collection<Object> getParameters(Object parameter) {
Collection<Object> parameters = null;
if (parameter instanceof Collection) {
parameters = (Collection) parameter;
} else if (parameter instanceof Map) {
Map parameterMap = (Map) parameter;
if (parameterMap.containsKey("collection")) {
parameters = (Collection) parameterMap.get("collection");
} else if (parameterMap.containsKey("list")) {
parameters = (List) parameterMap.get("list");
} else if (parameterMap.containsKey("array")) {
parameters = Arrays.asList((Object[]) parameterMap.get("array"));
}
}
if (parameters == null) {
parameters = new ArrayList<Object>();
parameters.add(parameter);
}
return parameters;
}
}
mapper
<insert id="batchInsert">
<selectKey keyColumn="id" keyProperty="id" order="AFTER" resultType="java.util.Map">
insert into public_user (name,password)
output inserted.id
values
<foreach collection="list" separator="," item="item">
(#{item.name}, #{itme.password})
</foreach>
<selectKey>
</insert>
PS:
需要注意的是id對象參數(shù)必須放在第一位,攔截器的代碼寫的比較粗糙,給各位提供思路。
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java數(shù)據(jù)結構之AC自動機算法的實現(xiàn)
AC自動機算法常被認為是Trie樹+KMP算法的結合體,它是一個多模式匹配算法,在模式匹配領域被廣泛應用。本文將詳細為大家介紹AC自動機的原理與實現(xiàn)方法,感興趣的可以了解一下2022-12-12
Java實現(xiàn)將容器 Map中的內容保存到數(shù)組
這篇文章主要介紹了Java實現(xiàn)將容器 Map中的內容保存到數(shù)組,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用案例詳解
通過 JdbcTemplate 直接執(zhí)行 SQL 語句,結合源碼動態(tài)編譯即可方便實現(xiàn)動態(tài)修改代碼邏輯的效果,這篇文章主要介紹了JDK20?+?SpringBoot?3.1.0?+?JdbcTemplate?使用,需要的朋友可以參考下2023-09-09
SpringCloud Gateway動態(tài)轉發(fā)后端服務實現(xiàn)過程講解
這篇文章主要介紹了SpringCloud Gateway動態(tài)轉發(fā)后端服務實現(xiàn)過程,簡單的路由轉發(fā)可以通過SpringCloudGateway的配置文件實現(xiàn),在一些業(yè)務場景種,會需要動態(tài)替換路由配置中的后端服務地址,單純靠配置文件無法滿足這種需求2023-03-03

