1.8 动态SQL****
1.8.1 多条件where****
where标签只负责第一个if标签中的and的有无,如果第一个标签值为空,则第二个做第一个
Test:放表达式****
stuName:属性值****
<select id= "queryStuByNOrAWishSQLTag" parameterType= "student" resultType= "student" > select stuno,stuname,stuage from student <if test= "stuName !=null and stuName!='' " > and stuname = #{stuName} <if test= "stuAge !=null and stuAge!=0 " > and stuage = #{stuAge} ****
1.8.2 迭代****
Collection:要迭代的属性 相当于for(String a:As)中的as****
Item:单个值 相当于for(String a:As)中的a****
Open:where中的起始位置****
Close:结束为止****
Separator:多个值得连接符****
<select id= "queryStudentsWithNosInGrade" parameterType= "grade" resultType= "student" > select * from student <if test= "stuNos!=null and stuNos.size>0" > <foreach collection= "stuNos" open= " and stuno in (" close= ")" item= "stuNo" separator= "," > #{stuNo} ****
1.8.3 简单的数组****
无论编写代码时,传递的是什么参数名(stuNos),在mapper.xml中 必须用array代替该数组****
标签中的属性 和上边的一样****
<select id= "queryStudentsWithArray" parameterType= "int[]" resultType= "student" > select * from student <if test= "array!=null and array.length" > <foreach collection= "array" open= " and stuno in (" close= ")" item= "stuNo" separator= "," > #{stuNo} ****
1.8.4 集合****
无论编写代码时,传递的是什么参数名(stuNos),在mapper.xml中 必须用list代替该数组****
<select id= "queryStudentsWithList" parameterType= "list" resultType= "student" > select * from student <if test= "list!=null and list.size>0" > <foreach collection= "list" open= " and stuno in (" close= ")" item= "stuNo" separator= "," > #{stuNo} ****
1.8.5 对象数组****
Student[] students = {student0,student1,student2} 每个studentx包含一个学号属性****
<select id= "queryStudentsWithObjectArray" parameterType= "Object[]" resultType= "student" > select * from student <!--如果sql片段和 引用处不在同一个文件中,则需要 在refid 引用时 加上namespace: namespace.id --> <include refid= "objectArrayStunos" >
注意的几点:****
parameterType="Object[]"
********<foreach collection="array" open=" and stuno in (" close=")"
************************item="student" separator=",">
************************#{student.stuNo}
********
1.8.6 SQL片段****
a.提取相似代码****
<sql id= "objectArrayStunos" > <if test= "array!=null and array.length>0" > <foreach collection= "array" open= " and stuno in (" close= ")" item= "student" separator= "," > #{student.stuNo} ****
b.引用****
<select id= "queryStudentsWithObjectArray" parameterType= "Object[]" resultType= "student" > select * from student <!--如果sql片段和 引用处不在同一个文件中,则需要 在refid 引用时 加上namespace: namespace.id --> <include refid= "objectArrayStunos" > ****
**1.9 一对一、一对多关联查询****** 复制代码
(MyBatis:多对一,多对多的本质就是 一对多的变化)****
1.9.1 一对一(两种方法)****
1.业务扩展类****
核心:用resultType指定类的属性 包含 多表查询的所有字段****
例如:学生(Student)和学生证(StudentCart)****
(1)重写业务扩展类(StudentBusiness)****
****
(2)配置mapper映射文件****
****
(3)接口和实现****
接口****
StudentBusiness queryStudentByNoWithOO(int stuno);****
实现方法:****
//可以通过build的第二参数 指定数据库环境
SqlSessionFactory sessionFacotry = new SqlSessionFactoryBuilder().build(reader,"development") ; SqlSession session = sessionFacotry.openSession() ; StudentMapper studentMapper = session.getMapper(StudentMapper.class) ; StudentBusiness studentBusiness = studentMapper.queryStudentByNoWithOO(2) ;//接口中的方法->SQL语句 System.out.println(studentBusiness);****
2.resultMap****
(1)在对象中添加成员属性****
****
(2)使用mapper映射文件中的resultMap****
(4)接口和实现
Student queryStudentByNoWithOO2(int stuNo); SqlSession session = sessionFacotry.openSession(); StudentMapper studentMapper = session.getMapper(StudentMapper.class); Student student = studentMapper.queryStudentByNoWithOO2(2);// 接口中的方法->SQL语句 System.out.println(student);****
1.9.2 一对多****
一对一:association**** 一对多:collection**** 表:student studentclass (关联:classid) 类:student studentClass (关联:List students ) select c.,s. from student s inner join studentclass c on c.classid = s.classid where c.classid = 1;
例:一个年级包含多个学生****
(1)关联属性****
(2)配置映射文件(mapper.xml)****
****
(3)接口和方法实现****
StudentClass queryClassAndStudents(int classId);StudentMapper StudentMapper studentMapper = session.getMapper(StudentMapper.class); // 班级 StudentClass studentClass = studentMapper.queryClassAndStudents(1); // System.out.println(studentClass); System.out.println(studentClass.getClassId() + "," + studentClass.getClassName()); // 班级对应的学生 List students = studentClass.getStudents(); for (Student student : students) { System.out.println(student.getStuNo() + "," + student.getStuName() + "," + student.getStuAge()); }