1. 程式人生 > 其它 >Mybatis中update與foreach搭配使用,collection引數為list型別

Mybatis中update與foreach搭配使用,collection引數為list型別

需求

新增一個介面,實現對錶t_c_nsos_mappercfg的修改操作,入參報文如下:

{

“params”:{

“nsDeptSubsName”: “鐵通”,

“excludeNsDeptIds”:“20131223162919,20131223162834,20131223162708,20131223162757”

}}

要求對newSysName欄位值包含“鐵通”二字,但newSysID欄位值不為“20131223162919,20131223162834,20131223162708,20131223162757”其中的任何一個,

獲取到符合要求的資料將dyfield1欄位值更新為1,介面返回更新的記錄條數。

update t_c_nsos_mappercfg set dyfield3 = 1
where newSysName like '%鐵通%'
and newSysID not in (20131223162919,20131223162834,20131223162708,20131223162757)
and id > 0;

寫符合需求的sql語句

update t_c_nsos_mappercfg set dyfield3 = 1
where newSysName like '%鐵通%'
and newSysID not in (20131223162919,20131223162834,20131223162708,20131223162757)
and id > 0;

UpdCfgMapper.java


@Repository
public interface UpdCfgMapper {
int updateCfg(Map<String,Object> paramMap);
}

UpdCfgService.java

@Service("updCfgService")
public class UpdCfgService {
private static final Logger LOGGER = LoggerFactory.getLogger(UpdCfgService.class);

private final UpdCfgMapper updCfgMapper;

@Autowired
public UpdCfgService(UpdCfgMapper updCfgMapper) {
this.updCfgMapper = updCfgMapper;
}

public void updCfg(@RequestBody InputObject inputObject, OutputObject outputObject) {
try {
Map<String, Object> paramsMap = inputObject.getParams();
String excludeNsDeptIds
= MapUtils.getString(paramsMap, "excludeNsDeptIds");

paramsMap.put("excludeNsDeptIds", excludeNsDeptIds.split(","));
int count = updCfgMapper.updateCfg(paramsMap);
Map<String, Object> objectMap = new HashMap<>();
objectMap.put("count", count);
outputObject.setObject(objectMap);
outputObject.setReturnCode(EsbOutObject.RTN_CODE.SUCCESS);
} catch (Exception e) {
outputObject.setReturnCode(EsbOutObject.RTN_CODE.ERROR);
LOGGER.error("UpdCfgService.updCfg,Exception:" + e);
}
}
}

updCfgMapper.xml

<update id="updateCfg" parameterType="map">
update t_c_nsos_mappercfg
set dyfield4 = 6
where newSysName like concat('%',#{nsDeptSubsName},'%')
and newSysID not in
<foreach item="value" index="key" collection="excludeNsDeptIds" open="(" close=")" separator=",">
#{value}
</foreach>
and id > 0
</update>

xml檔案環境新增到

<mapper resource="orm/test/updCfgMapper.xml"/>

介面對映

<action path="guest/updCfg">
<input uid="POST" scope="POST" service="updCfgService" method="updCfg"
desc="修改新老系統工單派送資訊"/>
</action>



————————————————
版權宣告:下文為CSDN博主「岸遠水聲微」的原創文章。
原文連結:https://blog.csdn.net/hzygcs/article/details/103419788

最近遇到了一個關於mybatis的問題,是使用foreach時導致的問題。報錯提示:‘The expression ‘XXX’ evaluated to a null value…’。

解決問題的同時也發現了對知識點的一知半解,平時只是用,並沒有對知識點進行細究。

常見的foreach樣式
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">#{item}</foreach>

在使用foreach的時候,collection屬性往往是最容易出錯的,在不同情況下,該屬性需要設定不同的值,如果不知道這點,就容易出錯。

主要有以下3種情況:

當傳入的是單引數且引數型別是一個List的時候,collection屬性值要寫死為list,collection="list"。
當傳入的是單引數且引數型別是一個array陣列的時候,collection的屬性值要寫死為array,collection="array"。
當傳入的引數是一個Map型別時,collection屬性的值是你傳遞的Map中,List或array物件對應的key的名稱,可以任意指定名稱。