美文网首页Java
MyBatis实现多表查询

MyBatis实现多表查询

作者: kanaSki | 来源:发表于2019-08-07 22:36 被阅读0次

1.多表查询方式:
业务装配(对两个表编写单表查询语句,在业务(service层)将查询的两个结果进行关联)
使用AutoMapping特性,在实现两表联合查询时通过别名完成映射
使用MyBatis的resultMap标签进行实现
2.多表查询时,类中包含另一个类的对象的分类:
单个对象
集合对象

<resultMap>标签写在mapper.xml中,有程序员控制sql查询结果与实体类的映射关系。默认MyBatis使用AutoMapping特性。
使用resultMap标签时,select标签内不写resultType属性,而使用resultMap属性引用resultMap标签

一、resultMap实现单表映射:

    <select id="selAll" resultMap="myMap">
        select * from emp
    </select>
    
    <resultMap id="myMap" type="emp">
        <!--主键使用id标签配置映射关系,其余使用result标签配置映射关系-->
        <id property="empno" column="empno"></id>
        <result property="ename" column="ename"></result>
    </resultMap>

column属性指的是数据库中的字段,property指的是实体类中的字段

类中包含一个类对象的结构

二、使用resultMap关联单个对象(N+1方式——先查询出某表的全部信息,再根据这个表的信息查询另一个表的信息)

    <select id="selStu" resultMap="stuMap">
        select * from student
    </select>

    <resultMap id="stuMap" type="student">
        <!--主键使用id标签配置映射关系,其余使用result标签配置映射关系-->
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="age" column="age"></result>
        <result property="tid" column="tid"></result>
        <!--如果关联一个对象,使用association,关联集合,则是使用collection-->
        <association property="teacher" select="com.mapper.StuMapper.selById" column="tid"></association>
    </resultMap>
    
    <select id="selById" resultMap="teacher" parameterType="int">
        select * from teacher where id=#{0}
    </select>

association标签在装配一个对象时使用,其中column属性指将哪个当前表哪个列的值作为参数传入sql语句中,property属性指对象在类中的属性名,select属性指通过哪个查询语句查询出该对象信息

在N+1方式时,如果列名与属性名相同,可以不进行配置,使用AutoMapping特性即可,但是MayBatis只能够给列装配一次,即作为查询条件的列必须写,不能省略

三、使用resultMap关联单个对象(联合查询方式)

    <resultMap type="Student" id="stuMap1">
        <id column="sid" property="id"/>
        <result column="sname" 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="selAll1" resultMap="stuMap1">
         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
    </select>

四、使用resultMap关联集合对象(N+1方式)


类中包含一个集合对象的结构
    <select id="selByTid" parameterType="int" resultType="student">
         select * from student where tid=#{0}
    </select>
    <resultMap type="teacher" id="mymap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <collection property="list" select="com.mapper.StudentMapper.selByTid" column="id"></collection>
    </resultMap>
    <select id="selAll" resultMap="mymap">
         select * from teacher
    </select>

五、使用resultMap关联集合对象(联合查询方式)

    <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>

六、使用AutoMapping关联单个对象

    <select id="selAll" resultMap="teacher">
        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.id
    </select>

相关文章

  • MyBatis实现多表查询

    1.多表查询方式:业务装配(对两个表编写单表查询语句,在业务(service层)将查询的两个结果进行关联)使用Au...

  • InvalidDefinitionException: No s

    在使用springboot + mybatis实现多表关联查询时报以下错误:com.fasterxml.jacks...

  • 5/10day51_查询&多表

    回顾 优化测试方法 mybatis查询和多表 一 Mybatis单表查询 1.1 resultMap标签 如果数据...

  • Mybatis多表查询

    mybatis的连接池 我们在实际开发中都会用到连接池,因为他可以减少我们获取连接消耗的时间。 mybatis提供...

  • mybatis实现跨库多表查询

    需求:查询用户名具体负责的项目的名称已有数据库表如下所示,二者通过项目ID关联 需要实现的SQL查询 实现:核心思...

  • MyBatis实现多表查询——案例:订单

    一、数据库表创建 users用户表、products商品表、types商品类型表、orders订单表、detail...

  • mybatis-plus配置xml进行多表查询

    mybatis-plus多表查询,需自己写xml进行查询。 在mapper中定义,如需分页查询可添加page。 在...

  • mybatis 多表自定义查询实现

    1.pom.xml 2.工具类2.1.自定义查询类 2.2.解析查询结果集为JSONArray字符串 3.单元测试...

  • mybatis联合多表查询

    数据库表 pms_user_tea表保存教师用户信息 pms_exp表保存实验室信息查询信息:所有教师下的所有实验...

  • mybatis多表联合查询

    开发环境:postgresql数据库、idea工具、easy code插件、springboot+mybatis数...

网友评论

    本文标题:MyBatis实现多表查询

    本文链接:https://www.haomeiwen.com/subject/xvxzdctx.html