mybatis 中简单的一对一关系

1:

  主要需要实现的是一个类型中的一个字段是另外一个类,如Class(班级)表中存在一个字段为teacher,Teacher为另外一个类

  本例源码保存在   D:\海同\mybatis\9.16\FirstMybatis   中

2:

  创建表 

CREATE TABLE teacher(
    t_id INT PRIMARY KEY AUTO_INCREMENT, 
    t_name VARCHAR(20)
);
CREATE TABLE class(
    c_id INT PRIMARY KEY AUTO_INCREMENT, 
c_name VARCHAR(20), 
    teacher_id INT
);
ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCES teacher(t_id);    

INSERT INTO teacher(t_name) VALUES('LS1');
INSERT INTO teacher(t_name) VALUES('LS2');

INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1);
INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2);

  相关类

    Teacher类

package com.yuwenhui.entity;

/**
 * Created by Administrator on 2017/9/17 0017.
 */
public class Teacher {
    private Integer id;
    private String name;

    @Override
    public String toString() {
        return "Teacher{" +
                "id=" + id +
                ", name='" + 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;
    }

    public Teacher(Integer id, String name) {

        this.id = id;
        this.name = name;
    }

    public Teacher() {

    }
}

  Class类

  

package com.yuwenhui.entity;

/**
 * Created by Administrator on 2017/9/17 0017.
 */
public class Class {
    private Integer id;
    private String name;
    private Teacher teacher;

    public Class() {
    }

    public Class(Integer id, String name, Teacher teacher) {

        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }

    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;
    }

    public Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Class{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", teacher=" + teacher +
                '}';
    }
}

  映射文件

<?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.yuwenhui.entity.classMapper">
    <!-- 嵌套结果查询,使用嵌套结果映射来处理重复的联合结果,多表联合查询  -->
    <select id="selectClass" resultMap="classResultMap" parameterType="Integer">
        SELECT * FROM class c,teacher t WHERE t.t_id = c.teacher_id AND c.c_id = #{id};
    </select>
    <resultMap id="classResultMap" type="Class">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <!--一对一声明,注意使用javaType,里面填一个类型,对teacher 的相关查询到的数据进行封装-->
        <association property="teacher" javaType="Teacher">
            <id property="id" column="t_id"/>
            <result property="name" column="t_name"/>
        </association>
    </resultMap>
    <!--嵌套查询,通过执行另一个sql语句,来完成相关的复合类型的,执行多次数据库查询-->
    <select id="selectClass2" parameterType="Integer" resultMap="classResultMap2">
        SELECT * FROM class WHERE c_id = #{id};
    </select>
    <resultMap id="classResultMap2" type="_Class">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <!--对teacher进行处理 column为另外一个查询的参数,select为要处理的对象-->
        <association property="teacher" column="teacher_id" select="getTeacher"/>
    </resultMap>
    <!--为selectClass2提供一个teacher-->
    <select id="getTeacher" parameterType="Integer" resultMap="teacherMap">
        SELECT * FROM teacher WHERE t_id = #{id};
    </select>
    <resultMap id="teacherMap" type="_Teacher">
        <id property="id" column="t_id"/>
        <result property="name" column="t_name"/>
    </resultMap>
</mapper>

  测试

  

private SqlSessionFactory sessionFactory;
    private SqlSession session;

    @Before
    public void befor() {
        String resource = "conf.xml";
//        InputStream resourceAsStream = TestMybatis.class.getClassLoader().getResourceAsStream(resource);
        Reader reader = null;
        try {

            reader = Resources.getResourceAsReader(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sessionFactory = new SqlSessionFactoryBuilder().build(reader);
        session = sessionFactory.openSession();
    }

 /**
     * 测试一对一关联
     * 教室的对象中存在一个老师对象
   * 多表联合查询
*/ @Test public void testOO(){ String statement = "com.yuwenhui.entity.classMapper"+".selectClass"; Class clazz = session.selectOne(statement, 1); System.out.println(clazz); } /** * 测试一对一关联 * 教室的对象中存在一个老师对象
   * 发送多条sql语句进行查询

   */
@Test
public void testOO2(){ String statement = "com.yuwenhui.entity.classMapper"+".selectClass2"; Class clazz = session.selectOne(statement, 1); System.out.println(clazz); } @After public void after() { session.close(); }

 

posted @ 2017-09-19 10:25  西瓜的小弟西西瓜  阅读(272)  评论(0编辑  收藏  举报