zoukankan      html  css  js  c++  java
  • 2017.9.15 mybatis批量插入后实现主键回填

    参考来自:mybatis mysql 批量insert 返回主键

    注意:必须要在mybatis3.3.1及其以上才能实现。

    1.service

    1 List branchEntryList = (ArrayList<Entry>)entryMap.get("branchEntryList");
    2 branchDao.insertBatch(branchEntryList);

    2.dao

    3.xml

    注意这里是list,不管参数名叫什么,这里都是list。

    4.效果

    执行前,branchEntryList里的对象是没有id的值的。

    执行完后,查看变量branchEntryList,可以看到主键已经回填了。

    5.其他

    在查资料的时候,说这种方式支持map和list。

    于是使用Map:

    (1)insertMap.insert("branchEntryList",branchEntryList);

    (2)int insertBatch(Map<String,Object> insertMap);

    (3)parameterType="java.util.Map"  

    (4)<foreach collection="branchEntryList" ...

    但是主键并没有回填。将Map的类型改为<String,List<Entry>>,主键则成功回填。所以使用此项功能的时候,List<Entry>必须指明,不管是不是放在map里。

  • 相关阅读:
    5.14事务
    5.13Mysql数据库Database
    未来打算
    浅谈P NP NPC
    1222
    1219
    Linux初等命令
    惩罚因子(penalty term)与损失函数(loss function)
    12 14
    java 泛型思考
  • 原文地址:https://www.cnblogs.com/lyh421/p/7527209.html
Copyright © 2011-2022 走看看