Mybatis 鉴别器

来源:互联网 发布:ios比价软件app 编辑:程序博客网 时间:2024/05/17 04:05


使用鉴别器,根据表中某个字段区别数据,将查询出的数据自动封装成不同类型的对象。

步骤

实现此案例需要按照如下步骤进行。

步骤一:建表

创建汽车表,并插入预置数据,脚本如下:

  1. CREATE TABLE t_car (
  2. id NUMBER(11) NOT NULL,
  3. type VARCHAR2(1) default NULL,
  4. doorSize NUMBER(11) default NULL,
  5. boxSize NUMBER(11) default NULL,
  6. color VARCHAR2(20) default NULL,
  7. PRIMARY KEY (id)
  8. );
  9. insert into t_car values (1,'C',2,null,'红色');
  10. insert into t_car values (2,'C',4,null,'黑色');
  11. insert into t_car values (3,'T',null,1,'蓝色');
  12. insert into t_car values (4,'T',null,2,'蓝色');
  13. commit;

步骤二:创建实体类

创建交通工具实体类Vehicle,封装汽车表中的公用字段,代码如下:

  1. package cn.edu.tju.entity;
  2. /**
  3. *    交通工具实体类,封装汽车表中公用的字段
  4. */
  5. public class Vehicle {
  6.     private int id;
  7.     private String type;
  8.     private String color;
  9.     public int getId() {
  10.         return id;
  11.     }
  12.     public void setId(int id) {
  13.         this.id = id;
  14.     }
  15.     public String getType() {
  16.         return type;
  17.     }
  18.     public void setType(String type) {
  19.         this.type = type;
  20.     }
  21.     public String getColor() {
  22.         return color;
  23.     }
  24.     public void setColor(String color) {
  25.         this.color = color;
  26.     }
  27. }

创建小汽车实体类,继承于Vehicle,并封装小汽车相关的字段,代码如下:

  1. package cn.edu.tju.entity;
  2. /**
  3. *    小汽车实体类,封装小汽车相关字段
  4. */
  5. public class Car extends Vehicle {
  6.     private int doorSize;
  7.     public int getDoorSize() {
  8.         return doorSize;
  9.     }
  10.     public void setDoorSize(int doorSize) {
  11.         this.doorSize = doorSize;
  12.     }
  13. }

创建卡车实体类,继承于Vehicle,并封装卡车相关的字段,代码如下:

  1. package cn.edu.tju.entity;
  2. /**
  3. *    卡车实体类,封装卡车相关的字段
  4. */
  5. public class Truck extends Vehicle {
  6.     private int boxSize;
  7.     public int getBoxSize() {
  8.         return boxSize;
  9.     }
  10.     public void setBoxSize(int boxSize) {
  11.         this.boxSize = boxSize;
  12.     }
  13. }

步骤三:创建DAO接口

创建DAO接口VehicleDao,并增加查询全部汽车数据的方法,代码如下:

  1. package cn.edu.tju.dao;
  2. import java.util.List;
  3. import cn.edu.tju.annotation.MyBatisRepository;
  4. import cn.edu.tju.entity.Vehicle;
  5. @MyBatisRepository
  6. public interface VehicleDao {
  7.     List<Vehicle> findAll();
  8. }

步骤四:创建映射文件

创建映射文件VehicleMapper.xml,并增加SQL实现查询全部汽车,代码如下:

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
  3. "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
  4. <mapper namespace="com.tarena.dao.VehicleDao">
  5.     <resultMap id="vehicleMap" type="c cn.edu.tju.entity.Vehicle">
  6.         <id property="id" column="ID" />
  7.         <result property="color" column="COLOR" />
  8.         <discriminator javaType="java.lang.String" column="TYPE">
  9.             <case value="T" resultType=" cn.edu.tju.entity.Truck">
  10.                 <result property="boxSize" column="BOXSIZE" />
  11.             </case>
  12.             <case value="C" resultType="cn.edu.tju.entity.Car">
  13.                 <result property="doorSize" column="DOORSIZE" />
  14.             </case>
  15.         </discriminator>
  16.     </resultMap>
  17.     <select id="findAll" resultMap="vehicleMap">
  18.         select * from T_CAR
  19.     </select>
  20. </mapper>

步骤五:增加测试方法

在TestMapping中增加方法,测试查询全部汽车的方法,代码如下:

  1. package cn.edu.tju..test;
  2. import java.sql.Date;
  3. import java.util.List;
  4. import org.junit.Test;
  5. import org.springframework.context.ApplicationContext;
  6. import org.springframework.context.support.ClassPathXmlApplicationContext;
  7. import cn.edu.tju.daoDeptDao;
  8. import cn.edu.tju.dao.EmpDao;
  9. import cn.edu.tju.VehicleDao;
  10. import cn.edu.tju.entity.Dept;
  11. import cn.edu.tju.entity.Emp;
  12. import cn.edu.tju.entity.Vehicle;
  13. /**
  14. *    测试MyBatis关联映射
  15. */
  16. public class TestMapping {
  17.     
  18.     //其他方法略
  19.     /**
  20.      * 鉴别器:
  21.      * 查询汽车表,根据类型封装成不同的对象。
  22.      */
  23.     @Test
  24.     public void test6() {
  25.         ApplicationContext ctx = new ClassPathXmlApplicationContext(
  26.                 "applicationContext.xml");
  27.         VehicleDao dao = ctx.getBean(VehicleDao.class);
  28.         List<Vehicle> list = dao.findAll();
  29.         for (Vehicle v : list) {
  30.             System.out.println(v);
  31.         }
  32.     }
  33.     
  34. }
1 0
原创粉丝点击