码迷,mamicode.com
首页 > 其他好文 > 详细

一对一关联

时间:2015-01-28 15:56:26      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

一对一关联

1)需求:根据班级 id 查询班级信息(带老师的信息)


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);
3).  定义实体类:
public class Teacher {
private int id;
private String name;
}

public class Classes {
private int id;
private String name;
private Teacher teacher;
}

4).  定义 sql  映射文件 ClassMapper.xml
<!--
方式一: 嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
封装联表查询的数据(去除重复的数据)
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=1
-->
<select id="getClass" parameterType="int" resultMap="ClassResultMap">
select * from class c, teacher t where c.teacher_id=t.t_id and c.c_id=#{id}
</select>
<resultMap type="_Classes" id="ClassResultMap">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association property="teacher" column="teacher_id" javaType="_Teacher">
<id property="id" column="t_id"/>
<result property="name" column="t_name"/>
</association>
</resultMap>
<!--
方式二: 嵌套查询:通过执行另外一个 SQL 映射语句来返回预期的复杂类型
SELECT * FROM class WHERE c_id=1;
SELECT * FROM teacher WHERE t_id=1 //1 是上一个查询得到的 teacher_id 的值
-->
<select id="getClass2" parameterType="int" resultMap="ClassResultMap2">
select * from class where c_id=#{id}
</select>
<resultMap type="_Classes" id="ClassResultMap2">
<id property="id" column="c_id"/>
<result property="name" column="c_name"/>
<association  property="teacher"  column="teacher_id"  javaType="_Teacher"
select="getTeacher">
</association>
</resultMap>
<select id="getTeacher" parameterType="int" resultType="_Teacher">
SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
</select>



association : 用于一对一关联查询
property : 对象属性的名称
javaType :对象属性的类型
column :所对应的外键字段的名称
select:使用另一个查询封装的结果

  

public class Test5 {

    public static void main(String[] args) {
        SqlSessionFactory factory = MybatisUtils.getFactory();
        SqlSession session = factory.openSession();
        
        String statement = "com.atguigu.day03_mybaits.test5.ClassMapper.getClass";
        
        statement = "com.atguigu.day03_mybaits.test5.ClassMapper.getClass2";
    
        
        Classes c = session.selectOne(statement , 2);
        System.out.println(c);
        
        session.close();
    }
}




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 + "]";
    }

}


public class Classes {

    private int id;
    private String name;
    private Teacher teacher;
    private List<Student> list;

    public Classes(int id, String name, Teacher teacher, List<Student> list) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
        this.list = list;
    }

    public Classes(int id, String name, Teacher teacher) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }

    public Classes() {
        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 "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher
                + ", list=" + list + "]";
    }



ClassMapper.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.atguigu.day03_mybaits.test5.ClassMapper">
    <!-- 
        根据班级id查询班级信息(带老师的信息)
        ##1. 联表查询
        SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=1;
        
        ##2. 执行两次查询
        SELECT * FROM class WHERE c_id=1;  //teacher_id=1
        SELECT * FROM teacher WHERE t_id=1;//使用上面得到的teacher_id
     -->
    <!-- 
        方式一:嵌套结果:使用嵌套结果映射来处理重复的联合结果的子集
                 封装联表查询的数据(去除重复的数据)
        select * from class c, teacher t where c.teacher_id=t.t_id and  c.c_id=1
    -->
    <select id="getClass" parameterType="int" resultMap="getClassMap">
        SELECT * FROM class c,teacher t WHERE c.teacher_id=t.t_id AND c.c_id=#{id}
    </select>
    <resultMap type="Classes" id="getClassMap">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" javaType="Teacher">
            <id property="id" column="t_id"/>
            <result property="name" column="t_name"/>
        </association>
    </resultMap>
    
    <!-- 
    方式二:嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
        SELECT * FROM class WHERE c_id=1;
        SELECT * FROM teacher WHERE t_id=1   //1 是上一个查询得到的teacher_id的值
    -->
    <select id="getClass2" parameterType="int" resultMap="getClass2Map">
        SELECT * FROM class WHERE c_id=#{id}
    </select>
    
    <select id="getTeacher" parameterType="int" resultType="Teacher">
        SELECT t_id id, t_name name FROM teacher WHERE t_id=#{id}
    </select>
    
    <resultMap type="Classes" id="getClass2Map">
        <id property="id" column="c_id"/>
        <result property="name" column="c_name"/>
        <association property="teacher" column="teacher_id" select="getTeacher">
        </association>
    </resultMap>
</mapper>



config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="db.properties"/>
    
    <!-- 配置实体类的别名 -->
    <typeAliases>
        <!-- <typeAlias type="com.atguigu.day03_mybaits.test1.User" alias="_User"/> -->
        <package name="com.atguigu.day03_mybaits.bean"/>
    </typeAliases>
<!-- 
    development : 开发模式
    work : 工作模式
 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${name}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    
    <mappers>
        <mapper resource="com/atguigu/day03_mybaits/test1/userMapper.xml"/>
        <mapper resource="com/atguigu/day03_mybaits/test2/userMapper.xml"/>
        <mapper class="com.atguigu.day03_mybaits.test3.UserMapper"/>
        <mapper resource="com/atguigu/day03_mybaits/test4/orderMapper.xml"/>
        <mapper resource="com/atguigu/day03_mybaits/test5/ClassMapper.xml"/>
        <mapper resource="com/atguigu/day03_mybaits/test6/ClassMapper.xml"/>
        <mapper resource="com/atguigu/day03_mybaits/test7/userMapper.xml"/>
        <mapper resource="com/atguigu/day03_mybaits/test8/userMapper.xml"/>
        <mapper resource="com/atguigu/day03_mybaits/test9/userMapper.xml"/>
    </mappers>
</configuration>

 

一对一关联

标签:

原文地址:http://www.cnblogs.com/aicpcode/p/4256044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!