mybatis 通用更新抽离写法

本文介绍了一个账户日期更新机制的设计与实现。通过监听消息并解析JSON数据来获取更新所需的参数,根据文档类型确定目标数据表,并调用服务进行更新操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前提是要更新的表中的字段名称必须命名统一

1. 

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Component
public class AccountDateListener {
   private static final Logger logger = LoggerFactory.getLogger(AccountDateListener.class);
   
   @Autowired
   private AccountDateService accountDateService;
   
    @Override
    public void consumeMessage(String body) throws Exception {
      JSONObject data = JSONObject.parseObject(body);
      Integer accountDate = data.getInteger("accountDate");
      Integer docType = data.getInteger("docType");
      Integer docId = data.getInteger("docId");
      accountDateService.updateAccountDate(docType, docId, accountDate);
    }
    
}

2.

public interface AccountDateService {
   void updateAccountDate(int docType, int docId, int accountDate);
}

3.


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class AccountDateServiceImpl implements AccountDateService {

   @Autowired
   private AccountDateMapper accountDateMapper;
   
   
   public void updateAccountDate(int docType, int docId, int accountDate){
       String tableName = getTable(docType);
      if(!"".equals(tableName)){
         updateAccountDate(tableName, docId, accountDate);
      }
    }
   
   public String getTable(int docType){
       String tableName = "";
       if(docType == DocType.Rec.getCode()||
            docType == AiDocType.InsureRec.getCode()){
         tableName = "t_fi_rec";
      }else if(docType == DocType.Pay.getCode()||
            docType == DocType.InsurePay.getCode()){
         tableName = "t_fi_pay";
      }
       return tableName;
    }
   
   @Override
   public int updateAccountDate(String tableName, int docId, int accountDate) {
      Map<String, Object> filter = new HashMap<String, Object>();
      filter.put("tableName", tableName);
      filter.put("id", docId);
      filter.put("accountDate", accountDate);
      filter.put("uT", Helper.getCurrentTime());
      int count = accountDateMapper.updateAccountDate(filter);
      if(count == 0){
         throw new BusinessException("数据更新失败"+docId+"数据表:"+tableName);
      }
      return count;
   }

}

3.接口

public interface AccountDateMapper {
   int updateAccountDate(Map<String, Object> filter);
}

4.  mapper 的写法 :

<update id="updateAccountDate" parameterType="Map">
       update ${tableName} set account_date = #{accountDate}, u_t = #{uT}
       where is_deleted = 0 and id = #{id}
</update>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇凝子潇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值