mybatis_SQL映射(4)鉴别器

时间:2023-11-24 22:43:20

摘录自:http://blog.csdn.net/y172158950/article/details/17505739

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

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. }
  1. <p>package com.yjq.entity;</p><p>public class Car extends Vehicle {
  2. private int doorCount;</p><p>   public Car() {
  3. }</p><p>    public int getDoorCount() {
  4. return doorCount;
  5. }</p><p>    public void setDoorCount(int doorCount) {
  6. this.doorCount = doorCount;
  7. }
  8. }
  9. </p>
  1. package com.yjq.entity;
  2. public class Boat extends Vehicle {
  3. private String quant;  //船桨
  4. public Boat() {
  5. }
  6. public String getQuant() {
  7. return quant;
  8. }
  9. public void setQuant(String quant) {
  10. this.quant = quant;
  11. }
  12. }

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. 表中的数据

mybatis_SQL映射(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]