mybatis主表與明細(xì)表一對多的同時插入操作方法
對主表(采購申請表)和明細(xì)表(申請物資表)同時進(jìn)行插入操作insert:

<!--對申請主表插入一條記錄 -->
<insert id="save" parameterType="com.bootdo.purchase.domain.ApplyDo" useGeneratedKeys="true" keyProperty="applyId">
INSERT INTO pur_apply
(apply_no,apply_depart_id,apply_person_id,apply_date,apply_estiamount,apply_status)
VALUES
(#{applyNo},
(SELECT dept_id FROM mat_department WHERE dept_name = #{deptName} ),
(SELECT sta_id FROM mat_staff WHERE sta_name = #{staName} ),
#{applyDate},#{applyEstiAmount},#{applyStatus})
<selectKey keyProperty="applyId" resultType="Integer" order="AFTER">
SELECT LAST_INSERT_ID()
</selectKey>
</insert>以上注:useGeneratedKeys="true" keyProperty="applyId" 或 <selectKey keyProperty="applyId" resultType="Integer" order="AFTER"> SELECT LAST_INSERT_ID() </selectKey>,可獲取數(shù)據(jù)表中的自增的apply_id存放在持久類ApplyDo中的屬性名applyId,applyId也是明細(xì)表對應(yīng)主表的外鍵,對應(yīng)持久類ApplyItemDo中的屬性名itemApplyId.
<!--對明細(xì)表插入單條記錄 -->
<insert id="saveDetail" parameterType="com.bootdo.purchase.domain.ApplyItemDo" >
INSERT INTO pur_apply_detail
(item_apply_id,item_name,item_type,item_number,item_unit,item_estiprice,item_purpose,item_demdate,item_remake)
VALUES
(#{itemApplyId},#{itemName},#{itemType},#{itemNumber},#{itemUnit},#{itemEstiprice},#{itemPurpose},#{itemDemdate},#{itemRemake})
</insert>ApplyServiceImpl.java:
@Autowired
public ApplyDao applyDao;
@Override
public int save(ApplyDo applyDo) {
//主表插入一條記錄
int count = applyDao.save(applyDo);
int count2 = 0;
int applyId = applyDo.getApplyId();
//明細(xì)表插入多條記錄
for(ApplyItemDo items : applyDo.getItemDoList() ){
items.setItemApplyId(applyId);
count2 = applyDao.saveDetail(items);
}
return count2;
}到此這篇關(guān)于mybatis主表與明細(xì)表一對多的同時插入操作方法的文章就介紹到這了,更多相關(guān)mybatis主表與明細(xì)表一對多同時插入內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Map的computeIfAbsent的使用場景和使用方式
這篇文章主要介紹了基于Map的computeIfAbsent的使用場景和使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
MyBatis?handleResultSet結(jié)果集解析過程示例
這篇文章主要為大家介紹了MyBatis?handleResultSet結(jié)果集解析過程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02
java web將數(shù)據(jù)導(dǎo)出為Excel格式文件代碼片段
這篇文章主要為大家詳細(xì)介紹了java web將數(shù)據(jù)導(dǎo)出為Excel格式文件代碼片段,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01
Spring Cloud Gateway調(diào)用Feign異步問題記錄
這篇文章主要介紹了Spring Cloud Gateway調(diào)用Feign異步問題記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
SpringBoot項目中出現(xiàn)不同端口跨域問題的解決方法
這篇文章主要介紹了SpringBoot項目中出現(xiàn)不同端口跨域問題的解決方法,文中介紹了兩種解決方法,并給出了詳細(xì)的代碼供大家參考,具有一定的參考價值,需要的朋友可以參考下2024-03-03
SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法
本文主要介紹了SpringBoot使用RedisTemplate.delete刪除指定key失敗的解決辦法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03

