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,如需转载请自行联系原作者



相关文章
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
658 0
|
SQL Java 数据库连接
MyBatis动态SQL的List传值错误
MyBatis动态SQL的List传值错误
277 0
|
SQL Java 数据库连接
Hibernate查询之SQL查询,查询结果用new新对象的方式接受,hql查询,通过SQL查询的结果返回到一个实体中,查询不同表中内容,并将查到的不同表中的内容放到List中
 package com.ucap.netcheck.dao.impl; import java.util.ArrayList;import java.util.List; import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.spring
1324 0
|
SQL 数据库
Powerdesigner 16.5 从SQL Server 2012做逆向工程时提示:Unable to list tables问题
原文:Powerdesigner 16.5 从SQL Server 2012做逆向工程时提示:Unable to list tables问题 公司深圳团队开发有一套系统在华北区这边推向客户,在一次更新补丁时,由于发生了数据字典的变更,但深圳团队并未给出数据库的更新脚本,只给了新版本的数据库创建脚本,为了保证客户方系统中已有数据不丢失,只能自己想办法了:用Powerdesigner把新版本数据库逆向过来后,将此模型Apply到已有数据库中,此时Powerdesigner会比较两个版本的差异,只更新差异而不会丢失数据了。
1187 0
|
SQL Java 索引
bboss 持久层sql语句中一维/多维数组类型变量、list变量、map变量、bean对象变量使用说明
本文介绍bboss 持久层sql语句中一维/多维数组类型变量、list变量、map变量、bean对象变量使用方法,该功能在bboss 3.5.2版本及后续版本提供。 很高兴地告诉大家,bboss模板sql中已经可以处理对象、数组、list、map类型的变量了,bboss 能够快速分析出这些变量,并将sql语句转换为预编译sql语句执行。
1155 0
|
关系型数据库 MySQL 网络安全
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
5-10Can't connect to MySQL server on 'sh-cynosl-grp-fcs50xoa.sql.tencentcdb.com' (110)")
|
SQL 存储 监控
SQL Server的并行实施如何优化?
【7月更文挑战第23天】SQL Server的并行实施如何优化?
590 13
解锁 SQL Server 2022的时间序列数据功能
【7月更文挑战第14天】要解锁SQL Server 2022的时间序列数据功能,可使用`generate_series`函数生成整数序列,例如:`SELECT value FROM generate_series(1, 10)。此外,`date_bucket`函数能按指定间隔(如周)对日期时间值分组,这些工具结合窗口函数和其他时间日期函数,能高效处理和分析时间序列数据。更多信息请参考官方文档和技术资料。
407 9
|
SQL 存储 网络安全
关系数据库SQLserver 安装 SQL Server
【7月更文挑战第26天】
286 6