Mybatis-nested query

1.1 Project directory

1.2 What is a nested query

Nested query is to split the joint query statement in the original multi-table query into a single table query, and then use the syntax of mybatis to nest together

Case realization

-- 需求:查询一个订单,与此同时查询出该订单所属的用户

# 1. 联合查询
select * from orders o left join user u on o.oid =u.id
#2. 嵌套查询
	#2.1先查询订单
		Select * from orders
	#2.2 再根据订单uid外键,查询用户
		select * from user where id = #{根据订单查询的oid}
	2.3 最后使用mybatis,将以上二步嵌套起来
...

1.3 One-to-one nested query

1.3.1 Basic introduction

Requirement: query an order, and at the same time query the user to which the order belongs

One-to-one query

-- 先查询订单
Select * from orders;
-- 再根据订单oid外键,查询用户
select * from user where id = #{根据订单查询的oid}

1.3.2 Code Implementation

OrderMapper interface

// 1.查询所有的订单,与此同时还要查出每个订单所属的用户信息
List<Orders> findAllWithUser1();

OrderMapper.xml

<!--1.1 orderMap映射-->
<resultMap id="orderMap1" type="orders">
    <id column="id" property="id"></id>
    <result column="ordertime" property="ordertime"></result>
    <result column="total" property="total"></result>
    <result column="oid" property="oid"></result>

    <association property="user" javaType="user" column="oid" select="cn.guardwhy.dao.UserMapper.findById"/>
</resultMap>
<!--1.2 一对一嵌套查询-->
<select id="findAllWithUser1" resultMap="orderMap1">
    select * from orders
</select>

UserMapper interface

// 根据ID查询用户
User findById(Integer id);

UserMapper.xml

<!--1.1映射主键-->
<resultMap id="userMap1" type="user">
    <id column="id" property="id"></id>
    <result column="user_name" property="username"></result>
    <result column="birthday" property="birthday"></result>
    <result column="sex" property="sex"></result>
    <result column="address" property="address"></result>
</resultMap>
<!--1.2 根据id查询用户-->
<select id="findById" resultMap="userMap1">
    select * from user where id = #{oid}
</select>

Test code

@Test
public void testOrderWithUser(){
    
    
    // 1.通过工具类得到会话对象
    SqlSession sqlSession = MybatisUtils.getSession();
    // 2.会话对象的得到mapper接口代理对象
    OrderMapper mapper = sqlSession.getMapper(OrderMapper.class);
    // 3.调用方法
    List<Orders> orders = mapper.findAllWithUser1();
    // 4.遍历操作
    for (Orders order : orders) {
    
    
        System.out.println(order);
    }
}

Results of the

1.4 One-to-many nested query

1.4.1 Basic introduction

Requirements: Query all users, and at the same time query the orders that the user has

One-to-many query

-- 先查询用户
Select * from user;
-- 再根据用户id主键,查询订单列表
select * from orders where oid = #{用户id}

1.4.2 Code Implementation

UserMapper interface

// 查询所有的用户,同时还要查询出每个用户所关联的订单信息
List<User> findAllWithOrders();

UserMapper.xml

<!--2.1映射主键-->
<resultMap id="userMap2" type="user">
    <id column="id" property="id"></id>
    <result column="user_name" property="username"></result>
    <result column="birthday" property="birthday"></result>
    <result column="sex" property="sex"></result>
    <result column="address" property="address"></result>
    <!--根据用户Id,查询订单表-->
    <collection property="ordersList" column="id" ofType="Orders" select="cn.guardwhy.dao.OrderMapper.findByUid"></collection>
</resultMap>
<!--一对多嵌套查询-->
<select id="findAllWithOrders" resultMap="userMap2">
    select * from user
</select>

OrderMapper interface

// 根据oid查询对应订单
List<Orders> findByUid(Integer oid);

OrderMapper.xml

<!--一对多嵌套查询-->
<select id="findByUid" parameterType="int" resultType="orders">
    select * from orders where oid = #{oid}
</select>

Test code

@Test
public void testUserWithOrder(){
    
    
    // 1.通过工具类得到会话对象
    SqlSession sqlSession = MybatisUtils.getSession();
    // 2.会话对象的得到mapper接口代理对象
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    // 3.调用方法
    List<User> userList = mapper.findAllWithOrders();
    // 4.遍历操作
    for (User user : userList) {
    
    
        System.out.println(user);
    }
}

Results of the

1.5 Many-to-many nested queries

1.5.1 Basic introduction

Requirement: Query the user and query all the roles of the user at the same time

Many-to-many query statement

-- 先查询用户
select * from user;
-- 再根据用户id主键,查询角色列表
 select * from role r inner join user_role ur on r.id = ur.user_id where ur.role_id = #{用户id};

1.5.2 Code example

UserMapper interface

// 查询所有的用户,同时还要查询出每个用户所关联的订单信息
List<User> findAllWithOrders();

UserMapper.xml

<!--2.1映射主键-->
<resultMap id="userMap2" type="user">
    <id column="id" property="id"></id>
    <result column="user_name" property="username"></result>
    <result column="birthday" property="birthday"></result>
    <result column="sex" property="sex"></result>
    <result column="address" property="address"></result>
    <!--根据用户Id,查询订单表-->
    <collection property="ordersList" column="id" ofType="Orders" select="cn.guardwhy.dao.OrderMapper.findByUid"></collection>
</resultMap>
<!--一对多嵌套查询-->
<select id="findAllWithOrders" resultMap="userMap2">
    select * from user
</select>

RoleMapper interface

// 根据id查询角色
List<Role> findByOid(Integer id);

RoleMapper.xml

<!--2.1 配置角色的映射-->
<resultMap id="roleMap" type="role">
    <id property="id" column="id"></id>
    <id property="roleName" column="role_name"/>
    <id property="roleDetail" column="role_detail"/>
</resultMap>
<!--2.2 多对多嵌入查询-->
<select id="findByOid" parameterType="int" resultMap="roleMap">
    select * from role r inner join user_role ur on r.id = ur.user_id where ur.role_id = #{oid}
</select>

Test code

 @Test
public void testUserWithRole(){
    
    
    // 1.通过工具类得到会话对象
    SqlSession sqlSession = MybatisUtils.getSession();
    // 2.会话对象的得到mapper接口代理对象
    UserMapper mapper = sqlSession.getMapper(UserMapper.class);
    // 3.调用方法
    List<User> list = mapper.findAllWithRole();
    // 4.遍历操作
    list.forEach(System.out::println);
}

Results of the

Guess you like

Origin blog.csdn.net/hxy1625309592/article/details/114847381