老生常谈 MyBatis 复杂查询

2022-07-25,,

一对一查询

在实际开发中,经常会遇到一对一查询,一对多查询等。这里我们先来看一对一查询。例如:每本书都有一个作者,作者都有自己的属性,根据这个,我来定义两个实体类:

public class book {
 
 private integer id;
 private string name;
 private author author;
	
 // 省略 getter/setter
}
public class author {
 
 private integer id;
 private string name;
 private integer age;

 // 省略 getter/setter
}

然后,在数据库中,添加两张表:

create database /*!32312 if not exists*/`test01` /*!40100 default character set utf8mb4 collate utf8mb4_general_ci */ /*!80016 default encryption='n' */;

use `test01`;

/*table structure for table `author` */

drop table if exists `author`;

create table `author` (
 `id` int(11) not null auto_increment,
 `name` varchar(255) collate utf8mb4_general_ci default null,
 `age` int(11) default null,
 primary key (`id`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_general_ci;

/*data for the table `author` */

/*table structure for table `book` */

drop table if exists `book`;

create table `book` (
 `id` int(11) not null auto_increment,
 `name` varchar(255) collate utf8mb4_general_ci default null,
 `aid` int(11) default null,
 primary key (`id`)
) engine=innodb default charset=utf8mb4 collate=utf8mb4_general_ci;

添加成功后,我们新建一个 bookmapper,bookmapper 中定义了一个查询 book 的方法,但是我希望查出来 book 的同时,也能查出来它的 author:

public interface bookmapper {
 
 book getbookbyid(integer id);
}

再定义一个 bookmapper.xml ,内容如下:

<?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.antonio.hello.mybatis.mapper.bookmapper">

 <resultmap id="bookwithauthor" type="com.antonio.hello.mybatis.entity.book">
  <id column="id" property="id"/>
  <result column="name" property="name"/>
  <association property="author" javatype="com.antonio.hello.mybatis.entity.author">
   <id column="aid" property="id"/>
   <result column="aname" property="name"/>
   <result column="aage" property="age"/>
  </association>
 </resultmap>

 <select id="getbookbyid" resultmap="bookwithauthor">
  select b.*,a.`age` as aage,a.`id` as aid,a.`name` as aname from book b,author a where b.`aid`=a.`id` and b.`id`=#{id}
 </select>
</mapper>

在这个查询 sql 中,首先应该做好一对一查询,然后,返回值一定要定义成 resultmap,注意,这里千万不能写错。然后,在 resultmap 中,来定义查询结果的映射关系。其中,association 节点用来描述一对一的关系。这个节点中的内容,和 resultmap 一样,也是 id,result 等,在这个节点中,我们还可以继续描述一对一。

由于在实际项目中,每次返回的数据类型可能都会有差异,这就需要定义多个 resultmap,而这多个 resultmap 中,又有一部份属性是相同的,所以,我们可以将相同的部分抽出来,做成一个公共的模板,然后被其他 resultmap 继承,优化之后的 mapper 如下:

<?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.antonio.hello.mybatis.mapper.bookmapper">

 <resultmap id="baseresultmap" type="com.antonio.hello.mybatis.entity.book">
  <id column="id" property="id"/>
  <result column="name" property="name"/>
 </resultmap>
 
 <resultmap id="bookwithauthor" type="com.antonio.hello.mybatis.entity.book" extends="baseresultmap">
  <association property="author" javatype="ocom.antonio.hello.mybatis.entity.author">
   <id column="aid" property="id"/>
   <result column="aname" property="name"/>
   <result column="aage" property="age"/>
  </association>
 </resultmap>

 <select id="getbookbyid" resultmap="bookwithauthor">
  select b.*,a.`age` as aage,a.`id` as aid,a.`name` as aname from book b,author a where b.`aid`=a.`id` and b.`id`=#{id}
 </select>
</mapper>

懒加载

上面这种加载方式,是一次性的读取到所有数据。然后在 resultmap 中做映射。如果一对一的属性使用不是很频繁,可能偶尔用一下,这种情况下,我们也可以启用懒加载。

懒加载:就是先查询 book,查询 book 的过程中,不去查询 author,当用户第一次调用了 book 中的 author 属性后,再去查询 author。例如,我们再来定义一个 book 的查询方法:

book getbookbyid2(integer id);

author getauthorbyid(integer id);

接下来,在 mapper 中定义相应的 sql:

<resultmap id="baseresultmap" type="com.antonio.hello.mybatis.entity.book">
 <id column="id" property="id"/>
 <result column="name" property="name"/>
</resultmap>

<resultmap id="bookwithauthor2" type="com.antonio.hello.mybatis.entity.book" extends="baseresultmap">
 <association property="author" javatype="com.antonio.hello.mybatis.entity.author"
     select="com.antonio.hello.mybatis.mapper.bookmapper.getauthorbyid" column="aid" fetchtype="lazy"/>
</resultmap>

<select id="getbookbyid2" resultmap="bookwithauthor2">
 select * from book where id=#{id};
</select>

<select id="getauthorbyid" resulttype="com.antonio.hello.mybatis.entity.author">
 select * from author where id=#{aid};
</select>

这里,定义 association 的时候,不直接指定映射的字段,而是指定要执行的方法,通过 select 字段来指定,column 表示执行方法时传递的参数字段,最后的 fetchtype 表示开启懒加载。当然,要使用懒加载,还需在全局配置中开启:

<settings>
 <setting name="lazyloadingenabled" value="true"/>
 <setting name="aggressivelazyloading" value="false"/>
</settings>

一对多查询

一对多查询,也是一个非常典型的使用场景。比如用户和角色的关系,一个用户可以具备多个角色。首先我们准备三个表:

set foreign_key_checks=0;

-- ----------------------------
-- table structure for role
-- ----------------------------
drop table if exists `role`;
create table `role` (
 `id` int(11) not null auto_increment,
 `name` varchar(32) default null,
 `namezh` varchar(32) default null,
 primary key (`id`)
) engine=innodb auto_increment=4 default charset=utf8;

-- ----------------------------
-- records of role
-- ----------------------------
insert into `role` values ('1', 'dba', '数据库管理员');
insert into `role` values ('2', 'admin', '系统管理员');
insert into `role` values ('3', 'user', '用户');

-- ----------------------------
-- table structure for user
-- ----------------------------
drop table if exists `user`;
create table `user` (
 `id` int(11) not null auto_increment,
 `username` varchar(32) default null,
 `password` varchar(255) default null,
 `enabled` tinyint(1) default null,
 `locked` tinyint(1) default null,
 primary key (`id`)
) engine=innodb auto_increment=4 default charset=utf8;

-- ----------------------------
-- records of user
-- ----------------------------
insert into `user` values ('1', 'root', '$2a$10$rmufxgq5ath4wovkuqyvuecpquseoxzyqilxzbz50dcersga.wyiq', '1', '0');
insert into `user` values ('2', 'admin', '$2a$10$rmufxgq5ath4wovkuqyvuecpquseoxzyqilxzbz50dcersga.wyiq', '1', '0');
insert into `user` values ('3', 'sang', '$2a$10$rmufxgq5ath4wovkuqyvuecpquseoxzyqilxzbz50dcersga.wyiq', '1', '0');

-- ----------------------------
-- table structure for user_role
-- ----------------------------
drop table if exists `user_role`;
create table `user_role` (
 `id` int(11) not null auto_increment,
 `uid` int(11) default null,
 `rid` int(11) default null,
 primary key (`id`)
) engine=innodb auto_increment=5 default charset=utf8;

-- ----------------------------
-- records of user_role
-- ----------------------------
insert into `user_role` values ('1', '1', '1');
insert into `user_role` values ('2', '1', '2');
insert into `user_role` values ('3', '2', '2');
insert into `user_role` values ('4', '3', '3');
set foreign_key_checks=1;

这三个表中,有用户表,角色表以及用户角色关联表,其中用户角色关联表用来描述用户和角色之间的关系,他们是一对多的关系。然后,根据这三个表,创建两个实体类:

public class user {
 private integer id;
 private string username;
 private string password;
 private list<role> roles;
	
 // 省略 setter/getter
}
public class role {
 private integer id;
 private string name;
 private string namezh;
		
 // 省略 setter/getter
}

接下来,定义一个根据 id 查询用户的方法:

user getuserbyid(integer id);

然后,定义该方法的实现:

<resultmap id="userwithrole" type="com.antonio.hello.mybatis.entity.user">
 <id column="id" property="id"/>
 <result column="username" property="username"/>
 <result column="password" property="password"/>
 <collection property="roles" oftype="com.antonio.hello.mybatis.entity.role">
  <id property="id" column="rid"/>
  <result property="name" column="rname"/>
  <result property="namezh" column="rnamezh"/>
 </collection>
</resultmap>

<select id="getuserbyid" resultmap="userwithrole">
 select u.*,r.`id` as rid,r.`name` as rname,r.`namezh` as rnamezh from user u,role r,user_role ur where u.`id`=ur.`uid` and ur.`rid`=r.`id` and u.`id`=#{id}
</select>

在 resultmap 中,通过 collection 节点来描述集合的映射关系。在映射时,会自动将一的一方数据集合并,然后将多的一方放到集合中,能实现这一点,靠的就是 id 属性。当然,这个一对多,也可以做成懒加载的形式,那我们首先提供一个角色查询的方法:

list<role> getrolesbyuid(integer id);

然后,在 xml 文件中,处理懒加载:

<resultmap id="userwithrole" type="com.antonio.hello.mybatis.entity.user">
 <id column="id" property="id"/>
 <result column="username" property="username"/>
 <result column="password" property="password"/>
 <collection property="roles" select="com.antonio.hello.mybatis.mapper.usermapper.getrolesbyuid" column="id" fetchtype="lazy">
 </collection>
</resultmap>

<select id="getuserbyid" resultmap="userwithrole">
 select * from user where id=#{id};
</select>

<select id="getrolesbyuid" resulttype="com.antonio.hello.mybatis.entity.role">
 select r.* from role r,user_role ur where r.`id`=ur.`rid` and ur.`uid`=#{id}
</select>

定义完成之后,我们的查询操作就实现了懒加载功能。

查询缓存

mybatis 一级缓存

mybatis 一级缓存的作用域是同一个 sqlsession,在同一个 sqlsession 中两次执行相同的 sql 语句,第一次执行完毕会将数据库中查询的数据写到缓存(内存),第二次会从缓存中获取数据将不再从数据库查询,从而提高查询效率。当一个 sqlsession 结束后该 sqlsession 中的一级缓存也就不存在了。mybatis 默认开启一级缓存。

public class main2 {
 public static void main(string[] args) {
  sqlsessionfactory instance = sqlsessionfactoryutils.getinstance();
  sqlsession sqlsession = instance.opensession();
  bookmapper mapper = sqlsession.getmapper(bookmapper.class);
  usermapper usermapper = sqlsession.getmapper(usermapper.class);
  user user = usermapper.getuserbyid(1);
  user = usermapper.getuserbyid(1);
  user = usermapper.getuserbyid(1);
  system.out.println(user.getusername());
 }
}

多次查询,只执行一次 sql。但是注意,如果开启了一个新的 sqlsession,则新的 sqlsession 无法就是之前的缓存,必须是同一个 sqlsession 中,缓存才有效。

mybatis 二级缓存

mybatis 二级缓存是多个 sqlsession 共享的,其作用域是 mapper 的同一个 namespace,不同的 sqlsession 两次执行相同 namespace 下的 sql 语句且向 sql 中传递参数也相同即最终执行相同的 sql 语句,第一次执行完毕会将数据库中查询的数据写到缓存(内存),第二次会从缓存中获取数据将不再从数据库查询,从而提高查询效率。mybatis 默认没有开启二级缓存需要在 setting 全局参数中配置开启二级缓存。

到此这篇关于老生常谈 mybatis 复杂查询的文章就介绍到这了,更多相关mybatis 复杂查询内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

《老生常谈 MyBatis 复杂查询.doc》

下载本文的Word格式文档,以方便收藏与打印。