The advantage of the ORM framework is that we can use the object-oriented thinking to operate databases. hibernate, as a heavyweight ORM framework, has powerful support for Object-Oriented. As a semi-automated mybatis, the object-oriented support is also very complete. This article will discuss how to use mybatis to implement inheritance ing.
Class Diagram
There is a motor vehicle parent class, which has two sub-classes: Car and bus
LINK model (t_vehicle)
There is a principle of ORM ing: fine-grained object models and coarse-grained relational models. Therefore, we store all vehicles in a table (t_vehicle) and identify the vehicle type by identifying the field vtype ("C" stands for car, "B" stands for bus)
Code of three entity classes
Vehicle
Package COM. tgb. mybatis. model; public class vehicle {// primary key idprivate string ID; // vehicle name 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
Package COM. tgb. mybatis. model; public class car extends vehicle {// number of doors private int cardoor; // car brand 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
Package COM. tgb. mybatis. model; public class bus extends vehicle {// capacity of the public car private int capacity; Public int getcapacity () {return capacity;} public void setcapacity (INT capacity) {This. capacity = capacity ;}}
Take a look at the Mapper interface for operations on the "car" [only focus on queries]
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 {// query vehicle Vehicle getvechiclebyid (string ID) by ID; // query car getcarbyname (string name) by name );}
Orm ing in XML format
<?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>
Analysis
The most critical is the content in the <discriminator> tag, which is automatically mapped to the corresponding subclass based on the value of the authentication field.
Client test code
Vehiclemapper mapper = session. getmapper (vehiclemapper. class); vehicle = mapper. getvechiclebyid ("1"); system. out. println (vehicle. getname (); car = mapper. getcarbyname ("Land Rover 007"); system. out. println (car. getband ());
A simple example.