Loading

MyBatis 一对一关联查询

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="Class">

    <!-- 1.联表查询SELECT * from class c ,teacher t where c.t_id = t.t_id and c.c_id=1 -->
    <select id="getClass" parameterType="int" resultMap="getClassMap">
        SELECT *
        from class c ,teacher t where c.t_id = t.t_id and c.c_id=#{id}
    </select>
    <resultMap type="com.stone.bean.ClassT" id="getClassMap">
        <id column="c_id" property="id" />
        <result column="c_name" property="name" />
        <association property="teacher" javaType="com.stone.bean.Teacher">
            <id column="t_id" property="id" />
            <result column="t_name" property="name" />
        </association>
    </resultMap>
    <!-- 2.查询两次select * from class where c_id = 1 ; SELECT * from teacher where 
        t_id = 1 -->
    <select id="getClass2" parameterType="int" resultMap="getClass2Map">
        SELECT *
        from class c where c.c_id=#{id}
    </select>
    <select id="getTeacher" parameterType="int" resultType="com.stone.bean.Teacher">
        select
        t_id id,t_name name
        from teacher where t_id=#{id}
    </select>
    <resultMap type="com.stone.bean.ClassT" id="getClass2Map">
        <id column="c_id" property="id" />
        <result column="c_name" property="name" />
        <association property="teacher" column="t_id" select="getTeacher">

        </association>
    </resultMap>
</mapper>

 

java Bean:

package com.stone.bean;

public class ClassT {

    private int id;
    private String name;
    private Teacher teacher;
    public ClassT(int id, String name, Teacher teacher) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }
    public ClassT() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int 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
                + "]";
    }
    
}
package com.stone.bean;

public class Teacher {
    private int id;
    private String name;
    public Teacher(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    public Teacher() {
        super();
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "Teacher [id=" + id + ", name=" + name + "]";
    }
    
}

 

测试:

package com.stone.dao;

import org.apache.ibatis.session.SqlSession;

import com.stone.bean.ClassT;
import com.stone.db.DBAccess;

public class DBDaoClass {

    public static void main(String[] args) {
        DBAccess dbAccess = new DBAccess();
        SqlSession sqlSession = null;
        try {
            sqlSession = dbAccess.getSqlSession();
            String statement = "Class.getClass";
            Object parameter = 1;
            // 通过sqlSession执行SQL语句;
            ClassT class1 = sqlSession.selectOne(statement, parameter);
            System.out.println(class1);
            System.out.println("=======================");
            statement = "Class.getClass2";
            ClassT class2 = sqlSession.selectOne(statement, parameter);
            System.out.println(class2);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

}

 

posted @ 2015-05-03 14:47  stono  阅读(319)  评论(0编辑  收藏  举报