Inheritance Mappings
There are three kinds of mappings in hibernate to create a table table generating tables
MyBatis Database design using a table-based approach
Where Eye_color field is the cat private attribute Fur_color for the dog private property
- entity class
public class Animal implements Serializable {
private static final long Serializable = 1L;
Private Integer ID;
private String name;
Public Integer getId () {
return ID;
}
public void SetId (Integer id) {
this.id = ID;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
this.name = name;
}
@Override public
String toString () {
return "animal{" +
"id=" + ID +
", name= ' + name + ' + ' +
' }';
}
}
Cat Class
public class Cat extends Animal {
private String eyecolor;
Public String Geteyecolor () {
return eyecolor;
}
public void Seteyecolor (String eyecolor) {
this.eyecolor = Eyecolor;
}
@Override public
String toString () {
return super.tostring () + "cat{" +
"eyecolor=" + eyecolor + ' \ ' +'}';
}
}
Dog Class
public class Dog extends Animal {
private String furcolor;
Public String Getfurcolor () {
return furcolor;
}
public void Setfurcolor (String furcolor) {
this.furcolor = Furcolor;
}
@Override public
String toString () {
return super.tostring () + "dog{" +
"furcolor=" + furcolor + ' \ ' +< c29/> '} ';
}
}
configuration file
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="jdbc.properties"/>
<typeAliases>
<package name="com.bjlemon.mybatis.domian"/>
<package name="com.bjlemon.mybatis.vo"/>
</typeAliases>
<environments default="mybatis_1">
<environment id="mybatis_1">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com.bjlemon.mybatis.mapper"></package>
</mappers>
</configuration>
Mapper Interface
void Savecat (cat cat);
void Savedog (dog dog);
/** *
polymorphic query
* @param ID
* @return *
/Animal Finbyid (Integer ID);
Service Layer
Public interface Animalservice {
public void Addanimal (Animal Animal);
Animal Findanimalbyid (Integer ID);
}
public class AnimalServiceImpl implements AnimalService {
@Override
public void addAnimal(Animal animal) {
SqlSession sqlSession = null;
Cat cat = null;
Dog dog =null;
try{
sqlSession = MybatisUtils.getSqlSession();
AnimalMapper mapper = sqlSession.getMapper(AnimalMapper.class);
if (animal instanceof Cat){
cat = (Cat) animal;
mapper.saveCat(cat);
}else if (animal instanceof Dog){
dog = (Dog) animal;
mapper.saveDog(dog);
}
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
sqlSession.rollback(); // 异常则回滚
}finally {
MybatisUtils.clolseSqlSession();
}
}
@Override
public Animal findAnimalById(Integer id) {
SqlSession sqlSession = null;
Cat cat = null;
Dog dog = null;
try {
sqlSession = MybatisUtils.getSqlSession();
AnimalMapper mapper = sqlSession.getMapper(AnimalMapper.class);
Animal animal = mapper.finById(id);
if (animal instanceof Cat) {
cat = (Cat) animal;
return cat;
} else if (animal instanceof Dog) {
dog = (Dog) animal;
return dog;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtils.clolseSqlSession();
}
return null;
}
}
Entity mapping File
<? 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.bjlemon.mybatis.mapper.AnimalMapper">
<insert id = "saveCat" parameterType = "Cat">
INSERT INTO
mybatis_in_action_adv_animal (animal_id, animal_name, eye_color, fur_color, animal_type)
VALUES
(null, # {name}, # {eyeColor}, null, 'C');
</ insert>
<insert id = "saveDog" parameterType = "Dog">
INSERT INTO
mybatis_in_action_adv_animal (animal_id, animal_name, eye_color, fur_color, animal_type)
VALUES
(null, # {name}, null, # {furColor}, 'D');
</ insert>
<!-
discriminator: the discriminator encapsulates the result differently according to the returned animal_type field
javaType: field type
column: field name used for identification
case: judged value
->
<resultMap id = "AnimalRM" type = "Animal">
<id property = "id" column = "animal_id" />
<result property = "name" column = "animal_name" />
<discriminator javaType = "string" column = "animal_type">
<case value = "C" resultMap = "CatRM" />
<case value = "D" resultMap = "DogRM" />
</ discriminator>
</ resultMap>
<resultMap id = "CatRM" type = "Cat" extends = "AnimalRM">
<result property = "eyeColor" column = "eye_color" />
</ resultMap>
<resultMap id = "DogRM" type = "Dog" extends = "AnimalRM">
<result property = "furColor" column = "fur_color" />
</ resultMap>
<select id = "finById" resultMap = "AnimalRM">
SELECT
*
FROM
mybatis_in_action_adv_animal
WHERE
animal_id = # {id}
</ select>
</ mapper>