mybatis批量查询、批量插入

批量查询 

Mapper层

List<Plant> selectListByPlantId(@Param("city")String city, @Param("plantName") String plantName,@Param("userName")String userName,@Param("list")List<Integer> plantiIds);

xml

<select id="selectListByPlantId" resultType="com.aa.Plant">
        select * from plant
                 where 1=1
        <if test="city != null and city != ''">
            and city like concat('%',#{city},'%')
        </if>
        <if test="plantName != null and plantName != ''">
            AND plantName like concat('%',#{plantName},'%')
        </if>
        <if test="userName != null and userName != ''">
            AND accountName like concat('%',#{userName},'%')
        </if>
        <if test="list != null and list.size() > 0">
            and  id in
            <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>

    </select>

这里要对list判断是不是为null,之前没有加判断一直报错,再就是where 1=1 这里是因为不能直接连and语句,所以加了一个一定正确的条件。

批量插入

int insertList((@Param("list")List<UsersModel> list);
  <!--批量插入信息-->
  <insert id="insertList" parameterType="java.util.List">
    insert into users(
    id, name
    )
    values
    <foreach collection="list" item="item" index="index" separator=",">
      (
      #{item.id,jdbcType=INTEGER}, #{item.name,jdbcType=VARCHAR}
      )
    </foreach>
  </insert>

批量插入跟批量查询类似,这里的UserModel是个对象,所以插入的时候取的时候item.id

批量删除和批量修改和上面的类似可参考文章:

SSM Mybatis 中传入List实现 批量插入、批量更新、批量删除_java ssm批量插入-CSDN博客

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值