Mybatis(关联查询)

  Mybatis在写查询的时候经常会遇到链表查询,Mybatis的联表查询写法如下:

  (1) 根据 classId 查询对应的班级信息,包括学生,老师

  
-- 创建表和数据:
create database mybatis;
use mybatis;
CREATE TABLE users(id INT PRIMARY KEY AUTO_INCREMENT, NAME
VARCHAR(20), age INT);
INSERT INTO users(NAME, age) VALUES('Tom', 12);
INSERT INTO users(NAME, age) VALUES('Jack', 11);

CREATE TABLE student(
s_id INT PRIMARY KEY AUTO_INCREMENT,
s_name VARCHAR(20),
class_id INT
);


  

//班级
public class Clazz {
    private int cId;
    private String cName;
    private String teacherId;
    private Teacher teacher;    //将Teacher封装到班级里
    //get,set      
}
//老师
public class Teacher {
    private int id;
    private String name;
    //get,set   
}

  1.一对一或是多对一时

  二次查询

<?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.zhiyou100.hhz.dao.ClazzDao">
    <select id="selectById" parameterType="int" resultMap="mymap">
        select * from class where c_id=#{cid}
    </select>
    <select id="selectByTeacherId" resultType="com.zhiyou100.hhz.bean.Teacher">
        select t_id tid,t_name tname from teacher where t_id=#{tid}
    </select>
    <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="mymap">
        <id column="c_id" property="cId"/>
        <result column="c_name" property="cName"/>
        <result column="teacher_id" property="teacherId"/>
        <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher" 
        column="teacher_id" select="selectByTeacherId"></association>
    </resultMap>
</mapper>

  联表查询

<?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.zhiyou100.hhz.dao.ClazzDao">
    
    <select id="selectByTeacherId" resultMap="TeacherMap">
        select * from teacher t join class c on t.t_id=c.teacher_id where c_id=#{id}
    </select>
    
    <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="TeacherMap">
        <id column="c_id" property="cId"/>
        <result column="c_name" property="cName"/>
        <result column="teacher_id" property="teacherId"/>
        <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher">
        <result column="t_id" property="tId"/>
        <result column="t_name" property="tName"/>
        </association>
    </resultMap>
</mapper>

  2. 一对多

  二次查询

<?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.zhiyou100.hhz.dao.ClazzDao">
    <select id="selectById" parameterType="int" resultMap="mymap">
        select * from class where c_id=#{cid}
    </select>
    <select id="selectByTeacherId" resultType="com.zhiyou100.hhz.bean.Teacher">
        select t_id tid,t_name tname from teacher where t_id=#{tid}
    </select>
    <select id="selectByClassId" resultType="com.zhiyou100.hhz.bean.Student">
        select s_id id,s_name name from student where class_id=#{classid}
    </select>
    <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="mymap">
        <id column="c_id" property="cId"/>
        <result column="c_name" property="cName"/>
        <result column="teacher_id" property="teacherId"/>
        <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher" 
        column="teacher_id" select="selectByTeacherId"></association>
        <collection property="students" ofType="com.zhiyou100.hhz.bean.Student" select="selectByClassId" column="c_id"></collection>
    </resultMap>
</mapper>

  联表查询

<?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.zhiyou100.hhz.dao.ClazzDao">
    
    <select id="selectByTeacherId" resultMap="TeacherMap">
        select * from teacher t join class c on t.t_id=c.teacher_id join student s on s.class_id=c.c_id where c_id=#{id}
    </select>
    
    <resultMap type="com.zhiyou100.hhz.bean.Clazz" id="TeacherMap">
        <id column="c_id" property="cId"/>
        <result column="c_name" property="cName"/>
        <result column="teacher_id" property="teacherId"/>
        <association property="teacher" javaType="com.zhiyou100.hhz.bean.Teacher">
        <result column="t_id" property="tId"/>
        <result column="t_name" property="tName"/>
        </association>
        <collection property="students" ofType="com.zhiyou100.hhz.bean.Student">
            <result column="s_id" property="id"/>
            <result column="s_name" property="name"/>
        </collection>
    </resultMap>
</mapper>

猜你喜欢

转载自www.cnblogs.com/zfyyfw/p/11440029.html