sql中传入一个list,返回一个list

-----------传入数组------返回list<string>----------

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


<!-- 通过userIds查询员工的电话-->
	<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);
}

<!-- 通过userId的list来查询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>
-----------传入一个map---------批量修改数据---------------

controller中:

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

<!--发送通知,保存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>		
		
总结:
①这里传入了一个String notifyNum和一个String[] userIdArr ,我们只要在sql中名称匹配就可以了。
②批量修改也可以用in 
③在修改的时候,我们可以在原来的字段值中直接后面追加字符串。当原来的值为数字的时候,我们可以  update user set notify_codes=notify_codes+'2'  where id='24' 
这样,假设原来为5,那么现在就为 7 了。
当原来的值是一个String类型时,我们可以用 CONCAT(notify_codes,',',#{notifyNum}) 来在后面追加 。比如原来为  "12" 现在最加一个  ",13"  那么结果为  "12,13"   
④判断一个字段是否为空的时候,用这样用  if(notify_codes is null or notify_codes='','为空或空字符串返回这个值','非空的时候返回这个值')  		

第二种方式:整条语句循环  (自己未验证)

<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中我们可以传入一个list或者一个数组,返回一个list。

这里用到了sql中的 In,用到了sql中的遍历。


在我们要向mapper.xml中传递String参数的时候,需要sql中设置

parameterType="String"

同时 要保证impl中的参数名和sql中的名字要一致。

如下:

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






      本文转自建波李 51CTO博客,原文链接:http://blog.51cto.com/jianboli/2045357,如需转载请自行联系原作者



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值