java mapper批量更新、插入、查询

1、组合字段批量查询

    <select id="batchSelect" resultMap="BaseResultMap">
        select
        <include refid="BaseColumnList"/>
        from good
        where mark = 1 and (shop_id,sku_id) in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            (#{item.shopId},#{item.skuId})
        </foreach>
    </select>

2、根据id批量更新

<update id="batchUpdate" parameterType="java.util.List">

        update good
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="sku_id =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.skuId!=null">
                        when id=#{item.id} then #{item.skuId}
                    </if>
                    <if test="item.skuId == null">
                        when id=#{item.id} then good.sku_id
                    </if>
                </foreach>
            </trim>
            <trim prefix="shop_id =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.shopId!=null">
                        when id=#{item.id} then #{item.shopId}
                    </if>
                    <if test="item.shopId == null">
                        when id=#{item.id} then good.shop_id
                    </if>
                </foreach>
            </trim>
            <trim prefix="shop_rate =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.shopRate!=null">
                        when id=#{item.id} then #{item.shopRate}
                    </if>
                    <if test="item.shopRate == null">
                        when id=#{item.id} then good.shop_rate
                    </if>
                </foreach>
            </trim>
            <trim prefix="technician_rate =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.technicianRate!=null">
                        when id=#{item.id} then #{item.technicianRate}
                    </if>
                    <if test="item.technicianRate == null">
                        when id=#{item.id} then good.technician_rate
                    </if>
                </foreach>
            </trim>

            <trim prefix="update_user_id =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.updateUserId!=null">
                        when id=#{item.id} then #{item.updateUserId}
                    </if>
                    <if test="item.updateUserId == null">
                        when id=#{item.id} then good.update_user_id
                    </if>
                </foreach>
            </trim>

            <trim prefix="update_user_name =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.updateUserName!=null">
                        when id=#{item.id} then #{item.updateUserName}
                    </if>
                    <if test="item.updateUserName == null">
                        when id=#{item.id} then good.update_user_name
                    </if>
                </foreach>
            </trim>
            <trim prefix="update_time =case" suffix="end,">
                <foreach collection="list" item="item" index="index">
                    <if test="item.updateTime!=null">
                        when id=#{item.id} then #{item.updateTime}
                    </if>
                    <if test="item.updateTime == null">
                        when id=#{item.id} then good.update_time
                    </if>
                </foreach>
            </trim>
        </trim>
        where mark = 1
        and id in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            #{item.id,jdbcType=BIGINT}
        </foreach>
    </update>

3、批量插入数据

    <insert id="insertBatchSomeColumn" parameterType="java.util.List">
        insert into good(
        sku_id,
        shop_id,
        shop_rate,
        technician_rate,
        type,
        update_user_id,
        update_user_name,
        create_time,
        update_time
        )
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.skuId},
            #{item.shopId},
            #{item.shopRate},
            #{item.technicianRate},
            #{item.type},
            #{item.updateUserId},
            #{item.updateUserName},
            now(),
            now()
            )
        </foreach>

    </insert>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,如果你使用 MyBatis 框架,可以通过 Mapper 接口和 XML 配置文件实现批量插入数据。 首先,你需要在 Mapper 接口中定义一个方法,用于批量插入数据。例如: ```java import java.util.List; public interface YourMapper { void batchInsert(List<YourObject> objects); } ``` 接下来,在对应的 XML 配置文件中,实现该方法的 SQL 语句。例如: ```xml <!-- your-mapper.xml --> <mapper namespace="com.example.YourMapper"> <insert id="batchInsert" parameterType="java.util.List"> INSERT INTO your_table (column1, column2, ...) VALUES <foreach collection="list" item="item" separator=","> (#{item.property1}, #{item.property2}, ...) </foreach> </insert> </mapper> ``` 在上述示例中,`batchInsert` 方法接受一个 `List<YourObject>` 参数,其中 `YourObject` 是你要插入的对象类型。XML 配置文件中的 SQL 语句使用了 MyBatis 的 foreach 标签,遍历传入的对象列表,并将属性值插入数据库表中。 最后,你可以在 Service 层调用 Mapper批量插入方法。例如: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class YourService { private final YourMapper mapper; @Autowired public YourService(YourMapper mapper) { this.mapper = mapper; } public void saveObjects(List<YourObject> objects) { mapper.batchInsert(objects); } } ``` 在上述示例中,`YourService` 类注入了 `YourMapper` 对象,然后在 `saveObjects` 方法中调用了 Mapper批量插入方法。 请注意,上述示例中的表名、列名以及对象的属性名需要根据你的实际情况进行修改。同时,需要配置正确的 MyBatis 配置文件和相关依赖,以使其能够正确运行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值