由于是练习,故只做了感兴趣的一部分测试。

测试类容
XML配置转注解方式

实体类
为了测试请忽略设计是否合理…
User.java

@Alias("User")
public class User {
    private Integer id;
    private String username;
    private String password;
    private Timestamp birthday;
    private Boolean sex;
}
1
2
3
4
5
6
7
8
UserInfo.java

@Alias("UserInfo")
public class UserInfo {
    private Integer id;
    private User user;
    private String name;
}
1
2
3
4
5
6
UserMessage.java

@Alias("UserMessage")
public class UserMessage {
    private Integer userId;
    private UserInfo userInfo;
    private List<Message> messages;
}
1
2
3
4
5
6
Message.java

@Alias("Message")
public class Message {
    private Integer id;
    private Integer userId;
    private String content;
    private Timestamp time;
}
1
2
3
4
5
6
7
三表之间的关联为 userId

XML配置方式
XML配置

<select id="selectUserInfoAndMessages" resultMap="UserMessages">
    select
    ui.id as uiid,
    ui.name as name,
    ui.user_id as info_user_id,
    u.id as user_id,
    u.username
    as username,
    u.password as password,
    u.birthday as birthday,
    u.sex as
    sex,
    um.id as umid,
    um.user_id as um_user_id,
    um.content as content,
    um.time as um_time
    from user_info ui
    left outer join user u on u.id = ui.user_id
    left outer join user_message um
    on u.id = um.user_id
    where u.id = #{id}
</select>
<resultMap type="UserMessage" id="UserMessages">
    <id property="userId" column="user_id" />
    <association property="userInfo" column="info_user_id"
        javaType="UserInfo">
        <id property="id" column="uiid" />
        <result property="name" column="name" />
        <association property="user" column="user_id" javaType="User">
            <id property="id" column="user_id" />
            <result property="username" column="username" />
            <result property="password" column="password" />
            <result property="birthday" column="birthday" />
            <result property="sex" column="sex" />
        </association>
    </association>
    <collection property="messages" javaType="ArrayList"
        ofType="Message">
        <id property="id" column="umid" />
        <result property="userId" column="user_id" />
        <result property="content" column="content" />
        <result property="time" column="um_time" />
    </collection>
</resultMap>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
查询方法

public List<UserMessage> testSelectUserInfoAndMessages(Integer userId) {
    try (SqlSession session = sessionFactory.openSession()) {
        //映射器方式调用
        //return session.getMapper(UserMapper.class).selectUserInfoAndMessage(userId);
        return session.selectList("mybatis.dao.mappers.UserMapper.selectUserInfoAndMessages", userId);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
1
2
3
4
5
6
7
8
9
10
执行过程

==>  Preparing: select ui.id as uiid, ui.name as name, ui.user_id as info_user_id, u.id as user_id, u.username as username, u.password as password, u.birthday as birthday, u.sex as sex, um.id as umid, um.user_id as um_user_id, um.content as content, um.time as um_time from user_info ui left outer join user u on u.id = ui.user_id left outer join user_message um on u.id = um.user_id where u.id = ?
==> Parameters: 9(Integer)
<==    Columns: uiid, name, info_user_id, user_id, username, password, birthday, sex, umid, um_user_id, content, um_time
<==        Row: 5, 江流, 9, 9, test中文2, 1234567, 2018-07-17 17:04:09.0, 0, 1, 9, 信息1, 2018-07-18 11:36:58.0
<==        Row: 5, 江流, 9, 9, test中文2, 1234567, 2018-07-17 17:04:09.0, 0, 2, 9, 信息2, 2018-07-18 11:37:05.0
<==      Total: 2
1
2
3
4
5
6
结果

[UserMessage [userId=9, userInfo=UserInfo [id=5, user=User [id=9, username=test中文2, password=1234567, birthday=2018-07-17 17:04:09.0, sex=false], name=江流], messages=[Message [id=1, userId=9, content=信息1, time=2018-07-18 11:36:58.0], Message [id=2, userId=9, content=信息2, time=2018-07-18 11:37:05.0]]]]
1
此种方式只查询了一次,结果集被过滤了重复项目,最终集合中只有一个UserMessage对象。

注解方式
注解方法

@Select("select * from user where id = #{userId}")
@Results(value = {
        @Result(property="userId" , column="id"),
        @Result(property="userInfo" , column="id", one = @One(select="mybatis.dao.mappers.UserMapper.getUserInfoByAnnotation")),
        @Result(property="messages" , column = "id" , many= @Many(select="mybatis.dao.mappers.UserMapper.getUserMessagesByAnnotation"))
})
public List<UserMessage> selectUserInfoAndMessagesByAnnotation(Integer userId);//查询用户信息和用户消息

@Select("select * from user_info where user_id = #{userId}")
@Results(value={
        @Result(property="id" , column="id"),
        @Result(property="name" , column = "name"),
        @Result(property="user" , column = "user_id" , one = @One(select="mybatis.dao.mappers.UserMapper.getUserByAnnotation"))
})
public UserInfo getUserInfoByAnnotation(Integer userId);//查询用户信息

@Select("select * from user where id = #{userId}")
public User getUserByAnnotation(Integer userId);//查询用户

@Select("select * from user_message where user_id = #{userId}")
public List<Message> getUserMessagesByAnnotation(Integer userId);//查询用户消息集合
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
查询方法

public List<UserMessage> testSelectUserMessagesByAnnotation(Integer userId) {
    try (SqlSession session = sessionFactory.openSession()) {
        return session.getMapper(UserMapper.class).selectUserInfoAndMessagesByAnnotation(userId);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
1
2
3
4
5
6
7
8
执行过程

==>  Preparing: select * from user where id = ?
==> Parameters: 9(Integer)
<==    Columns: id, username, password, sex, birthday
<==        Row: 9, test中文2, 1234567, 0, 2018-07-17 17:04:09.0
====>  Preparing: select * from user_info where user_id = ?
====> Parameters: 9(Integer)
<====    Columns: id, user_id, name
<====        Row: 5, 9, 江流
======>  Preparing: select * from user where id = ?
======> Parameters: 9(Integer)
<======    Columns: id, username, password, sex, birthday
<======        Row: 9, test中文2, 1234567, 0, 2018-07-17 17:04:09.0
<======      Total: 1
<====      Total: 1
====>  Preparing: select * from user_message where user_id = ?
====> Parameters: 9(Integer)
<====    Columns: id, user_id, content, time
<====        Row: 1, 9, 信息1, 2018-07-18 11:36:58.0
<====        Row: 2, 9, 信息2, 2018-07-18 11:37:05.0
<====      Total: 2
<==      Total: 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
可以看出执行了几次查询语句。

结果

[UserMessage [userId=9, userInfo=UserInfo [id=5, user=User [id=9, username=test中文2, password=1234567, birthday=2018-07-17 17:04:09.0, sex=false], name=江流], messages=[Message [id=1, userId=9, content=信息1, time=2018-07-18 11:36:58.0], Message [id=2, userId=9, content=信息2, time=2018-07-18 11:37:05.0]]]]
1
总结
XML执行联合查询效率是要高一些的
注解方式方法倒是可以独立使用,虽然XML也可以用select属性
混合使用更好
套用官方的话

1

因为最初设计时,MyBatis 是一个 XML 驱动的框架。配置信息是基于 XML 的,而且映射语句也是定义在 XML 中的。而到了 MyBatis 3,就有新选择了。MyBatis 3 构建在全面且强大的基于 Java 语言的配置 API 之上。这个配置 API 是基于 XML 的 MyBatis 配置的基础,也是新的基于注解配置的基础。注解提供了一种简单的方式来实现简单映射语句,而不会引入大量的开销。

2

注意 不幸的是,Java 注解的的表达力和灵活性十分有限。尽管很多时间都花在调查、设计和试验上,最强大的 MyBatis 映射并不能用注解来构建——并不是在开玩笑,的确是这样。比方说,C#属性就没有这些限制,因此 MyBatis.NET 将会比 XML 有更丰富的选择。也就是说,基于 Java 注解的配置离不开它的特性。

END

附 数据表SQL

CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(25) DEFAULT NULL,
  `password` varchar(25) DEFAULT NULL,
  `sex` bit(1) DEFAULT b'1',
  `birthday` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

CREATE TABLE `user_info` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `name` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `user_info_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

CREATE TABLE `user_message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) DEFAULT NULL,
  `content` varchar(255) DEFAULT NULL,
  `time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `message_ibfk_1` (`user_id`),
  CONSTRAINT `user_message_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
---------------------
作者:很倔也很天真
来源:CSDN
原文:https://blog.csdn.net/wang845252276/article/details/81120504
版权声明:本文为博主原创文章,转载请附上博文链接!

Mybatis 联合查询XML与注解对比的更多相关文章

  1. MyBatis联合查询和使用association 进行分步式查询

    查询Emp的同时,查出emp对应的部门Department 方法1:联合查询,使用级联属性封装结果集 <!-- 联合查询,使用级联属性封装结果集 type:要自定义规则的javaBean类型 i ...

  2. Mybatis联合查询(一)

    Mybatis的简单联合查询操作: 实体类: Employee: package com.test.mybatis; public class Employee { private Integer i ...

  3. SQL 联合查询 + XML解析

    (select a.EBILLNO, a.EMPNAME, a.APPLYDATE, b.HS_NAME, ), ),'') as SUMMARY, cast(c.XmlData as XML).va ...

  4. MyBatis联合查询association使用

    1.需求 两张表 channels(频道表)  member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...

  5. mybatis联合查询

    1.有学生实体 @Component @Scope("prototype") public class StudentInfo { private Integer studentI ...

  6. Mybatis联合查询记录,左连接参数操作

    公司业务需求要做个列表的排序 而实际排序的字段不再本库中,需要跨库去拿到字段,因为是微服务体系架构,不可能Left join跨库的表,所以决定调用一次跨服务的API拿到排序相关的对象,里面包含需要排序 ...

  7. mybatis 同时使用 XML 和注解

    引言 学过 MyBatis 框架的童靴都知道, MyBatis 支持 XML 和注解两种方式配置,如下: 使用 XML 方式配置 只写明 XML 的 resource 路径(或者URL路径) < ...

  8. Mybatis中@select注解联合查询

    前言 在项目中经常会使用到一些简单的联合查询获取对应的数据信息,我们常规都是会根据对应的mapper接口写对应的mapper.xml的来通过对应的业务方法来调用获取,针对这一点本人感觉有点繁琐,就对@ ...

  9. mybatis:开发环境搭建--增删改查--多表联合查询(多对一)

    什么是mybatisMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XML或 ...

随机推荐

  1. mysql中的分区

    第18章:分区 目录 18.1. MySQL中的分区概述 18.2. 分区类型 18.2.1. RANGE分区 18.2.2. LIST分区 18.2.3. HASH分区 18.2.4. KEY分区 ...

  2. linux中部署django项目

    通过Nginx部署Django(基于ubuntu) Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 在这种方式中,我们的通常做法是,将nginx作为服务器 ...

  3. 浅谈矩阵变换——Matrix

    矩阵变换在图形学上经常用到.基本的常用矩阵变换操作包括平移.缩放.旋转.斜切. 每种变换都对应一个变换矩阵,通过矩阵乘法,可以把多个变换矩阵相乘得到复合变换矩阵. 矩阵乘法不支持交换律,因此不同的变换 ...

  4. Vue入门(二)——Demo

    1.在工程目录下创建项目 右键GIT BASH 2.安装lib(建议使用淘宝镜像安装) 3.首页 App.vue <template> <el-container> <e ...

  5. 用cmd 如何输入命令,进入文件夹

    用cmd 如何输入命令 进入文件夹 盘符: 例如想进入D盘 d: cd 进入到当前盘某个目录.cd \ 进入当前盘根目录cd \windows 进入到当前盘Windows目录cd.. 退出到上一级目录 ...

  6. 信息: JSF1048:有 PostConstruct/PreDestroy 注释。标有这些注释的 ManagedBeans 方法将表示注释已处理。

    在Myeclipse运行项目时,控制台会输出如下信息,但是项目正常运行,没有异常,还不知道怎么解决 信息: JSF1048:有 PostConstruct/PreDestroy 注释.标有这些注释的 ...

  7. k8s部署dashboard

    1.首先去github上找到kubernetes 2.然后找到get started 3.复制yaml文件地址,并wget到服务器上并部署即可 PS:本文把自己部署的yaml文件贴出来:recomme ...

  8. IVIEW组件Table中加入EChart柱状图

    展示图如下: 主要利用了render函数和updated()钩子函数进行数据填充与渲染. 1.在Table的Colums中加入 1 { 2 title: '比例图', 3 align: 'center ...

  9. Restorator 2018 v3.90汉化注册版 含注册码汉化激活教程

    链接:https://pan.baidu.com/s/1wfh5VQfNgbDr-glezL4faQ 提取码:y507

  10. python 输出三角形

    pattern = input("请输入你要选择的模式:") while True: if pattern == "A": row = eval(input(& ...