关联查询:
一对一:
两种方式实现:
1.通过业务扩展的方式进行一对一查询,新建一个实体类,继承其中属性多的一个,然后写上另一个类中的属性:
实体类:
映射文件:
1 <!-- 业务扩展的方式进行一对一查询,创建一个新的实体类继承属性多的一方然后重写属性少的一方 --> 2 <select id="queryStudentWithOO" parameterType="int" resultType="StudentBusiness"> 3 select * from student s inner join studentcard c on s.sid = c.cid and sid = #{sid} 4 </select>
测试类:
1 //一对一查寻 2 public static void queryStudentWithOO() throws IOException { 3 //读取配置文件 4 Reader reader = Resources.getResourceAsReader("conf.xml"); 5 //创建sqlSessionFactory 6 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 7 //获取session 8 SqlSession session = sessionFactory.openSession(); 9 //定位映射文件 10 studentMapper studentMapper = session.getMapper(studentMapper.class); 11 //定位sql语句并执行 12 StudentBusiness studentBusiness = studentMapper.queryStudentWithOO(3); 13 //提交事务 14 session.commit(); 15 System.out.println(studentBusiness); 16 //关闭连接 17 session.close(); 18 }
2.通过ResultMap的方式实现一对一
然后在映射文件中使用association进行对象属性的映射:
实体类:
配置文件:
1 <select id="queryStudentWithMap" parameterType="int" resultMap="StudentBusiness1"> 2 select * from student s inner join studentcard c on s.sid = c.cid and sid = #{sid} 3 </select> 4 <resultMap type="StudentBusiness" id="StudentBusiness1"> 5 <id property="sid" column="sid"/> 6 <result property="sname" column="sname"/> 7 <result property="age" column="age"/> 8 <result property="sex" column="sex"/> 9 <result property="cid" column="cid"/> 10 <result property="stuInfo" column="stuInfo"/> 11 <association property="address" javaType="Address"> 12 <result property="homeAddress" column="homeaddress"/> 13 <result property="schoolAddress" column="schooladdress"/> 14 </association> 15 </resultMap>
测试类:
1 //一对一查寻,使用ResultMap的方式 2 public static void queryStudentWithMap() throws IOException { 3 //读取配置文件 4 Reader reader = Resources.getResourceAsReader("conf.xml"); 5 //创建sqlSessionFactory 6 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 7 //获取session 8 SqlSession session = sessionFactory.openSession(); 9 //定位映射文件 10 studentMapper studentMapper = session.getMapper(studentMapper.class); 11 //定位sql语句并执行 12 StudentBusiness studentBusiness = studentMapper.queryStudentWithMap(3); 13 //提交事务 14 session.commit(); 15 System.out.println(studentBusiness); 16 //关闭连接 17 session.close(); 18 }
2.一对多:
一个班级对应多个学生,应在班级的实体类中定义一个学生的属性,然后在映射文件中使用collection进行对象属性集合进行映射:
实体类:
配置文件:
1 <!-- 一对多查询 --> 2 <select id="queryStudentWithOM" parameterType="int" resultMap="Studentclass1"> 3 select * from student s inner join studentclass c on s.cno = c.cno and c.cno = #{cno} 4 </select> 5 6 <resultMap type="StudentClass" id="Studentclass1"> 7 <id property="cno" column="cno"/> 8 <result property="cname" column="cname"/> 9 <!-- 循环集合用collection,属性值写集合属性名,类型写集合中的元素的类型并且使用的是ofType --> 10 <collection property="students" ofType="student"> 11 <result property="sname" column="sname"/> 12 <result property="age" column="age"/> 13 <result property="sex" column="sex"/> 14 </collection> 15 </resultMap>
测试类:
1 //一对多查寻 2 public static void queryStudentWithOM() throws IOException { 3 //读取配置文件 4 Reader reader = Resources.getResourceAsReader("conf.xml"); 5 //创建sqlSessionFactory 6 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); 7 //获取session 8 SqlSession session = sessionFactory.openSession(); 9 //定位映射文件 10 studentMapper studentMapper = session.getMapper(studentMapper.class); 11 //定位sql语句并执行 12 StudentClass studentClass = studentMapper.queryStudentWithOM(2); 13 System.out.println(studentClass); 14 //提交事务 15 session.commit(); 16 List<Student> students = studentClass.getStudents(); 17 for (Student student : students) { 18 System.out.print(studentClass.getCno() + "," + studentClass.getCname() + ","); 19 System.out.println(student.getSname()+"," +student.getAge()+"," +student.getSex()); 20 } 21 //关闭连接 22 session.close(); 23 }