zoukankan      html  css  js  c++  java
  • MyBatis 高级查询之一对一查询(九)

    高级查询之一对一查询

    查询条件:根据游戏角色ID,查询账号信息

    我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下:

    		/**
         * 根据角色ID查询账号信息
         * @param id 角色Id
         * @return 角色实体对象
         */
        public RoleEntity selectRoleById(int id);
    

    接下来,我分别演示关联查询和子查询方式实现接口方法的映射。

    关联查询方式:

    现在我们暂时先抛开 MyBatis 框架,直接从数据库出发写一写关联查询的 SQL 语句,如下:

    select r.*,a.* from tb_role as r join tb_account as a on r.account_id=a.id where r.id=1
    

    另外,还有一种不使用 join 关键字的 SQL 语句写法,如下:

    select r.*,a.* from tb_role as r,tb_account as a where r.account_id=a.id and r.id=1
    

    第一种写法,使用 join 关键字,本质上是采用的内连接(inner join)。

    第二种写法,不使用 join 关键字,本质上是采用交叉连接(cross join),也即生成两表的笛卡尔积,得到的记录相当于两表记录的乘积。

    以上两种写法查询结果是相同的,但推荐使用第一种,因为第一种性能比第二种高,特别是在有大表的情况下。

    理由是第二种交叉连接将产生更多的记录,然后通过 where 后的 r.account_id=a.id 条件过滤不需要的记录,而第一种内连接会直接过滤不需要的记录,所以执行效率更高。

    我们执行一下查询结果,如下:

    现在,我们回到 MyBatis 框架,看一下在 XML 映射文件中,如何实现关联查询映射。

    首先,我们需要建立 RoleEntity 实体类与 AccountEntity 实体类之间的关联关系,如下:

    public class RoleEntity {
        private int id;
        private String profession;
        private int rank;
        private int money;
        private AccountEntity account; //关联引用属性
        ...
    }
    

    这样,通过账号名查询的账号信息就可以映射到 account 属性中。注意 account 属性的 get 和 set 方法要记得添加上去,还有 toString 方法要重写一下,添加 account 属性的打印信息。

    现在,我们来编写映射文件中 SQL 语句映射,如下:

    <?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="mapper.GameMapper">
        <resultMap id="roleResultMap" type="entity.RoleEntity">
            <id property="id" column="id" />
            <result property="profession" column="profession" />
            <result property="rank" column="rank" />
            <result property="money" column="money" />
            <!-- association:用于映射关联查询单个对象信息
    		         property:将关联查询的账号信息映射到属性 account 上 -->
            <association property="account" javaType="entity.AccountEntity">
                <id property="id" column="id" />
                <result property="userName" column="user_name" />
                <result property="password" column="password" />
            </association>
        </resultMap>
    
        <select id="selectRoleById" resultMap="roleResultMap">
            select r.*,a.* from tb_role as r join tb_account as a on r.account_id=a.id where r.id=#{id}
        </select>
    </mapper>
    

    和之前单表映射相比,没什么太大差别,就是多了 association 子标记以及相应的内容罢了。

    意思很也简单,就是将关联查询结果中的账号信息,具体而言就是 a.id,a.user_name,a.password ,映射到 AccountEntity 实例中,也就是 RoleEntity.account 属性。

    最后,我们在 MyBatisTest 中添加一个单元测试方法,如下:

    		@Test
        public void selectRoleByAccountNameTest() {
            RoleEntity roleEntity = gameMapper.selectRoleById(1);
            System.out.println(roleEntity);
    
            Assert.assertNotNull(roleEntity);
        }
    

    执行测试,结果如下:

    2020-07-15 10:49:28,860 [main] [mapper.GameMapper.selectRoleById]-[DEBUG] ==>  Preparing: select r.*,a.id,a.user_name,a.password from tb_role as r join tb_account as a on r.account_id=a.id where r.id=? 
    2020-07-15 10:49:28,954 [main] [mapper.GameMapper.selectRoleById]-[DEBUG] ==> Parameters: 1(Integer)
    2020-07-15 10:49:28,985 [main] [mapper.GameMapper.selectRoleById]-[DEBUG] <==      Total: 1
    RoleEntity{id=1, profession='战士', rank=10, money=2000, account=AccountEntity{id=1, userName='潇洒哥', password='12345'}}
    

    这样,我们通过游戏角色ID查询到了游戏账号信息,并封装在 RoleEntity 对象的 account 属性中。

    作者:Binge
    本文版权归作者和博客园共有,转载必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
  • 相关阅读:
    CDN缓存
    nginx作用
    Linux下4个查找命令which、whereis、locate、find
    @ModelAttribute的用法,与@RequestBody的区别
    将kafka消息解析拷贝
    永久代溢出(java.lang.OutOfMemoryError: PermGen space )
    失败重试机制-递归应用1
    kafka-重复消费-2
    读写分离-延时问题-1
    UILabel处理html标签
  • 原文地址:https://www.cnblogs.com/binbingg/p/13751857.html
Copyright © 2011-2022 走看看