zoukankan      html  css  js  c++  java
  • Mybatis中的collection和association一关系

    collection 一对多和association的多对一关系

    学生和班级的一对多的例子

    班级类:

    package com.glj.pojo;
    
    import java.io.Serializable;
    import java.util.List;
    
    public class Clazz implements Serializable{
        private Integer id;
        private String code;
        private String name;
            //班级与学生是一对多的关系
        private List<Student> students;
    //省略set/get方法
    }

    学生类:

    package com.glj.pojo;
    
    import java.io.Serializable;
    
    public class Student implements Serializable {
        private Integer id;
        private String name;
        private String sex;
        private Integer age;
            //学生与班级是多对一的关系
        private Clazz clazz;
    //省略set/get方法
    }

    ClazzMapper使用到了集合-collection 即为一对多,一个班级面对多个学生

    package com.glj.mapper;
    
    import com.glj.pojo.Clazz;
    
    public interface ClazzMapper {
        Clazz selectClazzById(Integer id);
    }
    <?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.glj.mapper.ClazzMapper">
        <select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
            select * from tb_clazz where id = #{id}
        </select>
        <resultMap type="com.glj.pojo.Clazz" id="clazzResultMap">
            <id property="id" column="id"/>
            <result property="code" column="code"/>
            <result property="name" column="name"/>
            <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
            <collection property="students" ofType="com.glj.pojo.Student"
            column="id" javaType="ArrayList"
            fetchType="lazy" select="com.glj.mapper.StudentMapper.selectStudentByClazzId">
                <id property="id" column="id"/>
                <result property="name" column="name"/>
                <result property="sex" column="sex"/>
                <result property="age" column="age"/>
            </collection>
        </resultMap>
    </mapper>

    StudentMapper则是与班级为多对一关系,所以使用了关联-association

    package com.glj.mapper;
    
    import com.glj.pojo.Student;
    
    public interface StudentMapper {
        Student selectStudentById(Integer id); 
    }
    <?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.glj.mapper.StudentMapper">
        <select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
            select * from tb_clazz c,tb_student s where c.id = s.id and s.id = #{id}
        </select>
        <select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
            select * from tb_student where clazz_id = #{id}
        </select>
        <resultMap type="com.glj.pojo.Student" id="studentResultMap">
            <id property="id" column="id"/>
            <result property="name" column="name"/>
            <result property="sex" column="sex"/>
            <result property="age" column="age"/>
            <association property="clazz" javaType="com.glj.pojo.Clazz">
                <id property="id" column="id"/>
                <result property="code" column="code"/>
                <result property="name" column="name"/>
            </association>
        </resultMap>
    </mapper>




  • 相关阅读:
    Web开发人员必备工具-Emmet (Zen Coding)
    Zerver是一个C#开发的Nginx+PHP+Mysql+memcached+redis绿色集成开发环境
    jquery 插件站
    数据挖掘项目之---通过对web日志的挖掘来实现内容推荐系统
    类似wordpress的网站模板
    浅谈 SOAP
    在Livemedia的基础上开发自己的流媒体客户端 V 0.01
    模式识别之分类器knn---c语言实现带训练数据---反余弦匹配
    多项式相乘快速算法原理及相应C代码实现---用到fft
    模式识别之基础---常用分类算法特性归纳
  • 原文地址:https://www.cnblogs.com/leccoo/p/11105417.html
Copyright © 2011-2022 走看看