一:使用动态sql完成多条件查询
a:使用if+where实现多条件查询
首先场景需求,有 个年级和班级表,第一个要求是根据模糊查询姓名,和年龄大小进行条件查询,接口层方法
public ListgetStudentByIf(student stu);
其次是映射文件的配置
商品查询功能提供了一个快速查看商品的途径。商品查询分为基本查询和高级查询。基本查询:提供关键字和商品大类两种条件的查询,用户可以只填写关键字或者选择商品大类或者关键字和商品大类都填写来查询商品。高级查询:提供关键字,商品大类,商品小类,商品价格范围四种条件的查询,用户可以任意填写其中一种或几种的查询条件来查询想要了解的商品信息。商品查询功能大大的方便了用户,提高了网站的用户体验。(5)帮助系统模块
测试
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.= "z"Listlist="----------"+
----------zhangyu
----------zy
----------zy
----------zhang
b:choose when 分类
这种方式和java中choose循环结构原理是一样的,判断多种情况,只要修改一下映射文件即可
接口 类
public ListgetAllStudentByLike(Map userMap); //使用map作为参数
映射文件
<select id="getAllStudentByLike" parameterType="Map" resultType="stu">select * from student<where><choose><when test="stuName!=null"> stuName like CONCAT('%',#{stuName},'%')when><when test="stuAge!=0"> stuAge> #{stuAge}when>
1=1
choose>where>select>
结果
zhangyu zy zy zhang
c:使用foreach完成复杂 查询,有三种方式,
第一种:传入的参数为数组类型
//传一组 xueshengID public ListgetStudentBystuId_foreach_array(Integer[] ints); 映射文件配置
测试类
Integer[] ints = {2,3,4};
List list = dao.getStudentBystuId_foreach_array(ints);for (student item:list) {
System.out.println(item.getStuName());
}
第二种:传入list集合
public ListgetStudentBystuId_foreach_list(List list);
测试:
studentDao dao = MyBatis.getSessionTwo().getMapper(studentDao.class);
Integer ints = 2;
List list = new ArrayList();
list.add(ints);
List stulist = dao.getStudentBystuId_foreach_list(list);
for (student item:stulist) {
System.out.println(item.getStuName());
}
第三种:根据Map集合
public ListgetStudentBystuId_foreach_map(Map stuMap);
Mapstumap = new HashMap (); List listStuId = new ArrayList (); listStuId.add(2); listStuId.add(3); listStuId.add(4); stumap.put("stuId",listStuId); List list = dao.getStudentBystuId_foreach_map(stumap); for (student item:list ) { System.out.println(item.getStuName()); }
打印结果可以执行以下。
d;一对多的两种实现方式
主要是resultMapper里的配置不同
接口方法
public grade getGradeById(int gradeId);
映射文件配置
<resultMap id="gradeMapOne" type="grade"><id column="gradeId" property="gradeId">id><result column="gradeName" property="gradeName">result><collection property="gatStudent" ofType="stu"><id column="stuUd" property="stuId">id><result column="stuName" property="stuName">result><result column="stuAge" property="stuAge">result>collection>resultMap><resultMap id="gradeMap" type="entity.grade"><id column="gradeId" property="gradeId">id><result column="gradeName" property="gradeName">result><collection property="gatStudent" ofType="student" select="getStudentById" column="gradeId">collection> resultMap>
@Testpublic void TestConn(){
gradeDao dao = MyBatis.getSessionTwo().getMapper(gradeDao.class);
grade grade = dao.getGradeById(1); for (student item:grade.getGatStudent() ) {
System.out.println(item.getStuName());
}
}
两种方式都能实现,打印效果
方案一打印效果
==> Preparing: select * from grade,student where grade.gradeId = student.stuGrade and gradeId = ? ============一条sql
==> Parameters: 1(Integer)
zhangyu
zy
zy
Process finished with exit code 0
方案二打印效果
==> Preparing: select * from grade where gradeId=? ==========第一条sql
==> Parameters: 1(Integer)
====> Preparing: select * from student where stuGrade = ? ==========第二条sql
====> Parameters: 1(Long)
zhangyu
zy
zy
Process finished with exit code 0









