MyBatis系列四 之 智能标签进行查询语句的拼接

                                              MyBatis系列四 之 智能标签进行查询语句的拼接

使用Foreach进行多条件查询

1.1 foreach使用数组进行多条件查询

在MyBatis的映射文件中进行如下配置

 <!--根据数组进行多条件查询  -->
   <select id="findByForeachAraay" resultType="Student">
        select * from Student
       <if test="array.length>0">
       where sid in
         <foreach collection="array" open="(" close=")" separator="," item="myid">
            #{myid}
         </foreach>
       
       </if>
   </select>

在接口类中定义和映射文件中的查询语句的id值相同的方法名称

//根据数组查询
   public List<Student> findByForeachAraay(int[] ids);

在测试类中进行如下代码的书写进行测试

//根据数组进行多条件查询
    @Test
    public void findByForeachAraay() throws IOException{
        
        int[] ids=new int[2];
        ids[0]=48;
        ids[1]=50;
        
        List<Student> list = dao.findByForeachAraay(ids);
        for (Student student : list) {
            System.out.println(student.getSname());
        }
    }

1.2foreach使用list泛型集合进行多条件查询

在MyBatis的映射文件中做如下配置

<!--根据list进行多条件查询  -->
   <select id="findByForeachlist" resultType="Student">
        select * from Student
       <if test="list.size>0">
       where sid in
         <foreach collection="list" open="(" close=")" separator="," item="myid">
            #{myid}
         </foreach>
       
       </if>
   </select>

在接口类中定义一个和银蛇文件中id值相同的方法名称

 //根据list集合进行多条件查询
   public List<Student> findByForeachlist(List<Integer> ids);

在测试类中书写如下代码进行代码测试

//根据list集合进行多条件查询
        @Test
        public void findByForeachlist() throws IOException{
            
            List<Integer> ids=new ArrayList<Integer>();
            ids.add(49);
            ids.add(50);
            
            List<Student> list = dao.findByForeachlist(ids);
            for (Student student : list) {
                System.out.println(student.getSname());
            }
        }

1.3foreach使用自定义的泛型集合进行多条件查询

在MyBatis的映射文件中做如下配置

<!--根据自定义泛型集合进行多条件查询  -->
   <select id="findByForeachMyList" resultType="Student">
        select * from Student
       <if test="list.size>0">
       where sid in
         <foreach collection="list" open="(" close=")" separator="," item="stu">
            #{stu.sid}
         </foreach>
       
       </if>
   </select>

在接口中定义一个和映射文件中插叙语句的id值相同的方法名称

 //根据自定义泛型集合进行多条件查询
   public List<Student> findByForeachMyList(List<Student> ids);

在测试类中书写如下代码进行代码测试

//根据自定义泛型集合进行多条件查询
            @Test
            public void findByForeachMyList() throws IOException{
                
                List<Student> ids=new ArrayList<Student>();
                Student stu=new Student();
                stu.setSid(48);
                Student stu2=new Student();
                stu2.setSid(49);
                ids.add(stu);
                ids.add(stu2);
                
                List<Student> list = dao.findByForeachMyList(ids);
                for (Student student : list) {
                    System.out.println(student.getSname());
                }
            }
原文地址:https://www.cnblogs.com/hmy-1365/p/6197332.html