一条sql语句,实现mybatis批量插入或更新数据

前言

trader_users_fee_result表没有主键,没有唯一索引,唯一的一个要求是:
user_id,ename,Date三个字段 在没条记录里唯一,即不能有2条记录,每一条记录的这三个字段都相同。
将这3个字段做一个联合索引:
alter table trader_users_fee_result add index ‘unionIndex’(‘user_id’,‘ename’,‘Date’);
这样后面在replace into时,mysql才能判断新的带插入的数据是否在表里已存在。

原始数据类型

待插入的list的数据如下:
List<TraderUserFeeResultBean> beanList;

Mapper接口里的方法:

/**
 * 批量更新用户费率表 trader_users_fee_result表
 * @param bean
 */
void insertUserByBeans(List<TraderUserFeeResultBean> bean);

xml里的sql写法

replace into trader_users_fee_result ( user_id,ename,month, first_level_organization_commission,second_level_organization_commission, third_level_organization_commission,fourth_level_organization_commission, first_level_organization_settle_fee,second_level_organization_settle_fee, third_level_organization_settle_fee,fourth_level_organization_settle_fee ) values ( #{item.user_id},#{item.ename},#{item.month}, #{item.first_level_organization_commission},#{item.second_level_organization_commission}, #{item.third_level_organization_commission},#{item.fourth_level_organization_commission}, #{item.first_level_organization_settle_fee},#{item.second_level_organization_settle_fee}, #{item.third_level_organization_settle_fee},#{item.fourth_level_organization_settle_fee} )
在foreach里,每个item就是一个bean对象,可以通过对象.属性的方式获取值,index=“index”里的index标识list集合的下标值。
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值