mybatis多表查询

摘要:
Mybatis实现多表查询有三种方法:1.查询两个表,并将获得的数据分配给实体类。3.使用MyBatis的标记,程序员编写映射关系。DOCTYPEmaapperPUBLIC“//mybatis.org//DTDMaper3.0//EN”http://mybatis.org/dtd/mybatis-3-mapper.dtd“˃select*fromtstudenthereid=#{0}2。当映射的实体类包含单个对象时,使用映射对象。2.1当前sql查询不包含该对象的属性。然后需要调用其他查询方法来查询<association>标签属性:property:类中对象的属性名称。select:调用哪个查询来查询该对象的信息。哪个列值作为参数传递给调用的查询语句?

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

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

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

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

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

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

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

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

mybatis多表查询第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>来映射一个对象

mybatis多表查询第2张mybatis多表查询第3张

  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>标签对集合进行映射。

mybatis多表查询第4张

  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>

免责声明:文章转载自《mybatis多表查询》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇美国 ZIP Code 一览表pb中数据窗口函数小结(转)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

SQLSERVER排查CPU占用高的情况

内存占用不太高,只占用了30个G CPU占用100% 排查方向 一般排查都是用下面的脚本,一般会用到三个视图sys.sysprocesses ,dm_exec_sessions ,dm_exec_requests 1 USE master 2 GO 3 --如果要指定数据库就把注释去掉 4 SELECT * FROM sys.[sysprocesse...

oracle高级部分

回顾 多表关联查询的方式内连接 根据AB表关联的条件进行过滤查询,只保留满足条件数据 Select * from a,b where a.xxx=b.xxx;     Select * from a inner join b on a.xxx=b.xxxx     inner join c on a.xxxx=c.xxxx 外连接 左外连接 以左表为...

oracle不同用户间访问表不添加用户名(模式)前缀

默认的情况下,oracle里面的用户A,要访问用户B的表需要带用户B的前缀,如访问用户B的 user表,需要这样访问 select * from B.user;如果想要不添加用户前缀,需要这样处理:(user01 就是A用户)1.用视图  create view user select * from B.user;2.使用同义词:grant CREATE...

Java消息系统简单设计与实现

前言:由于导师在我的毕设项目里加了消息系统(本来想水水就过的..),没办法...来稍微研究研究吧..简单简单... 需求分析 我的毕设是一个博客系统,类似于简书这样的,所以消息系统也类似,在用户的消息里包含了有:喜欢和赞、评论、关注、私信这样的一类东西,这样的一个系统应该包含以下的功能: 当用户评论/关注/点赞时能够通知到被评论/关注/点赞的用户,并...

把存储过程SELECT INTO到临时表

在开发过程中,很多时候要把结果集存放到临时表中,常用的方法有两种。 一. SELECT INTO1. 使用select into会自动生成临时表,不需要事先创建12 select * into #temp from sysobjectsselect * from #temp 2. 如果当前会话中,已存在同名的临时表 1 select * into #tem...

layui select动态添加option

<form class="layui-form" action=""> <div class="layui-form-item proSelect"> <label class="layui-form-label">产品类别</label> <div...