Mybatis 继承映射

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZQ741552720/article/details/79556021

继承映射

  • 在hibernate中有三种映射方式

    1. 建立一张表
    2. 字表生成表
    3. 各类生成表
  • Mybatis使用建立一张表的方式


  • 数据库设计
    这里写图片描述
    其中eye_color字段为猫私有属性fur_color为狗私用属性

-实体类

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 + '\'' +
                '}';
    }
}

  • 配置文件
<?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接口
 void saveCat(Cat cat);
    void saveDog(Dog dog);

    /**
     * 多态查询
     * @param id
     * @return
     */
    Animal finById(Integer id);

  • service层
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;
    }

}

  • 实体映射文件
<?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:鉴别器 根据返回的animal_type字段的不同 对结果进行不同的封装
    javaType:字段类型
    column:用来鉴别的字段名
    case:判断的值
    -->
    <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>
展开阅读全文

没有更多推荐了,返回首页