MyBatis系列四 之 智能标签进行查询语句的拼接
2016-12-19 14:22
344 查看
MyBatis系列四 之 智能标签进行查询语句的拼接
使用Foreach进行多条件查询
1.1 foreach使用数组进行多条件查询
在MyBatis的映射文件中进行如下配置
在接口类中定义和映射文件中的查询语句的id值相同的方法名称
在测试类中进行如下代码的书写进行测试
1.2foreach使用list泛型集合进行多条件查询
在MyBatis的映射文件中做如下配置
在接口类中定义一个和银蛇文件中id值相同的方法名称
在测试类中书写如下代码进行代码测试
1.3foreach使用自定义的泛型集合进行多条件查询
在MyBatis的映射文件中做如下配置
在接口中定义一个和映射文件中插叙语句的id值相同的方法名称
在测试类中书写如下代码进行代码测试
使用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()); } }
相关文章推荐
- mybatis 查询时 拼接order by语句
- mybatis按照关键字(符号)截取字符串,字符串拼接,循环查询语句,分组求和函数用法
- MyBatis下动态语句使用like进行模糊查询
- mybatis使用select语句进行关联查询
- MyBatis 模糊查询-拼接SQL语句
- 使用json实现查询条件传回Action进行hql语句拼接查询实现在结果中查询功能在datagrid中显示查询结果
- MyBatis智能标签查询
- 使用MyBatis对项目中的统计功能进行处理的查询语句优化
- 简单sql查询接口(需要在文件中指定conn才能进行sql语句操作)
- ASP.NET偷懒大法三 (利用Attribute特性简化多查询条件拼接sql语句的麻烦)
- 有用的IsNull用于拼接select语句的多个字段(适用于查询结果有空的时候)
- Oracle 简单拼接查询语句
- 简单sql查询接口(输入user:pwd@host即可进行sql语句操作)
- 让存储过程中的查询语句按照不同查询条件进行优化(即每次查询都使用参数option(recompile))
- 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询
- 数据查询语句的拼接
- 查询模板拼接成sql语句的地方
- php对特殊语句查询结果进行数组排序
- SQL语句对查询结果进行行转列