mybatis鉴别器(类之间关系为多态时可以使用)

鉴别器:有时一个单独的数据库查询也许返回很多不同(但是希望有些关联)数据类型的结果集。鉴别器元素就是被设计来处理这个情况的,还有包括类的继承层次结构。[抄了一个定义,不是很理解,还是看例子吧]

1. 交通工具表vehicle

  1. create table test.vehicle (
  2. id bigint(10) primary key AUTO_INCREMENT,
  3. vin varchar(10),
  4. year date,
  5. color varchar(10),
  6. vendor varchar(10),
  7. vehicle_type int, //类型:1表示car, 2表示boat
  8. door_count int, //车门数量,car独有属性
  9. quant varchar(10) //船桨,boat独有属性
  10. );

2. java对应的实体类Vehicle,Car,Boat

  1. package com.yjq.entity;
  2. import java.sql.Date;
  3. public class Vehicle {
  4. private int id;
  5. private String vin; //交通登记号码
  6. private Date year;
  7. private String color;
  8. private String vendor;
  9. private int vehicleType;
  10. public Vehicle() {
  11. }
  12. public int getId() {
  13. return id;
  14. }
  15. public void setId(int id) {
  16. this.id = id;
  17. }
  18. public String getVin() {
  19. return vin;
  20. }
  21. public void setVin(String vin) {
  22. this.vin = vin;
  23. }
  24. public Date getYear() {
  25. return year;
  26. }
  27. public void setYear(Date year) {
  28. this.year = year;
  29. }
  30. public String getColor() {
  31. return color;
  32. }
  33. public void setColor(String color) {
  34. this.color = color;
  35. }
  36. public String getVendor() {
  37. return vendor;
  38. }
  39. public void setVendor(String vendor) {
  40. this.vendor = vendor;
  41. }
  42. public int getVehicleType() {
  43. return vehicleType;
  44. }
  45. public void setVehicleType(int vehicleType) {
  46. this.vehicleType = vehicleType;
  47. }
  48. }

package com.yjq.entity;

public class Car extends Vehicle {   private int doorCount;

public Car() { }

public int getDoorCount() {   return doorCount; }

public void setDoorCount(int doorCount) {   this.doorCount = doorCount; }   }

[java]  view plain  copy
  1. <code class="language-java">package com.yjq.entity;  
  2.   
  3. public class Boat extends Vehicle {  
  4.       
  5.     private String quant;  //船桨  
  6.   
  7.     public Boat() {  
  8.     }  
  9.   
  10.     public String getQuant() {  
  11.         return quant;  
  12.     }  
  13.   
  14.     public void setQuant(String quant) {  
  15.         this.quant = quant;  
  16.     }  
  17.       
  18. }</code>  

3. 如何将查询结果映射为不同的对象呢?鉴别器登场

  1. <?xml version= "1.0" encoding= "UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace= "com.yjq.entity.Vehicle">
  4. <resultMap id= "vehicleResult" type= "Vehicle">
  5. <id property= "id" column= "id" />
  6. <result property= "vin" column= "vin"/>
  7. <result property= "year" column= "year"/>
  8. <result property= "vendor" column= "vendor"/>
  9. <result property= "color" column= "color"/>
  10. <result property= "vehicleType" column= "vehicle_type"/>
  11. <discriminator javaType= "int" column= "vehicle_type">
  12. < case value= "1" resultMap= "carResult"/>
  13. < case value= "2" resultMap= "boatResult"/>
  14. </discriminator>
  15. </resultMap>
  16. <resultMap id= "carResult" type= "Car">
  17. <result property= "vehicleType" column= "vehicle_type"/>
  18. <result property= "doorCount" column= "door_count" />
  19. </resultMap>
  20. <resultMap id= "boatResult" type= "Boat">
  21. <result property= "vehicleType" column= "vehicle_type"/>
  22. <result property= "quant" column= "quant" />
  23. </resultMap>
  24. <select id= "selectVehicle" parameterType= "int" resultMap= "vehicleResult">
  25. select * from vehicle where id =#{id};
  26. </select>
  27. </mapper>

4. 表中的数据

5. dao代码,看看查询效果

  1. package com.yjq.dao;
  2. import org.apache.ibatis.session.SqlSession;
  3. import com.yjq.db.DbFactory;
  4. import com.yjq.entity.Boat;
  5. import com.yjq.entity.Car;
  6. import com.yjq.entity.Vehicle;
  7. public class VehicleDao {
  8. public Vehicle selectVehicleById(int id) {
  9. SqlSession session = DbFactory.getInstance().openSession();
  10. Vehicle vehicle = (Vehicle) session.selectOne( "com.yjq.entity.Vehicle.selectVehicle", id);
  11. session.commit();
  12. session.close();
  13. return vehicle;
  14. }
  15. public static void print(Vehicle v) {
  16. if(v instanceof Car) {
  17. Car c = (Car)v;
  18. System.out.println( "Car: [id=" + c.getId() + ", vehicleType="
  19. + c.getVehicleType() + ", doorCount=" + c.getDoorCount() + "]");
  20. } else if (v instanceof Boat) {
  21. Boat b = (Boat)v;
  22. System.out.println( "Boat: [id=" + b.getId() + ", vehicleType="
  23. + b.getVehicleType() + ", quant=" + b.getQuant() + "]");
  24. } else {
  25. System.out.println( "Vehicle: [id=" + v.getId() + ", vehicleType="
  26. + v.getVehicleType() + "]");
  27. }
  28. }
  29. public static void main(String[] args) {
  30. VehicleDao dao = new VehicleDao();
  31. Vehicle v1 = dao.selectVehicleById( 1);
  32. Vehicle v2 = dao.selectVehicleById( 2);
  33. Vehicle v3 = dao.selectVehicleById( 3);
  34. VehicleDao.print(v1);
  35. VehicleDao.print(v2);
  36. VehicleDao.print(v3);
  37. }
  38. }
  1. //output
  2. Car: [id= 1, vehicleType= 1, doorCount= 4]
  3. Boat: [id= 2, vehicleType= 2, quant=lxj]
  4. Vehicle: [id= 3, vehicleType= 3]

6. 蛋疼的地方

a) 重复配置:为了在Car,Boat,Vehicle类中能获取vehicleType属性,<result property="vehicleType" column="vehicle_type"/>配置了三次。

b) 多个resultMap:

i. 将查询语句的resultMap修改下,再看看输出:将所有的查询结果强行映射为Car了

  1. <select id= "selectVehicle" parameterType= "int" resultMap= "carResult">
  2. select * from vehicle where id =#{id};
  3. </select>
  1. //output
  2. Car: [id= 1, vehicleType= 1, doorCount= 4]
  3. Car: [id= 2, vehicleType= 2, doorCount= 0]
  4. Car: [id= 3, vehicleType= 3, doorCount= 0]

7. 鉴别器的另外一种写法

  1. <resultMap id= "vehicleResult" type= "Vehicle">
  2. <id property= "id" column= "id" />
  3. <result property= "vin" column= "vin"/>
  4. <result property= "year" column= "year"/>
  5. <result property= "vendor" column= "vendor"/>
  6. <result property= "color" column= "color"/>
  7. <result property= "vehicleType" column= "vehicle_type"/>
  8. <discriminator javaType= "int" column= "vehicle_type">
  9. < case value= "1" resultType= "Car">
  10. <result property= "doorCount" column= "door_count" />
  11. </ case>
  12. < case value= "2" resultType= "Boat" >
  13. <result property= "quant" column= "quant" />
  14. </ case>
  15. </discriminator>
  16. </resultMap>

猜你喜欢

转载自blog.csdn.net/qq_33315102/article/details/80870802
今日推荐