MyBatis——關聯映射——OneToMany


實際開發應用中存在很多一對多的關系,例如班級和學生。班級與學生是一對多的關系,學生與班級是多對一的關系。數據庫中常使用多表關聯的方式存儲信息,方便多維維護。

由於配置文件以及封裝獲取SqlSession對象的FKSqlSessionFactory.java文件相同,可參考之前實例配置。

第一步:創建數據庫表

CREATE TABLE tb_clazz(
   id INT PRIMARY KEY,
   code VARCHAR(18)
)

CREATE TABLE tb_student(
   id INT PRIMARY KEY,
   name VARCHAR(18),
   sex VARCHAR(18),
   age INT,
   clazz_id INT,
   FOREIGN KEY (clazz_id) REFERENCES tb_clazz(id)
)

第二步:創建與數據庫表映射的對象類

/MyBatisRelationMapping/src/com/web/mybatis/domain/Clazz.java

    private Integer id;
    private String code;
    private List<Student> clazzlist;

/MyBatisRelationMapping/src/com/web/mybatis/domain/Student.java

    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Clazz clazz;

第三步:寫mapper文件

/MyBatisRelationMapping/src/com/web/mybatis/mapper/ClazzMapper.xml

<?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.web.mybatis.mapper.ClazzMapper">
    <!-- 根據id查詢班級信息,返回resultMap -->
    <select id="selectClazzById" parameterType="int" resultMap="clazzResultMap">
        SELECT * FROM tb_clazz WHERE id=#{id}
    </select>
    <!-- 映射clazz對象的resultMap -->
    <resultMap type="com.web.mybatis.domain.Clazz" id="clazzResultMap">
        <id property="id" column="id"/>
        <result property="code" column="code"/>
        <!-- 一對多關聯映射:collection fetchType="lazy"表示懶加載 -->
        <collection property="clazzlist" javaType="ArrayList" 
            column="id" ofType="com.web.mybatis.domain.Student"
            select="com.web.mybatis.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>

/MyBatisRelationMapping/src/com/web/mybatis/mapper/StudentMapper.xml

<?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.web.mybatis.mapper.StudentMapper">
    <!-- 根據班級id查詢學生信息,返回resultMap -->
    <select id="selectStudentByClazzId" parameterType="int" resultMap="studentResultMap">
        SELECT * FROM tb_student WHERE clazz_id=#{id}
    </select>
    <!-- 根據id查詢學生信息,多表連接,返回resultMap -->
    <select id="selectStudentById" parameterType="int" resultMap="studentResultMap">
        SELECT * FROM tb_clazz c,tb_student s WHERE c.id=s.clazz_id AND s.id=#{id}
    </select>
    <!-- 映射Student對象的resultMap -->
    <resultMap type="com.web.mybatis.domain.Student" id="studentResultMap">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <!-- 多對一關聯映射:association -->
        <association property="clazz" javaType="com.web.mybatis.domain.Clazz">
            <id property="id" column="id"/>
            <result property="code" column="code"/>
        </association>
    </resultMap>
</mapper>

第四步:創建與mapper文件對應的接口類

/MyBatisRelationMapping/src/com/web/mybatis/mapper/ClazzMapper.java

package com.web.mybatis.mapper;

import com.web.mybatis.domain.Clazz;

public interface ClazzMapper {
    Clazz selectClazzById(Integer id);
}

/MyBatisRelationMapping/src/com/web/mybatis/mapper/StudentMapper.java

package com.web.mybatis.mapper;

import com.web.mybatis.domain.Student;

public interface StudentMapper {
    Student selectStudentById(Integer id);
}

第五步:測試代碼

package com.web.mybatis.test;
import org.apache.ibatis.session.SqlSession;
import com.web.mybatis.domain.Clazz;
import com.web.mybatis.domain.Student;
import com.web.mybatis.factory.FKSqlSessionFactory;
import com.web.mybatis.mapper.ClazzMapper;
import com.web.mybatis.mapper.StudentMapper;

public class Test {
    /**一對多
     * 查詢班級級聯查詢學生
     * */
    public static void oneToManyTest() {
        SqlSession session = FKSqlSessionFactory.getSqlSession();
        //獲得mapper接口的代理對象
        ClazzMapper cm = session.getMapper(ClazzMapper.class);
        Clazz clazz = cm.selectClazzById(1);
        System.out.println("班級:"+clazz.toString());
        for(Student stu : clazz.getClazzlist()) {
            System.out.println("--學生:"+stu.toString());
        }
        session.commit();
        session.close();
    }
    /**多對一
     * 查詢學生級聯查詢班級
     * */
    public static void manyToOneTest() {
        SqlSession session = FKSqlSessionFactory.getSqlSession();
        //獲得mapper接口的代理對象
        StudentMapper sm = session.getMapper(StudentMapper.class);
        Student stu = sm.selectStudentById(1);
        System.out.println(stu.toString()+stu.getClazz().toString());
        session.commit();
        session.close();
    }
    public static void main(String[] args) {
//        oneToManyTest();
        manyToOneTest();
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM