批量插入、更新mapper写法

这篇具有很好参考价值的文章主要介绍了批量插入、更新mapper写法。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

批量更新

参考:https://blog.csdn.net/mianma_YAYIZI/article/details/102466672?spm=1001.2014.3001.5506

<update id="batchUpdate" parameterType="java.util.List">
    update base_supplier_purchasing_info_sap
    set updated_time = now(), update_by = 'sys',
    finance_purchase_frozen = case
    <foreach collection="list" item="item" index="index" separator=" ">
        when (supplier_code = #{item.supplierCode} and purchase_org_code = #{item.purchaseOrgCode}) then
        #{item.financePurchaseFrozen}
    </foreach>
    end where
    <foreach collection="list" index="index" item="item" separator="or">
        (supplier_code = #{item.supplierCode} and purchase_org_code = #{item.purchaseOrgCode})
    </foreach>
</update>

对应sql文章来源地址https://www.toymoban.com/news/detail-511362.html

update bak_supplier_finance_purchase_frozen_info
set updated_time            = now(),
    update_by               = 'sys',
    finance_purchase_frozen = case when (supplier_code = '20036982' and purchase_org_code = 'P001') then '1'  end
where (supplier_code = '20036982' and purchase_org_code = 'P001');

批量插入

<insert id="batchInsert" parameterType="java.util.List">
   insert into bak_supplier_finance_purchase_frozen_info
       (id, supplier_code, purchase_org_code, finance_purchase_frozen)
   values
   <foreach collection="list" item="item" separator=",">
       (#{item.id,jdbcType=BIGINT}, #{item.supplierCode,jdbcType=VARCHAR}, #{item.purchaseOrgCode,jdbcType=VARCHAR}, #{item.financePurchaseFrozen,jdbcType=VARCHAR})
   </foreach>
</insert>

批量更新&插入

<insert id="insertAndUpdate" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
  insert into instead_of_sending_order_relation
  (supplier_code, apply_order_no, purchase_order_no, account_direction, apply_date,
   is_delete, created_time, updated_time, created_by, updated_by) values
  <foreach collection="list" separator="," item="item">
    (#{item.supplierCode},#{item.applyOrderNo},#{item.purchaseOrderNo},#{item.accountDirection},#{item.applyDate},
    #{item.isDelete},#{item.createdTime},#{item.updatedTime},#{item.createdBy},#{item.updatedBy})
  </foreach>
    ON DUPLICATE KEY UPDATE
        supplier_code = values(supplier_code),
        apply_order_no = values(apply_order_no),
        purchase_order_no = values(purchase_order_no),
        account_direction = values(account_direction),
        apply_date = values(apply_date),
        is_delete = values(is_delete),
        created_time = values(created_time),
        updated_time = values(updated_time),
        created_by = values(created_by),
        updated_by = values(updated_by)
</insert>

查询

 @Override
public Page<ResSupplierWhiteListVo> selectByConditions(ReqSupplierWhiteListQueryVO reqSupplierWhitelistQueryVO) {
    Page<ResSupplierWhiteListVo> resultPage = new Page<>();
    com.baomidou.mybatisplus.extension.plugins.pagination.Page<SupplierWhiteList> queryPage
            = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(reqSupplierWhitelistQueryVO.getPage(), reqSupplierWhitelistQueryVO.getSize());
    queryPage.setDesc("updated_time");
    List<SupplierWhiteList> supplierWhiteLists = supplierWhiteListMapper.selectByConditionsByPage(queryPage, reqSupplierWhitelistQueryVO);
//获取特例名称
Map<String, String> specialCaseMap =  categoryAttributeService.getCategoryAttribute(BizConstant.ATTRIBUTE_KEY_SUPPLIER_WHITE_LIST,  BizError.SUPPLIER_WHITE_LIST_GET_SPECIAL_CASE_CODE_FAIL.getMsg());
List<ResSupplierWhiteListVo> resSupplierWhiteListVos = SupplierWhiteListConverter.toVos(supplierWhiteLists, specialCaseMap);
    PaginationUtil.pageConvert(queryPage, resultPage, resSupplierWhiteListVos);
    return resultPage;
}

// Mapper.java定义
List<SupplierWhiteList> selectByConditionsByPage(@Param("queryPage") Page<SupplierWhiteList> queryPage, @Param("reqSupplierWhitelistQueryVO") ReqSupplierWhiteListQueryVO req);

<select id="selectByConditionsByPage"
       resultType="com.yonghui.yh.rme.srm.suppliercenter.dao.entity.SupplierWhiteList">
   select
   <include refid="Base_Column_List"/>
   from supplier_white_list
   <where>is_delete = 0
       <if test="reqSupplierWhitelistQueryVO.supplierCodes != null and reqSupplierWhitelistQueryVO.supplierCodes.size > 0">
           and supplier_code in
           <foreach collection="reqSupplierWhitelistQueryVO.supplierCodes" separator="," open="(" close=")"
                    item="item">
               #{item}
           </foreach>
       </if>
       <if test="reqSupplierWhitelistQueryVO.purchaseOrgCodes != null and reqSupplierWhitelistQueryVO.purchaseOrgCodes.size > 0">
           and purchase_org_code in
           <foreach collection="reqSupplierWhitelistQueryVO.purchaseOrgCodes" separator="," open="(" close=")"
                    item="item">
               #{item}
           </foreach>
       </if>
       <if test="reqSupplierWhitelistQueryVO.brandLevel != null">
           and brand_level = #{reqSupplierWhitelistQueryVO.brandLevel}
       </if>
       <if test="reqSupplierWhitelistQueryVO.supplierAttributeCode != null and reqSupplierWhitelistQueryVO.supplierAttributeCode!=''">
           and supplier_attribute_code = #{reqSupplierWhitelistQueryVO.supplierAttributeCode}
       </if>
       <if test="reqSupplierWhitelistQueryVO.supplierAttributeName != null and reqSupplierWhitelistQueryVO.supplierAttributeName!=''">
           and supplier_attribute_name = #{reqSupplierWhitelistQueryVO.supplierAttributeName}
       </if>
       <if test="reqSupplierWhitelistQueryVO.updateStartTime != null">
           and updated_time &gt;= #{reqSupplierWhitelistQueryVO.updateStartTime}
       </if>
       <if test="reqSupplierWhitelistQueryVO.updatedEndTime != null">
           and updated_time &lt; #{reqSupplierWhitelistQueryVO.updatedEndTime}
       </if>
   </where>
</select>

到了这里,关于批量插入、更新mapper写法的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 使用saveOrUpdateBatch实现批量插入更新数据

    saveOrUpdateBatch 是 Hibernate 中的一个方法,可以用来批量插入或更新数据。这个方法的参数是一个 List,里面可以存储多个实体对象。当 Hibernate 执行这个方法时,会检查每个实体对象是否存在主键,如果存在主键就执行 update 操作,否则执行 insert 操作。 使用 saveOrUpdateBatch 的代

    2024年02月11日
    浏览(39)
  • Mybatis批量插入/更新性能优化思路

            最近在做数据写入服务的性能优化,主要是基于Mybatis-Plus实现一套批量写数据的服务,不过该服务是支持整个平台所有需要持久化的业务实体。所以这种服务不仅仅有insert操作还有update的操作。根据以往的MySQL数据库写入经验,主要总结了两套批量插入、批量插入

    2024年04月25日
    浏览(28)
  • 18. ElasticSearch系列之批量插入与更新

    本文介绍工作中Python版常用的高效ES批量插入、更新数据方式 1. 批量插入 2.批量更新 批量更新只需要改动action的以下内容即可 欢迎关注公众号算法小生或沈健的技术博客

    2024年02月11日
    浏览(32)
  • 批量插入或更新数据(MyBatis-plus框架)

    目录 1.场景说明 2.DUPLICATE 和REPLACE比较 3.批量插入或者更新(两种方式) 方式一:mybatis-plus的saveOrUpdateBatch方法 问题:如果操作类集成了基础类,比如封装了BaseEntity去集成,那么这样使用会出问题 方式二:on duplicate key (推荐) 4.注意 5.常见问题  插入数据时,我们经常会遇到这

    2024年02月04日
    浏览(67)
  • Mysql 实现批量插入对已存在数据忽略或更新

    对已存在的数据进行 忽略/更新 ,需要唯一索引/主键。 唯一索引可为多个字段的联合索引,比如根据我提供的sql中,我需要``name + age`不重复,则可把这2个字段联合创建为唯一索引 创建联合唯一索引的sql 批量插入对已存在数据忽略 批量插入对已存在数据更新 笔者这里只举

    2024年02月15日
    浏览(32)
  • springboot使用aop排除某些方法,更新从另外一张表,从另外一张表批量插入

    在Spring Boot中使用AOP时,如果想要排除某些方法不被切面所影响,可以通过使用切面表达式中的!within来实现。以下是一个示例: 在上面的示例中,@Before注解用于定义切面的beforeAdvice方法。execution(* com.example.service. . (…))表示切入所有com.example.service包下的方法。而!wit

    2024年02月13日
    浏览(32)
  • Spring Boot Elasticsearch7.6.2实现创建索引、删除索引、判断索引是否存在、获取/添加/删除/更新索引别名、单条/批量插入、单条/批量更新、删除数据、递归统计ES聚合的数据

    注意:我的版本是elasticsearch7.6.2、spring-boot-starter-data-elasticsearch-2.5.6 引入依赖 有时候你可能需要查询大批量的数据,建议加上下面配置文件

    2024年02月13日
    浏览(52)
  • Mybatis mapper.xml 判断条件写法注意

    1.判断String是否为空 if test=\\\"stringParam != null and stringParam != \\\'\\\'\\\"/if 2.判断Integer是否大于0 判断等于  when test=\\\"item.mark == 1\\\"\\\" 3.判断List是否不为空 5.判断字符串是否等于特定字符(比如此处的user)

    2024年02月16日
    浏览(39)
  • Mybatis-Plus 自定义mapper批量新增修改函数

    version MybatisPlusConfig CustomizedSqlInjector InsertBatchMethod UpdateBatchMethod RootMapper 使用方式 mapper接口实现自定义的RootMapper,即可调用批量新增修改函数

    2024年02月12日
    浏览(38)
  • 问题解决:使用Mybatis Plus的Mapper插入达梦数据库报“数据溢出”错误

    使用Mybatis Plus的Mapper插入达梦数据库报“数据溢出”错误 问题描述 在进行批量插入中,抛出异常为数据溢出 插入方法:this.baseMapper.insertBatchSomeColumn() 抛出异常:数据溢出 对失败的数据进行循环,尝试使用单个插入的方法,同样抛出异常为数据溢出 插入方法:this.baseMapper

    2024年02月07日
    浏览(39)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包