mybatis新增save結束后自動返回主鍵id詳解
mybatis新增save結束后自動返回主鍵id
1.使用場景
save操作之前實體類中id為null,save之后自動返回帶id的實體類
@Override
public ChartPagePanel save(ChartPagePanel entity) {
UserDetails user = SecurityContextHolder.getUserDetails();
entity.setCreateUser(user.getUsername());
entity.setLastModifyUser(user.getUsername());
//entity中的id為null
chartPagePanelMapper.save(entity);
//經(jīng)過save操作后自動返回帶id的entity
// savePanelManage(entity); 其中的entity帶有id
savePanelManage(entity);
return entity;
}
@Transactional
public void savePanelManage(ChartPagePanel entity){
if(entity.getChartPageManges()!=null && entity.getChartPageManges().size()>0) {
Map<String, Object> map = new HashMap<>();
for (int i = 0; i < entity.getChartPageManges().size(); i++) {
int manageId = entity.getChartPageManges().get(i).getId();
map.put("manageId", manageId);
map.put("panelId", entity.getId());
chartPagePanelManageMapper.save(map);
}
}
}
2.原理在Mybatis配置了
useGeneratedKeys=“true” keyProperty=“id”
<insert id="save" parameterType="com.centrin.generator.entity.ChartPagePanel" useGeneratedKeys="true" keyProperty="id">
insert into chart_page_panel
(
`parent_id`,
`position`,
`name`,
`create_time`,
`create_user`,
`last_modify_time`,
`last_modify_user`
)
values (
#{parentId},
#{position},
#{name},
NOW(),
#{createUser},
NOW(),
#{lastModifyUser}
)
</insert>
mybatis或者mybatis-plus中save方法返回主鍵值
1.mapper.xml中
方式:
useGeneratedKeys=“true” keyProperty=“id” keyColumn=“id”
解釋:
在xml中定義useGeneratedKeys為true,返回主鍵id的值,keyColumn和keyProperty分別代表數(shù)據(jù)庫記錄主鍵字段和java對象成員屬性名
<insert id="saveBill" parameterType="com.jd.fp.mrp.domain.model.biz.AdjustBillInfo"
useGeneratedKeys="true" keyProperty="id" keyColumn="id">
INSERT INTO adjust_bill_info(external_bill_id, warehouse_code, warehouse_name)
VALUES(#{externalBillId}, #{warehouseCode}, #{warehouseName});
</insert>
2.service或者dao中
注意:通過該種方式得到的結果是受影響的行數(shù)?。。。?!
如果要獲取主鍵id值,需要從傳入的對象中獲?。。。。?!
Long id = aTranscationMapper.saveBill(adjustBillInfo);
System.out.println("===========保存受影響的行數(shù):"+id+" 保存的id值為:"+adjustBillInfo.getId());
輸出結果展示:
===========保存受影響的行數(shù):1 保存的id值為:191
mybatis-plus的insert后,返回主鍵id,直接通過傳入的對象獲取id即可!
bizApplicationFormMapper.insert(form);
System.out.println("=============="+form.getId());
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
SpringCloud?微服務數(shù)據(jù)權限控制的實現(xiàn)
這篇文章主要介紹的是權限控制的數(shù)據(jù)權限層面,意思是控制可訪問數(shù)據(jù)資源的數(shù)量,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2021-11-11
Java利用DelayQueue實現(xiàn)延遲任務代碼實例
這篇文章主要介紹了Java利用DelayQueue實現(xiàn)延遲任務代碼實例,DelayQueue?是一個支持延時獲取元素的阻塞隊列,?內部采用優(yōu)先隊列?PriorityQueue?存儲元素,同時元素必須實現(xiàn)?Delayed?接口,需要的朋友可以參考下2023-12-12
SpringBoot?Security使用MySQL實現(xiàn)驗證與權限管理
安全管理是軟件系統(tǒng)必不可少的的功能。根據(jù)經(jīng)典的“墨菲定律”——凡是可能,總會發(fā)生。如果系統(tǒng)存在安全隱患,最終必然會出現(xiàn)問題,這篇文章主要介紹了SpringBoot安全管理Spring?Security基本配置2022-11-11
使用maven-assembly-plugin如何將system 依賴范圍的jar以class 方式
這篇文章主要介紹了使用maven-assembly-plugin如何將system 依賴范圍的jar以class 方式打包進 jar包中,本文給大家分享完美解決思路,結合實例代碼給大家介紹的非常詳細,需要的朋友可以參考下2023-06-06
java中如何實現(xiàn) zip rar 7z 壓縮包解壓
這篇文章主要介紹了java中如何實現(xiàn) zip rar 7z 壓縮包解壓問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07

