mybatis传值List类型的参数如何遍历
2018-07-10 · 国内最优秀java资源共享平台
关注
展开全部
基本概念
@Test
public void findUserList() throws Exception{
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
UserQueryVo queryVo = new UserQueryVo();
UserCustomer customer = new UserCustomer();
queryVo.setCustomer(customer);
List<Integer> ids=new ArrayList<Integer>();
ids.add(1);
ids.add(10);
ids.add(22);
queryVo.setIds(ids);
List<UserCustomer> list = mapper.findUserList(queryVo);
for (UserCustomer userCustomer : list) {
System.out.println(userCustomer.getId()+":"+userCustomer.getUsername());
}
session.close();
}
2.创建UserQueryVo
public class UserQueryVo {
//传递多个id
private List<Integer> ids;
//在这里包装查询条件(用户查询条件)
/*private User user;*/
private UserCustomer customer;
set get 省
}
public class UserCustomer extends User {
//在这里面进行扩展用户信息
}
public class User implements Serializable {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
//添加订单属性
private List<Orders> ordersList;
set get 省
3. UserMapper
public interface UserMapper {
//用户信息综合查询列表
public List<UserCustomer> findUserList(UserQueryVo queryVo) throws Exception;
}
4.mapper.xml
<!-- 用户综合查询列表
#{customer.sex} : 取出pojo包装类型里面的性别的值
${customer.name}: 取出pojo对象里面的用户名称
-->
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustomer">
select * from user <!-- user.sex=#{customer.sex} and user.username like '%${customer.username}%' -->
<where>
<!-- 如果引用sql片段不在这个 映射文件中那么前面就要加上namespace -->
<include refid="query_sql_where"/>
</where>
<!-- 定义一个sql 片段
id: 是sql片段的唯一标识
基于单表定义sql片段 , 那么这样sql片段重用性才高;
sql片段里面不要包含where
-->
<sql id="query_sql_where">
<if test="customer.sex!=null and customer.sex!=''">
and user.sex=#{customer.sex}
</if>
<if test="customer.username!=null and customer.username!=''">
and user.username like '%${customer.username}%'
</if>
<if test="ids!=null">
<!--
collection : 指定对象集合里面的属性 ids
item : 遍历的时候每次生成的名称 user_id
open : 开始遍历的时候 拼接串AND (
close : 遍历结束时拼接串 )
separator : 每次遍历的时候要拼接的串
-->
<!-- AND ( id=1 OR id=10 OR id=22 -->
<!-- <foreach collection="ids" item="user_id" open="AND ( " close=")" separator="or">
每次遍历需要拼接的串
id=#{user_id}
</foreach> -->
<!-- AND id IN(1,10,22)-->
<foreach collection="ids" item="user_id" open="AND id IN( " close=")" separator=",">
<!-- 每次遍历需要拼接的串 -->
#{user_id}
</foreach>
</if>
</sql>
WHERE id=1 OR id=10 OR id=16
在查询条件中,查询条件定义成一个sql片段,需要修改sql片段
如图所示
案例:
1.创建测试类@Test
public void findUserList() throws Exception{
SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
SqlSession session = sqlSessionFactory.openSession();
UserMapper mapper = session.getMapper(UserMapper.class);
UserQueryVo queryVo = new UserQueryVo();
UserCustomer customer = new UserCustomer();
queryVo.setCustomer(customer);
List<Integer> ids=new ArrayList<Integer>();
ids.add(1);
ids.add(10);
ids.add(22);
queryVo.setIds(ids);
List<UserCustomer> list = mapper.findUserList(queryVo);
for (UserCustomer userCustomer : list) {
System.out.println(userCustomer.getId()+":"+userCustomer.getUsername());
}
session.close();
}
2.创建UserQueryVo
public class UserQueryVo {
//传递多个id
private List<Integer> ids;
//在这里包装查询条件(用户查询条件)
/*private User user;*/
private UserCustomer customer;
set get 省
}
public class UserCustomer extends User {
//在这里面进行扩展用户信息
}
public class User implements Serializable {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
//添加订单属性
private List<Orders> ordersList;
set get 省
3. UserMapper
public interface UserMapper {
//用户信息综合查询列表
public List<UserCustomer> findUserList(UserQueryVo queryVo) throws Exception;
}
4.mapper.xml
<!-- 用户综合查询列表
#{customer.sex} : 取出pojo包装类型里面的性别的值
${customer.name}: 取出pojo对象里面的用户名称
-->
<select id="findUserList" parameterType="UserQueryVo" resultType="UserCustomer">
select * from user <!-- user.sex=#{customer.sex} and user.username like '%${customer.username}%' -->
<where>
<!-- 如果引用sql片段不在这个 映射文件中那么前面就要加上namespace -->
<include refid="query_sql_where"/>
</where>
<!-- 定义一个sql 片段
id: 是sql片段的唯一标识
基于单表定义sql片段 , 那么这样sql片段重用性才高;
sql片段里面不要包含where
-->
<sql id="query_sql_where">
<if test="customer.sex!=null and customer.sex!=''">
and user.sex=#{customer.sex}
</if>
<if test="customer.username!=null and customer.username!=''">
and user.username like '%${customer.username}%'
</if>
<if test="ids!=null">
<!--
collection : 指定对象集合里面的属性 ids
item : 遍历的时候每次生成的名称 user_id
open : 开始遍历的时候 拼接串AND (
close : 遍历结束时拼接串 )
separator : 每次遍历的时候要拼接的串
-->
<!-- AND ( id=1 OR id=10 OR id=22 -->
<!-- <foreach collection="ids" item="user_id" open="AND ( " close=")" separator="or">
每次遍历需要拼接的串
id=#{user_id}
</foreach> -->
<!-- AND id IN(1,10,22)-->
<foreach collection="ids" item="user_id" open="AND id IN( " close=")" separator=",">
<!-- 每次遍历需要拼接的串 -->
#{user_id}
</foreach>
</if>
</sql>
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询