Mybatis批量查询、插入

作者: 阿历Ali | 来源:发表于2017-12-13 17:41 被阅读38次

核心是用Mybatis的foreach

<foreach collection="someList" item="someItem" ...>
</foreach>

1

批量查询:入参为List,包装在Map中,参考:http://blog.csdn.net/lxxxzzl/article/details/46376023

List<String> queryNamesByIds(Map<String, Object> userIdsParam); 
<select id="queryNamesByIds" resultType="java.lang.String">
    SELECT name from user 
    WHERE id in 
    <foreach collection="userIds" item="id" open="(" close=")" separator=",">
      #{id}
    </foreach>
</select>

2

批量插入:

void batchInsert(List<AcctLoanInfo> acctInfoList);
<insert id="batchInsert" parameterType="java.util.List">
    INSERT INTO USER_OPERATE_RECORD
    (
      <include refid="Columns_For_Insert"/>
    )
    VALUES
    <foreach collection="list" item="item" separator=",">
      (
      #{item.userAccountId,jdbcType=VARCHAR}, #{item.requestParameter,jdbcType=VARCHAR},
      #{item.operateType,jdbcType=INTEGER}, #{item.gmtCreate,jdbcType=TIMESTAMP}
      )
    </foreach>
</insert>

相关文章

  • 注解版Mybatis 批量查询,插入(更新暂不支持)

    注解版 Mybatis 3.1.1 不支持