Mybatis如何实现多表查询

简介: Mybatis如何实现多表查询

Mybatis是新多表查询的方式也有二种 :

第一种是 : 编写多表关联查询的SQL语句 , 使用ResultMap建立结果集映射 , 在ResultMap中建立多表结果集映射的标签有associationcollection

<resultMap id="Account_User_Map" type="com.heima.entity.Account">
    <id property="id" column="id"></id>
    <result property="uid" column="uid"></result>
    <result property="money" column="money"></result>
    <association property="user">
        <id property="id" column="uid"></id>
        <result property="username" column="username"></result>
        <result property="birthday" column="birthday"></result>
        <result property="sex" column="sex"></result>
        <result property="address" column="address"></result>
    </association>
</resultMap>
<!--public Account findByIdWithUser(Integer id);-->
<select id="findByIdWithUser" resultMap="Account_User_Map">
    select  a.*,  username, birthday, sex, address  from account a , user u where a.UID = u.id and a.ID = #{id} ;
</select>

第二种是 : 将多表查询分解为多个单表查询, 使用ResultMap表的子标签associationcollection标签的select属性指定另外一条SQL的定义去执行, 然后执行结果会被自动封装

<resultMap id="Account_User_Map" type="com.heima.entity.Account" autoMapping="true">
    <id property="id" column="id"></id>
    <association property="user" select="com.heima.dao.UserDao.findById" column="uid" fetchType="lazy"></association>
</resultMap>
<!--public Account findByIdWithUser(Integer id);-->
<select id="findByIdWithUser" resultMap="Account_User_Map">
    select * from account where  id = #{id}
</select>
相关文章
|
5月前
|
Java 数据库连接 数据库
MyBatis之多表查询
MyBatis之多表查询
|
7月前
|
SQL XML Java
Mybatis:SQL注入问题 like模糊查询 多表查询 动态SQL
Mybatis:SQL注入问题 like模糊查询 多表查询 动态SQL
104 0
|
2月前
|
XML Java 数据库连接
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
【MyBatis】1、MyBatis 核心配置文件、多表查询、实体映射文件 ......
90 0
|
7月前
|
XML Java 数据库连接
“MyBatis中的关联关系配置与多表查询“
“MyBatis中的关联关系配置与多表查询“
27 0
|
8月前
|
SQL XML Java
Mybatis的多表查询操作 2
Mybatis的多表查询操作
55 1
|
8月前
|
XML Java 数据库连接
Mybatis的多表查询操作 1
Mybatis的多表查询操作
43 1
|
9月前
|
SQL Java 数据库连接
|
10月前
|
SQL Java 数据库连接
Mybatis如何实现多表查询?
MyBatis是一个Java持久层框架,它提供了多种方式来实现多表查询。
709 0
|
10月前
|
SQL XML Java
如何在 MyBatis 中进行多表查询以及注解开发?
如何在 MyBatis 中进行多表查询以及注解开发?
248 0
|
11月前
|
SQL Java 数据库连接
Mybatis多表查询之一对多、多对一
Mybatis多表查询之一对多、多对一
107 0