• mybatis_SQL映射(4)鉴别器


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

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

    1. 交通工具表vehicle

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.yjq.entity;  
    2.   
    3. import java.sql.Date;  
    4.   
    5. public class Vehicle {  
    6.       
    7.     private int id;  
    8.     private String vin;  //交通登记号码  
    9.     private Date year;  
    10.     private String color;  
    11.     private String vendor;  
    12.     private int vehicleType;  
    13.       
    14.     public Vehicle() {  
    15.     }  
    16.   
    17.     public int getId() {  
    18.         return id;  
    19.     }  
    20.   
    21.     public void setId(int id) {  
    22.         this.id = id;  
    23.     }  
    24.   
    25.     public String getVin() {  
    26.         return vin;  
    27.     }  
    28.   
    29.     public void setVin(String vin) {  
    30.         this.vin = vin;  
    31.     }  
    32.   
    33.     public Date getYear() {  
    34.         return year;  
    35.     }  
    36.   
    37.     public void setYear(Date year) {  
    38.         this.year = year;  
    39.     }  
    40.   
    41.     public String getColor() {  
    42.         return color;  
    43.     }  
    44.   
    45.     public void setColor(String color) {  
    46.         this.color = color;  
    47.     }  
    48.   
    49.     public String getVendor() {  
    50.         return vendor;  
    51.     }  
    52.   
    53.     public void setVendor(String vendor) {  
    54.         this.vendor = vendor;  
    55.     }  
    56.   
    57.     public int getVehicleType() {  
    58.         return vehicleType;  
    59.     }  
    60.   
    61.     public void setVehicleType(int vehicleType) {  
    62.         this.vehicleType = vehicleType;  
    63.     }  
    64.       
    65. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <p>package com.yjq.entity;</p><p>public class Car extends Vehicle {  
    2.    
    3.     private int doorCount;</p><p>   public Car() {  
    4.     }</p><p>    public int getDoorCount() {  
    5.        return doorCount;  
    6.     }</p><p>    public void setDoorCount(int doorCount) {  
    7.        this.doorCount = doorCount;  
    8.     }  
    9.    
    10. }  
    11. </p>  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. 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. }  

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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.       
    25.     <select id="selectVehicle" parameterType="int" resultMap="vehicleResult">  
    26.         select * from vehicle where id =#{id};  
    27.     </select>  
    28. </mapper>  

    4. 表中的数据

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

    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. package com.yjq.dao;  
    2.   
    3. import org.apache.ibatis.session.SqlSession;  
    4.   
    5. import com.yjq.db.DbFactory;  
    6. import com.yjq.entity.Boat;  
    7. import com.yjq.entity.Car;  
    8. import com.yjq.entity.Vehicle;  
    9.   
    10. public class VehicleDao {  
    11.       
    12.     public Vehicle selectVehicleById(int id) {  
    13.         SqlSession session = DbFactory.getInstance().openSession();  
    14.         Vehicle vehicle = (Vehicle) session.selectOne("com.yjq.entity.Vehicle.selectVehicle", id);  
    15.         session.commit();  
    16.         session.close();  
    17.         return vehicle;  
    18.     }  
    19.       
    20.     public static void print(Vehicle v) {  
    21.         if(v instanceof Car) {  
    22.             Car c = (Car)v;  
    23.             System.out.println("Car: [id=" + c.getId() + ", vehicleType="   
    24.                     + c.getVehicleType() + ", doorCount=" + c.getDoorCount() + "]");  
    25.         } else if (v instanceof Boat) {  
    26.             Boat b = (Boat)v;  
    27.             System.out.println("Boat: [id=" + b.getId() + ", vehicleType="  
    28.                     + b.getVehicleType() + ", quant=" + b.getQuant() + "]");  
    29.         } else {  
    30.             System.out.println("Vehicle: [id=" + v.getId() + ", vehicleType="  
    31.                     + v.getVehicleType() + "]");  
    32.         }  
    33.     }  
    34.       
    35.     public static void main(String[] args) {  
    36.         VehicleDao dao = new VehicleDao();  
    37.         Vehicle v1 = dao.selectVehicleById(1);  
    38.         Vehicle v2 = dao.selectVehicleById(2);  
    39.         Vehicle v3 = dao.selectVehicleById(3);  
    40.         VehicleDao.print(v1);  
    41.         VehicleDao.print(v2);  
    42.         VehicleDao.print(v3);  
    43.     }  
    44.       
    45. }  
    [java] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. //output  
    2. Car: [id=1, vehicleType=1, doorCount=4]  
    3. Boat: [id=2, vehicleType=2, quant=lxj]  
    4. Vehicle: [id=3, vehicleType=3]  
  • 相关阅读:
    C#中的编译为什么不叫Compile而叫build
    类型(Type)
    C#中关于值类型和引用类型的区别
    php通用化api格式输出
    tp6获取参数的五种办法
    PHP 数组------分割、合并
    explain结果中的type字段的含义
    order by 排序
    SQL开发技巧
    tp5模板输出日期时间
  • 原文地址:https://www.cnblogs.com/haimishasha/p/5710581.html
Copyright © 2020-2023  润新知