discriminator或者叫做分类器
Vehicle类
package io.github.coinsjack.pojo;
import java.util.Date;
public class Vehicle {
protected Integer id; protected String vin; protected Date year; protected String make; protected String model; protected String color;
public Vehicle() { }
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
}
|
Car继承自Vehicle
package io.github.coinsjack.pojo;
public class Car extends Vehicle {
private Integer doorCount;
public Car() { }
public Integer getDoorCount() { return doorCount; }
public void setDoorCount(Integer doorCount) { this.doorCount = doorCount; }
@Override public String toString() { return "Car{" + "doorCount=" + doorCount + ", id=" + id + ", vin='" + vin + '\'' + ", year=" + year + ", make='" + make + '\'' + ", model='" + model + '\'' + ", color='" + color + '\'' + '}'; } }
|
Suv继承自Vehicle
package io.github.coinsjack.pojo;
public class Suv extends Vehicle{
private Boolean allWheelDriveFlag;
public Suv() { } public Boolean getAllWheelDriveFlag() { return allWheelDriveFlag; } public void setAllWheelDriveFlag(Boolean allWheelDriveFlag) { this.allWheelDriveFlag = allWheelDriveFlag; } @Override public String toString() { return "Suv{" + "allWheelDriveFlag=" + allWheelDriveFlag + ", id=" + id + ", vin='" + vin + '\'' + ", year=" + year + ", make='" + make + '\'' + ", model='" + model + '\'' + ", color='" + color + '\'' + '}'; } }
|
数据库表字段
id int(11) NO PRI auto_increment vin varchar(50) YES year year(4) YES make varchar(50) YES model varchar(20) YES color varchar(20) YES vehicle_type int(11) YES door_count int(11) YES all_wheel_drive tinyint(1) YES |
通过vehicle_type判断是Car还是Suv, 就需要用到discriminator
VehicleMapper映射文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.github.coinsjack.dao.VehicleMapper">
<resultMap id="vehicleResultMap" type="Vehicle"> <id column="id" property="id"/> <id column="vin" property="vin"/> <id column="year" property="year"/> <id column="make" property="make"/> <id column="model" property="model"/> <id column="color" property="color"/> <discriminator javaType="int" column="vehicle_type"> <case value="1" resultType="Car"> <result column="door_count" property="doorCount"/> </case> <case value="2" resultType="Suv"> <result column="all_wheel_drive" property="allWheelDriveFlag"/> </case> </discriminator> </resultMap>
<select id="getVehicleById" resultMap="vehicleResultMap"> select * from tb_vehicle WHERE `id` = #{id}; </select> </mapper>
|
测试
@Test public void testGetVehicleById() { SqlSession sqlSession = MyBatisUtil.getSqlSession(); VehicleMapper vehicleMapper= sqlSession.getMapper(VehicleMapper.class);
System.out.printf("查询结果: %s%n", vehicleMapper.getVehicleById(5));; System.out.printf("查询结果: %s%n", vehicleMapper.getVehicleById(6));; }
|
测试结果
2018-12-29 17:38:29,567 [main] DEBUG [io.github.coinsjack.dao.VehicleMapper.getVehicleById] - ==> Preparing: select * from tb_vehicle WHERE `id` = ?; 2018-12-29 17:38:29,677 [main] DEBUG [io.github.coinsjack.dao.VehicleMapper.getVehicleById] - ==> Parameters: 5(Integer) 2018-12-29 17:38:29,750 [main] DEBUG [io.github.coinsjack.dao.VehicleMapper.getVehicleById] - <== Total: 1 查询结果: Car{doorCount=4, id=5, vin='2627159999', year=Mon Jan 01 08:00:00 CST 2018, make='IDEA', model='DA-20', color='Black'} 2018-12-29 17:38:29,762 [main] DEBUG [io.github.coinsjack.dao.VehicleMapper.getVehicleById] - ==> Preparing: select * from tb_vehicle WHERE `id` = ?; 2018-12-29 17:38:29,763 [main] DEBUG [io.github.coinsjack.dao.VehicleMapper.getVehicleById] - ==> Parameters: 6(Integer) 2018-12-29 17:38:29,804 [main] DEBUG [io.github.coinsjack.dao.VehicleMapper.getVehicleById] - <== Total: 1 查询结果: Suv{allWheelDriveFlag=false, id=6, vin='2627159998', year=Mon Jan 01 08:00:00 CST 2018, make='IDEA', model='AA-19', color='Pink'}
|
标签:coinsjack,29,鉴别器,public,year,Mybatis,discriminator,VehicleMapper,id
来源: https://www.cnblogs.com/litran/p/10546259.html