欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis主表與明細(xì)表一對多的同時插入操作方法

 更新時間:2023年02月13日 10:16:40   作者:馬二科  
對主表(采購申請表)和明細(xì)表(申請物資表)同時進(jìn)行插入操作insert,怎么實(shí)現(xiàn)呢,下面給大家分享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)文章

最新評論