mybatis 继承映射
ORM框架的优势在于能让我们利用面向对象的思维去操作数据库,hibernate作为重量级的ORM框架对面向对象的支持很强大。作为半自动化的mybatis,对面向对象的支持也是很完备的。这篇文章就来讨论一下如何利用mybatis实现继承映射。
类图
有一个机动车父类,它有两个子类:Car和Bus
关系模型(t_vehicle)
ORM映射有一个原则:对象模型细粒度,关系模型粗粒度。所以我们将所有的车都存储一张表里(t_vehicle),通过鉴别字段vType来区分车的类型("c"代表Car,"b"代表Bus)
三个实体类的代码
Vehicle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com.tgb.mybatis.model; public class Vehicle { //主键id private String id; //车的名字 private String name; public String getId() { return id; } public void setId(String id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } } |
Car
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
package com.tgb.mybatis.model; public class Car extends Vehicle { //车门的数量 private int carDoor; //车的牌子 private String band; public int getCarDoor() { return carDoor; } public void setCarDoor( int carDoor) { this .carDoor = carDoor; } public String getBand() { return band; } public void setBand(String band) { this .band = band; } } |
Bus
1
2
3
4
5
6
7
8
9
10
11
12
|
package com.tgb.mybatis.model; public class Bus extends Vehicle { //公共汽车的容量 private int capacity; public int getCapacity() { return capacity; } public void setCapacity( int capacity) { this .capacity = capacity; } } |
看看对“车”进行操作的Mapper接口【只关注查询】
1
2
3
4
5
6
7
8
9
10
|
package com.tgb.mybatis.data; import com.tgb.mybatis.model.Bus; import com.tgb.mybatis.model.Car; import com.tgb.mybatis.model.Vehicle; public interface VehicleMapper { //根据id查询机动车 Vehicle getVechicleById(String id); //根据名字查询小汽车 Car getCarByName(String name); } |
xml方式ORM映射
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<? 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 = "com.tgb.mybatis.data.VehicleMapper" > < select id = "getVechicleById" resultMap = "vehicleMap" > SELECT * FROM TB_VEHICLE WHERE VID = #{id} </ select > < select id = "getCarByName" resultMap = "vehicleMap" > SELECT * FROM TB_VEHICLE WHERE VTYPE='c' AND VName = #{id} </ select > < resultMap type = "vehicle" id = "vehicleMap" > < id property = "id" column = "vId" /> < result property = "name" column = "vName" /> < discriminator javaType = "string" column = "vType" > < case value = "c" resultType = "car" > < result property = "carDoor" column = "cardoor" /> < result property = "band" column = "band" /> </ case > < case value = "b" resultType = "bus" > < result property = "capacity" column = "capacity" /> </ case > </ discriminator > </ resultMap > </ mapper > |
分析
其中最为关键的就是<discriminator>标签中的内容,根据鉴别字段的值自动映射成对应的子类
客户端测试代码
1
2
3
4
5
|
VehicleMapper mapper = session.getMapper(VehicleMapper. class ); Vehicle vehicle = mapper.getVechicleById( "1" ); System.out.println(vehicle.getName()); Car car = mapper.getCarByName( "路虎007" ); System.out.println(car.getBand()); |
很简单的一个例子,和大家分享一下。
mybatis xml映射文件的继承问题
1、首先dao层mapper.java需要继承原来的接口
原dao层接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public interface TagMapper { /** * This method was generated by MyBatis Generator. * This method corresponds to the database table t_tag * * @mbg.generated */ long countByExample(TagExample example); /** * This method was generated by MyBatis Generator. * This method corresponds to the database table t_tag * * @mbg.generated */ int deleteByExample(TagExample example); } |
扩展后的dao层接口
1
2
3
|
public interface TagExtendMapper extends TagMapper { ... } |
2、继承原始mapper.xml的结果映射
原始mapper.xml的结果映射
1
2
3
4
5
6
7
8
9
10
11
|
< mapper namespace = "com.xxx.dao.mapper.TagMapper" > < resultMap id = "BaseResultMap" type = "com.xxx.dao.Tag" > <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> < id column = "id" jdbcType = "CHAR" property = "id" /> < result column = "tag_name" jdbcType = "VARCHAR" property = "tagName" /> < result column = "tag_alias" jdbcType = "VARCHAR" property = "tagAlias" /> </ resultMap > </ mapper > |
扩展mapper.xml的结果映射
1
2
3
4
|
< mapper namespace = "com.xxx.dao.TagExtendMapper" > < select id = "xxxxx" resultMap = "com.xxx.dao.mapper.TagMapper.BaseResultMap" > <!-- 这里时原始命名空间加上结果集id --> </ select > </ mapper > |
或者是
1
2
3
4
5
|
< mapper namespace = "com.xxx.dao.TagExtendMapper" > < resultMap id = "ExtBaseResultMap" type = "com.xxx.dao.Tag" extend = "com.xxx.dao.mapper.TagMapper.BaseResultMap" > ... </ resultMap > </ mapper > |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/wzwenhuan/article/details/36031021