12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
ADADADADAD
mysql数据库 时间:2024-12-25 09:56:15
作者:文/会员上传
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
12-09
-----------传入数组------返回list<string>----------String[]sendPersonIdArr=sendPersonId.split(",");List<String>list=staffInfoService.ListPhonesByIds(sendPersonI
以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。
-----------传入数组------返回list<string>----------String[]sendPersonIdArr=sendPersonId.split(",");List<String>list=staffInfoService.ListPhonesByIds(sendPersonIdArr);<!--通过userIds查询员工的电话--><selectid="ListPhonesByIds"parameterType="String"resultType="String">SELECTh.telphonefromhr_staff_infohleftJOINsys_useruonh.STAFFINFO_ID=u.STAFF_IDwhereu.idin<foreachcollection="array"index="index"item="item"open="("separator=","close=")">#{item}</foreach></select>-----传入List<string>----返回List<User>------publicList<User>findByUserIdList(List<String>userlist)throwsException{return(List<User>)dao.findForList("UserMapper.findByUserIdList",userlist);}<!--通过userId的list来查询userlist--><selectid="findByUserIdList"parameterType="java.util.List"resultMap="userResultMap">select*fromsys_userwhereidin<foreachcollection="list"item="item"index="index"open="("separator=","close=")">#{item}</foreach></select>
-----------传入一个map---------批量修改数据---------------controller中:Map<String,Object>map=newHashMap<>();map.put("notifyNum",notifyNum);map.put("userIdArr",userIdArr);userService.sendNotify(map);sql中:<!--发送通知,保存notifyNum到user表中的notifyCodes中--><updateid="sendNotify"parameterType="java.util.Map">updateusersetnotify_codes=if(notify_codesisnullornotify_codes='',#{notifyNum},CONCAT(notify_codes,',',#{notifyNum}))whereidin<foreachcollection="userIdArr"index="index"item="item"open="("separator=","close=")">#{item}</foreach></update>总结:①这里传入了一个StringnotifyNum和一个String[]userIdArr,我们只要在sql中名称匹配就可以了。②批量修改也可以用in③在修改的时候,我们可以在原来的字段值中直接后面追加字符串。当原来的值为数字的时候,我们可以updateusersetnotify_codes=notify_codes+'2'whereid='24'这样,假设原来为5,那么现在就为7了。当原来的值是一个String类型时,我们可以用CONCAT(notify_codes,',',#{notifyNum})来在后面追加。比如原来为"12"现在最加一个",13"那么结果为"12,13"④判断一个字段是否为空的时候,用这样用if(notify_codesisnullornotify_codes='','为空或空字符串返回这个值','非空的时候返回这个值')
第二种方式:整条语句循环 (自己未验证)
<updateid="batchUpdate"parameterType="java.util.List"><foreachcollection="list"item="item"index="index"open=""close=""separator=";">updatetest<set>test=${item.test}+1</set>whereid=${item.id}</foreach></update>
sql中我们可以传入一个list或者一个数组,返回一个list。
这里用到了sql中的 In,用到了sql中的遍历。
在我们要向mapper.xml中传递String参数的时候,需要sql中设置
parameterType="String"
同时 要保证impl中的参数名和sql中的名字要一致。
如下:
@OverridepublicUserfindByUE(StringuserId)throwsException{return(User)dao.findForObject("UserMapper.findById",userId);}sql:u.id=#{userId}
11-20
11-19
11-20
11-20
11-20
11-19
11-20
11-20
11-19
11-20
11-19
11-19
11-19
11-19
11-19
11-19