zoukankan      html  css  js  c++  java
  • mybatis多表查询

    Mybatis实现多表查询有三种方式:

    (需求:给一个实体类设置属性值,但该实体类的数据保存在数据库的两张表中)

    1,分别对两张表进行查询,将获取到的数据分别赋值给实体类。

    2,编写多表查询的sql语句,通过给查询到的数据设置与实体类相同的别名,使用Auto Mapping特性,将查询结果自动映射到实体类。

    3,使用MyBatis的<resultMap>标签,程序员编写映射关系。

    (注意这些方法可以用于多表查询,当然单表查询也可以)

    下面介绍使用<resultMap>标签的方法:

    1,需要映射的实体类中不包含对象时;

      1.1 使用<resultMap>标签时,<select>标签不写 resultType 属性,而是使用 resultMap 属性引用<resultMap>标签,resultMap属性写<resultMap>标签的id属性值,表示引用该<resultMap>的映射关系。

      1.2 <resultMap>标签中的每一个标签代表一个映射关系,<id>表示主键映射,<result>表示普通映射。

    <?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.bjsxt.mapper.StudentMapper">
        <resultMap type="student" id="mymap">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="age" property="age"/>
            <result column="tid" property="tid"/>
        </resultMap>
        <select id="selById" resultMap="mymap" parameterType="int">
            select * from student where id = #{0}
        </select>
    </mapper>

    2,当映射的实体类中包含单个对象,使用<association>来映射一个对象

      2.1当前sql查询不包含那个对象的属性,则需要调用别的查询方法来查询

      <association>标签属性:

    • (1) property: 对象在类中的属性名
    • (2)select:调用哪个查询查询出这个对象的信息
    • (3)将当前哪列的值作为参数传递给调用的查询语句
    <?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.bjsxt.mapper.StudentMapper">
        <resultMap type="student" id="mymap">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="age" property="age"/>
            <result column="tid" property="tid"/>
            <association property="teacher" select="com.bjsxt.mapper.TeacherMapper.selById" column="tid"></association>
        </resultMap>
        <select id="selById" resultMap="mymap" parameterType="int">
            select * from student where id = #{0}
        </select>
    </mapper>

      2.2如果当前查询结果包含该对象的属性,则不需要调用别的查询方法

        <association>标签属性:

      (1) 此时把<association/>看成小的<resultMap>看待

      (2)javaType 属性:<association/>专配完后返回一个什么类型的对象.取值是一个类(或类的别名)

    <?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.bjsxt.mapper.StudentMapper">
        <resultMap type="student" id="mymap">
            <id column="id" property="id"/>
            <result column="name" property="name"/>
            <result column="age" property="age"/>
            <result column="tid" property="tid"/>
            <association property="teacher" javaType="Teacher" >
                <id column="tid" property="id"/>
                <result column="tname" property="name"/>
            </association>
        </resultMap>
        <select id="selById" resultMap="mymap" parameterType="int">
            select s.id sid,s.name sname,age age,t.id tid,t.name tname FROM student s left outer join teacher t on s.tid=t.id where s.id = #{0}
        </select>
    </mapper>            

    3,当要映射的实体类中包含一个集合对象,使用<collection>标签对集合进行映射。

      3.1当前查询不包含集合数据,调用其他方法进行查询集合的数据

    <resultMap type="teacher" id="mymap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="list" select="com.bjsxt.mapper.StudentMapper.selByTid" column="id"></collection>
        </resultMap>
    <select id="selAll" resultMap="mymap">
        select * from teacher
    </select>

       3.2当前查询已经包含集合的数据,不需要调用其他方法查询

        属性:ofType与javaType相似,表示该标签配置完返回一个什么类型的对象。

    <resultMap type="teacher" id="mymap1">
        <id column="tid" property="id"/>
        <result column="tname" property="name"/>
        <collection property="list" ofType="student" >
            <id column="sid" property="id"/>
            <result column="sname" property="name"/>
            <result column="age" property="age"/>
            <result column="tid" property="tid"/>
        </collection>
    </resultMap>
    <select id="selAll1" resultMap="mymap1">
        select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid;
    </select>

    注意:在上面的代码中查询结果包含多个Student,则肯定创建了多个student对象,那teacher会不会也被创建了多个呢?其实不会的,虽然查询的结果确实每一条数据都会包含teacher的数据,但是每条的teacher的id属性都是一样的,MyBatis相同的id值只会创建一个对象。

    Auto Mapping 也可对对象进行映射

      1,将查到的属性名改为 `teacher.id`的形式,Mybatis就会去寻找teacher对象的id属性进行映射。

    <select id="selAll" resultType="student">
        select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid
    from student s LEFT JOIN teacher t on t.id=s.tid
    </select>
  • 相关阅读:
    服务器
    python的并发编程学习
    python的网络编程学习
    python的基础学习 模块
    pymysql基础操作
    查询简单练习
    数据准备
    数据库的三大设计范式
    数据库基础-建表约束
    mysql基础-基本语法
  • 原文地址:https://www.cnblogs.com/zdl2234/p/11337013.html
Copyright © 2011-2022 走看看