Mybatis_9动态sql之foreach

时间:2021-10-15 22:43:15

例子:根据多个id查询用户

 

User.xml:

Mybatis_9动态sql之foreachMybatis_9动态sql之foreach
 1     <!--多个id查询 通过queryvo -->
 2     <select id="selectUserByIds" parameterType="QueryVo"
 3         resultType="User">
 4         <include refid="selector" />
 5         <where>
 6             id in
 7             <foreach collection="idlist" item="id" separator="," open="("
 8                 close=")">
 9                 #{id}
10             </foreach>
11         </where>
12     </select>
User.xml

UserMapper.java:

Mybatis_9动态sql之foreachMybatis_9动态sql之foreach
 1 package com.me.mybatis.mapper;
 2 
 3 import java.util.List;
 4 
 5 import com.me.mybatis.pojo.QueryVo;
 6 import com.me.mybatis.pojo.User;
 7 
 8 public interface UserMapper {
 9         //遵循四个原则
10         //1.接口方法名==user.xml文件中的id名
11         //2.返回值类型与mapper.xml文件中的返回值类型要一致
12         //3.方法的入参类型与mapper.xml中入参的类型一致
13         //4.mapper.xml文件的namespace与接口绑定在一起
14         public User findUserById(Integer id);
15         public List<User> findUserByQueryVo(QueryVo vo);
16         public Integer countUsers();
17         public List<User> selectUserBySexAndUserName(User user);
18         //根据多个id查询用户信息
19         public List<User> selectUserByIds(Integer[] ids);
20         public List<User> selectUserByIds(List<Integer> ids);
21         public List<User> selectUserByIds(QueryVo vo);
22         
23 }
UserMapper.java

QueryVo.java:

Mybatis_9动态sql之foreachMybatis_9动态sql之foreach
 1 package com.me.mybatis.pojo;
 2 
 3 import java.io.Serializable;
 4 import java.util.List;
 5 
 6 public class QueryVo implements Serializable {
 7 
 8     /**
 9      * 
10      */
11     private static final long serialVersionUID = 1L;
12     private User user;
13     private List<Integer> idlist;
14     private Integer[] ids;
15     public List<Integer> getIdlist() {
16         return idlist;
17     }
18     public void setIdlist(List<Integer> idlist) {
19         this.idlist = idlist;
20     }
21     public Integer[] getIds() {
22         return ids;
23     }
24     public void setIds(Integer[] ids) {
25         this.ids = ids;
26     }
27     public User getUser() {
28         return user;
29     }
30     public void setUser(User user) {
31         this.user = user;
32     }
33 }
QueryVo.java

测试类:

Mybatis_9动态sql之foreachMybatis_9动态sql之foreach
 1 @Test
 2     void testselectUserByIds() throws Exception {
 3         // 加载核心配置文件
 4         String resource = "SqlMapConfig.xml";
 5         InputStream resourceAsStream = Resources.getResourceAsStream(resource);
 6         // 创建sqlsessionFactory
 7         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
 8         // 创建sqlsession
 9         SqlSession openSession = sqlSessionFactory.openSession();
10         //sqlsession生成一个实现类
11         UserMapper mapper = openSession.getMapper(UserMapper.class);
12         //利用接口调方法
13         List<Integer> ids=new ArrayList<Integer>();
14         ids.add(16);
15         ids.add(22);
16         ids.add(24);
17         QueryVo vo=new QueryVo();
18         vo.setIdlist(ids);
19         List<User> users = mapper.selectUserByIds(vo);
20         for (User user1 : users) {
21             
22             System.out.println(user1);
23         }
24         
25     }
测试类

结果:

Mybatis_9动态sql之foreach

 

 

注意:

index:为数组的下标。

item:为数组每个元素的名称,名称随意定义

open:循环开始

close:循环结束

separator:中间分隔输出

 

 

别的都一样但是应该注意:、

Mybatis_9动态sql之foreach