首页 > 其他 > 详细

Mybatis(关联查询)

时间:2019-08-31 19:25:45      阅读:80      评论:0      收藏:0      [点我收藏+]

  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>

 

 

Mybatis(关联查询)

原文:https://www.cnblogs.com/zfyyfw/p/11440029.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!