sql中傳入一個(gè)list,返回一個(gè)list

-----------傳入數(shù)組------返回list<string>----------

String[] sendPersonIdArr = sendPersonId.split(",");
List<String> list = staffInfoService.ListPhonesByIds(sendPersonIdArr);


<!-- 通過(guò)userIds查詢(xún)員工的電話(huà)-->
	<select id="ListPhonesByIds" parameterType="String" resultType="String">
	 	SELECT h.telphone from hr_staff_info h left JOIN sys_user u on h.STAFFINFO_ID=u.STAFF_ID where 
 			u.id in  
 			<foreach collection="array" index="index" item="item" open="(" separator="," close=")">
				 #{item}
			</foreach>
	</select>

-----傳入List<string>----返回List<User>------
 
public List<User> findByUserIdList(List<String> userlist) throws Exception {
	return (List<User>) dao.findForList("UserMapper.findByUserIdList", userlist);
}

<!-- 通過(guò)userId的list來(lái)查詢(xún)userlist -->
	<select id="findByUserIdList" parameterType="java.util.List" resultMap="userResultMap" >
	select * from sys_user  where id in 
	 <foreach collection="list"  item="item" index="index" open="(" separator="," close=")">  
		 #{item}
	</foreach>
	</select>
-----------傳入一個(gè)map---------批量修改數(shù)據(jù)---------------

controller中:

Map<String, Object> map = new HashMap<>();
		map.put("notifyNum", notifyNum);
		map.put("userIdArr", userIdArr);
		userService.sendNotify(map);
		
sql中:

<!--發(fā)送通知,保存notifyNum 到user表中的notifyCodes中-->
<update id="sendNotify" parameterType="java.util.Map" >
	update user 
	set notify_codes=if(notify_codes is null or notify_codes='',#{notifyNum},CONCAT(notify_codes,',',#{notifyNum}))
	 where id in
        <foreach collection="userIdArr" index="index" item="item" open="(" separator="," close=")">
			  #{item}
       </foreach>
</update>		
		
總結(jié):
①這里傳入了一個(gè)String notifyNum和一個(gè)String[] userIdArr ,我們只要在sql中名稱(chēng)匹配就可以了。
②批量修改也可以用in 
③在修改的時(shí)候,我們可以在原來(lái)的字段值中直接后面追加字符串。當(dāng)原來(lái)的值為數(shù)字的時(shí)候,我們可以  update user set notify_codes=notify_codes+'2'  where id='24' 
這樣,假設(shè)原來(lái)為5,那么現(xiàn)在就為 7 了。
當(dāng)原來(lái)的值是一個(gè)String類(lèi)型時(shí),我們可以用 CONCAT(notify_codes,',',#{notifyNum}) 來(lái)在后面追加 。比如原來(lái)為  "12" 現(xiàn)在最加一個(gè)  ",13"  那么結(jié)果為  "12,13"   
④判斷一個(gè)字段是否為空的時(shí)候,用這樣用  if(notify_codes is null or notify_codes='','為空或空字符串返回這個(gè)值','非空的時(shí)候返回這個(gè)值')  		

第二種方式:整條語(yǔ)句循環(huán)  (自己未驗(yàn)證)

創(chuàng)新互聯(lián)主營(yíng)宣恩網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,重慶App定制開(kāi)發(fā),宣恩h5小程序開(kāi)發(fā)搭建,宣恩網(wǎng)站營(yíng)銷(xiāo)推廣歡迎宣恩等地區(qū)企業(yè)咨詢(xún)

<update id="batchUpdate"  parameterType="java.util.List">  
        
          <foreach collection="list" item="item" index="index" open="" close="" separator=";">  
                update test   
                <set>  
                  test=${item.test}+1  
                </set>  
                where id = ${item.id}  
         </foreach>  
            
    </update>

sql中我們可以傳入一個(gè)list或者一個(gè)數(shù)組,返回一個(gè)list。

這里用到了sql中的 In,用到了sql中的遍歷。

在我們要向mapper.xml中傳遞String參數(shù)的時(shí)候,需要sql中設(shè)置

parameterType="String"

同時(shí) 要保證impl中的參數(shù)名和sql中的名字要一致。

如下:

@Override
	public User findByUE(String userId)throws Exception{
		return (User)dao.findForObject("UserMapper.findById",userId);
	}
	
	sql :
	u.id = #{userId}

網(wǎng)站題目:sql中傳入一個(gè)list,返回一個(gè)list
網(wǎng)站地址:http://muchs.cn/article20/pphjjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版App開(kāi)發(fā)、網(wǎng)站設(shè)計(jì)公司、定制網(wǎng)站自適應(yīng)網(wǎng)站、建站公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)