鉴别器

Posted erbinok

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鉴别器相关的知识,希望对你有一定的参考价值。

1.鉴别器 * 查询汽车表,根据类型封装成不同的对象

创建公共交通类作为基类

技术分享图片
package com.zixue.entity;
/**
 * 交通工具实体类 ,封装汽车表中公用的字段
 * */
public class Vehicle {

    private int id;
    private String type;
    private String color;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    
}
Vehicle.java
技术分享图片
package com.zixue.entity;
/**
 * 小汽车实体类 封装小汽车相关字段
 * */
public class Car extends Vehicle{

    private int doorSize;

    public int getDoorSize() {
        return doorSize;
    }

    public void setDoorSize(int doorSize) {
        this.doorSize = doorSize;
    }
    
}
Car
技术分享图片
package com.zixue.entity;
/**
 * 卡车实体类,封装卡车相关的字段
 * */
public class Truck extends Vehicle{

    public int boxSize;

    public int getBoxSize() {
        return boxSize;
    }

    public void setBoxSize(int boxSize) {
        this.boxSize = boxSize;
    }
    
}
Truck.java
技术分享图片
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.zixue.dao.VehicleDao">
<resultMap type="com.zixue.entity.Vehicle" id="vehicleMap">
<id property="id" column="id"/>
<result property="color" column="color"/>
<discriminator javaType="java.lang.String" column="type">
    <case value="T" resultType="com.zixue.entity.Truck">
        <result property="boxSize" column="boxSize"/>
    </case>
    <case value="C" resultType="com.zixue.entity.Car">
        <result property="doorSize" column="doorSize"/>
    </case>
</discriminator>
</resultMap>
<select id="findAll" resultMap="vehicleMap">
select * from t_car
</select>
</mapper>
VehicleMapper.xml
技术分享图片
package com.zixue.dao;

import java.util.List;

import com.zixue.annotation.MyBatisRepository;
import com.zixue.entity.Vehicle;
@MyBatisRepository
public interface VehicleDao {

    List<Vehicle> findAll();
}
VehicleDao
技术分享图片
package com.zixue.test;

import java.util.List;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zixue.dao.VehicleDao;
import com.zixue.entity.Vehicle;

public class TestFindAll {
    /**
     * 鉴别器
     * 查询汽车表,根据类型封装成不同的对象
     * */
    @Test
    public void testFindAll(){
        
        ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml");
        VehicleDao dao =ac.getBean(VehicleDao.class);
        List<Vehicle> list =dao.findAll();
        for( Vehicle v:list){
            
            System.out.println(v);
        }
    }
}
TestFindVehicle

 

以上是关于鉴别器的主要内容,如果未能解决你的问题,请参考以下文章

未知的鉴别器值“SqlException”,如何忽略动态类型的鉴别器

Hibernate:当没有物理列或值用于区分实例时,鉴别器列如何工作?

discriminator 鉴别器

鉴别器图中缺少的实体

JPA 鉴别器类型性能

未找到鉴别器键时的 Mongoid 行为