前提是要更新的表中的字段名称必须命名统一
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>