鑒別器(discriminator)是MyBatis為我們提供的第三個級聯也是最后一個。基於之前兩篇級聯中的場景,現增加學生們去體檢,但男女體檢項目不一樣,我們把男女體檢表做成兩張表,當然我想也可以設計為一張表,只有女生的項目男生不填就行了,為了講解鑒別器就把男女體檢表分開。鑒別器的作用在這里就是根據性別的不同去不同的表里進行查詢體檢情況,例如是男生就在男生體檢表里查詢,是女生就在女生體檢表里查詢。
POJO類我們也分為了男生、女生,他們分別繼承之前的Student類。
MaleStudent類:
1 package day_8_mybatis.pojo; 2 3 import java.util.List; 4 5 /** 6 * @author turbo 7 * 8 * 2016年11月6日 9 */ 10 public class MaleStudent extends Student { 11 List<MaleStudentHealth> studentHealthList; 12 //省略getter/setter方法 13 }
一個學生和他體檢表的對應關系應該是一對一的關系,為什么在這里是一對多的關系呢?呃……這是因為在體檢表的設計中有一個日期的字段,也就是說一個學生在不同時間的體檢情況都有記錄,所以學生和體檢表的對應關系就是一對多的關系,在這里也就是一個List的引用。
FemaleStudent類:
1 package day_8_mybatis.pojo; 2 3 import java.util.List; 4 5 /** 6 * @author turbo 7 * 8 * 2016年11月6日 9 */ 10 public class FemaleStudent extends Student{ 11 List<FemaleStudentHealth> studentHealthList; 12 //省略getter/setter方法 13 }
這里的List的引用道理同上。
現在看看體檢表的POJO類。
MaleStudentHealth類:
package day_8_mybatis.pojo; /** * @author turbo * * 2016年11月6日 */ public class MaleStudentHealth { private int id; private int studentId; private String date; private String prostate; //前列腺 //省略setter/getter方法 }
FemaleStudentHealth類:
1 package day_8_mybatis.pojo; 2 3 /** 4 * @author turbo 5 * 6 * 2016年11月6日 7 */ 8 public class FemaleStudentHealth { 9 private int id; 10 private int studentId; 11 private String date; 12 private String womb; 13 //省略setter/getter方法 14 }
基本的數據結構設計就是上面所貼出來的代碼了。下面我們看看mapper映射器,對於體檢情況的查詢不管男生女生都是通過student_id來查詢的。
查詢根據男生的student_id查詢該生的體檢表:
1 package day_8_mybatis.mapper; 2 3 import day_8_mybatis.pojo.MaleStudentHealth; 4 5 /** 6 * @author turbo 7 * 8 * 2016年11月6日 9 */ 10 public interface MaleStudentHealthMapper { 11 MaleStudentHealth findMaleStudentHealthByStudentId(int id); 12 }
其對應的MaleStudentHealthMapper.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <mapper namespace="day_8_mybatis.mapper.MaleStudentHealthMapper"> 7 <select id="findMaleStudentHealthByStudentId" parameterType="int" resultType="day_8_mybatis.pojo.MaleStudentHealth"> 8 select id, student_id as studentId, date, prostate from t_male_student_health where student_id = #{id} 9 </select> 10 </mapper>
查詢根據女生的student_id查詢該生的體檢表:
1 package day_8_mybatis.mapper; 2 3 import day_8_mybatis.pojo.FemaleStudentHealth; 4 5 /** 6 * @author turbo 7 * 8 * 2016年11月6日 9 */ 10 public interface FemaleStudentHealthMapper { 11 FemaleStudentHealth findFemaleStudentHealthByStudentById(int id); 12 }
其對應的FemaleStudentHealthMapper.xml:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="day_8_mybatis.mapper.FemaleStudentHealthMapper"> 6 <select id="findFemaleStudentHealthByStudentById" parameterType="int" resultType="day_8_mybatis.pojo.FemaleStudentHealth"> 7 select id, student_id as studentId, date, womb from t_female_student_health where student_id = #{id} 8 </select> 9 </mapper>
基本工作已經做完了,剩下就是在StudentMapper.xml利用鑒別器來查詢不同的體檢表。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="day_8_mybatis.mapper.StudentMapper"> 6 <resultMap type="day_8_mybatis.pojo.Student" id="studentMap"> 7 <id property="id" column="id"/> 8 <result property="name" column="name"/> 9 <result property="sex" column="sex"/> 10 <association property="selfCard" column="id" select="day_8_mybatis.mapper.SelfCardMapper.findSelfCardByStudentId"/> 11 <collection property="courseScoreList" column="id" select="day_8_mybatis.mapper.CourseScoreMapper.findCourseScoreByStudentId" /> 12 <discriminator javaType="string" column="sex"> 13 <case value="男" resultMap="maleStudentMap"/> 14 <case value="女" resultMap="femaleStudentMap"/> 15 </discriminator> 16 </resultMap> 17 18 <select id="getStudent" parameterType="int" resultMap="studentMap"> 19 select id, name, sex from t_student where id = #{id} 20 </select> 21 22 <resultMap id="maleStudentMap" type="day_8_mybatis.pojo.MaleStudent" extends="studentMap"> 23 <collection property="studentHealthList" select="day_8_mybatis.mapper.MaleStudentHealthMapper.findMaleStudentHealthByStudentId" column="id" /> 24 </resultMap> 25 26 <resultMap id="femaleStudentMap" type="day_8_mybatis.pojo.FemaleStudent" extends="studentMap"> 27 <collection property="studentHealthList" select="day_8_mybatis.mapper.FemaleStudentHealthMapper.findFemaleStudentHealthByStudentById" column="id" /> 28 </resultMap> 29 </mapper>
第12-15行就是本節的要講的discriminator鑒別器,它通過查詢出來的學生性別選擇不同的體檢表來查詢出體檢情況。
最后稍微修改的測試類,即可測試結果。
1 package day_8_mybatis; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 9 import day_8_mybatis.mapper.StudentMapper; 10 import day_8_mybatis.pojo.MaleStudent; 11 import day_8_mybatis.util.SessionFactory2; 12 13 /** 14 * 客戶端 15 * @author turbo 16 * 17 * 2016年11月6日 18 */ 19 public class Main { 20 21 /** 22 * @param args 23 * @throws IOException 24 */ 25 public static void main(String[] args) throws Exception { 26 String resource = "day_8_mybatis/mybatis-config.xml"; //獲取mybatis配置文件路徑 27 InputStream inputStream = Resources.getResourceAsStream(resource); 28 SqlSession sqlSession = SessionFactory2.getInstance(inputStream).openSession(); 29 StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class); 30 MaleStudent student =(MaleStudent)studentMapper.getStudent(1); 31 System.out.println("學生:" + student.getName() + " 課程:" + student.getCourseScoreList().get(0).getCourse().getCourseName() + " 分數:" + student.getCourseScoreList().get(0).getScore()+ " 性別:" + student.getSex() + " 前列腺:" + student.getStudentHealthList().get(0).getProstate()); 32 33 } 34 35 }
別忘了把新增加的mapper映射注冊到mybatis-config.xml配置文件中。