IBatis: Discriminator Column Example – Inheritance Mapping(Ibatis中的继承映射)

继承映射介绍-iBATIS supports inheritance hierarchies by using a special mapping called a discriminator. Using a discriminator you can determine the type of class to be instantiated based on a value in the database. The discriminator is a part of the ResultMap and works much like a switch statement.

 

问题:对于Manager和Developer来说,他们都是Employee类的子类,且都有自己特殊的属性,Manager有managerId和info属性,developer有developerId和project属性。在使用IBATIS查询一批Employee类时,如何通过自动映射通过判断Employee_type取得他们各自的属性?

 

 1、pojo beans

2、sqlMap

<sqlMap namespace="Employee">
	<typeAlias alias="Employee" type="model.Employee" />



	<resultMap id="resultEmployee" class="Employee">
		<result property="id" column="id" />
		<result property="name" column="name" />
		<discriminator javaType="int" column="employee_type">
			<subMap value="1" resultMap="managerMap" />
			<subMap value="2" resultMap="developerMap" />
		</discriminator>
<!-- 如果employee_type的值是1,则对应managerMap的映射。如果是2,则对应developerMap的映射。-->
	</resultMap>

	<resultMap id="managerMap" class="model.Manager" extends="resultEmployee">
		<result property="managerId" column="manager_id" />
		<result property="info" column="info" />
	</resultMap>

	<resultMap id="developerMap" class="model.Developer" extends="resultEmployee">
		<result property="developerId" column="developer_id" />
		<result property="project" column="product" />
	</resultMap>

	<select id="getAllEmployees" resultMap="resultEmployee">
		SELECT id, name ,employee_type, manager_id, info, developer_id,
		product FROM employee E left join manager M on M.employee_id =
		E.id left join developer D on D.employee_id = E.id
	</select>

</sqlMap>

 3、test code

public class TestSqlMap {
  private static SqlMapClient sqlMapClient = null;
  @Before
  public void loadFile() {
    String resource = "sqlMap/sql-map-config.xml";
    try {
      Reader reader = Resources.getResourceAsReader(resource);

      sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
      reader.close();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
  
  @Test
  public void testSql(){
    try {
      List<Employee> list =  sqlMapClient.queryForList("getAllEmployees",null);
    
      
      for (Employee employee : list){
        System.out.println(employee.toString());
      }
      
      assertEquals(5, list.size());
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

 

4、test result:

Employee ID = 1 Name = Kate Manager ID = 1 Info = info Kate
Employee ID = 4 Name = James Manager ID = 2 Info = info James
Employee ID = 5 Name = Susan Developer ID = 3 Project = web
Employee ID = 2 Name = Josh Developer ID = 1 Project = web
Employee ID = 3 Name = Peter Developer ID = 2 Project = desktop

修改sqlMap,去掉managerMap的extends="resultEmployee"和developerMap的extends="resultEmployee" :

Employee ID = 0 Name = null Manager ID = 1 Info = info Kate
Employee ID = 0 Name = null Manager ID = 2 Info = info James
Employee ID = 0 Name = null Developer ID = 3 Project = web
Employee ID = 0 Name = null Developer ID = 1 Project = web
Employee ID = 0 Name = null Developer ID = 2 Project = desktop

5、在以上的例子中,子映射(sub map)对应这标准的结果映射,如果discriminator 找到了一个值去匹配subMap ,则父结果集的映射将失效,参加第四点的修改后的输出,除非子映射明确的扩展了父映射(extends)。

如果discriminator 找不到值去匹配subMap,则使用父结果映射:

Employee ID = 1 Name = Kate
Employee ID = 4 Name = James
Employee ID = 5 Name = Susan
Employee ID = 2 Name = Josh
Employee ID = 3 Name = Peter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值