07.MyBatis中的關聯查詢

關聯查詢:java

一對一:sql

兩種方式實現:session

1.經過業務擴展的方式進行一對一查詢,新建一個實體類,繼承其中屬性多的一個,而後寫上另外一個類中的屬性:app

實體類:測試

 

 映射文件:ui

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>

測試類:spa

 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的方式實現一對一code

而後在映射文件中使用association進行對象屬性的映射:xml

實體類:對象

配置文件:

 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     }
相關文章
相關標籤/搜索