mybatis association collection关键字

如欲转载,请注明出处                   ----------------------------原创 wksdu

对于级联操作,很多框架给出了相应的实现方案,其中Hibernate直接给出onetoone manytomany等一系列注解 简单直接 但是使用mybatis如何实现呢?

mybatis并没有给出类似hibernate简单直接的一对一 一对多的显式声明 但给予了两个相关的association collection关键字

在这里 解释下两个关键字 association 在英语中多指一对一性质的相关 注意这是意思 但是mybatis为什么不直接用onetoone代表呢,这就体现出命名和功能相匹配的好处或者概念来了

在《深入浅出mybatis技术原理与实战》这本书中,很显然将association collection当成了一对一 一对多的标志,事实上是不对的,虽然他能达到相似的效果 如下:

如有student表 studen_tself_card表 显然是一对一的关系

如下:

 class Student 
    private int id;
    private String name;
    private StudentSelfCard card;
 class StudentSelfCard 
    private int id;
    private String startDate;
    private String endDate;
    private Student student;

注意此处student类中包含studentselfcard的引用,同时studentselfcard类亦包含student的引用

那么怎么实现类似需求呢? 

声明的mapper;接口如下:

interface StudentMapper 
    public Student getStudentById(int id);
interface StudentSelfCardMapper 
    public StudentSelfCard getSelfCardById(int id);

mapper文件如下:

<mapper namespace="Helison.mapper_interfaces.StudentMapper">
    <resultMap id="studentMap" type="Helison.po.Student">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <association property="card" column="id"  select="Helison.mapper_interfaces.StudentSelfCardMapper.getSelfCardById" />   注意此处加粗!!!
    </resultMap>
    <select id="getStudentById" resultMap="studentMap" parameterType="int">
        select *
        from STUDENT
        where ID=#{id}
    </select>
    <select id="getStudentByClassId" resultMap="studentMap">
        select *
        from STUDENT
        where CLASS_ID=#{classid}
    </select>

</mapper>
<mapper namespace="Helison.mapper_interfaces.StudentSelfCardMapper">
    <resultMap id="studentsc" type="Helison.po.StudentSelfCard">
        <id property="id" column="id"/>
        <result property="endDate" column="end_date"/>
        <result property="startDate" column="start_date"/>
        <association property="student" column="id" 	select="Helison.mapper_interfaces.StudentMapper.getStudentById"/>   注意此处加粗!!!!
    </resultMap>
    <select id="getSelfCardById" resultMap="studentsc" parameterType="int">
        select *
        from STUDENT_SELF_CARD
        where ID=#{id}
    </select>
</mapper>

之后test一下看是否会成功!!

public class App 
{
    public static void main( String[] args )
    {
        String config="Helison/config/config.xml";
        try {
            InputStream inputStream=Resources.getResourceAsStream(config);
            SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
            SqlSessionFactory factory=builder.build(inputStream);
            SqlSession sqlSession=factory.openSession();
            StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);
            Student student=mapper.getStudentById(3);
            System.out.println(student.getName());
            System.out.println(student.getCard().getStudent().getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

学生表:

学生证表:

控制台打印数据如下:

证明两者均有效!

---------------------------------------------------------分割线---------------------------------------------

上面是怎么实现的 明明是循环互相调用,这个应该怎么想呢?原理方面暂时还没想清楚,仅仅是猜测这些对象君保存在了mybatis缓存中,当mybatis检测到循环依赖时,会将之前已经写好的对象直接映射过来。当时目前只是假设,还有很多疏漏的地方。。。。

猜你喜欢

转载自blog.csdn.net/WK_SDU/article/details/81095291