在hibernate中有三种映射方式
Mybatis使用建立一张表的方式
-实体类
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类
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类
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 + '\'' +
'}';
}
}
<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>
void saveCat(Cat cat);
void saveDog(Dog dog);
/**
* 多态查询
* @param id
* @return
*/
Animal finById(Integer id);
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;
}
}
<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>
<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>