Mybatis多表联查的几种办法

前言

开发过程中,对数据库多表连接查询是常规操作。Mybatis(plus)在多表连接查询上提供了很好的支持。通常,表与表之间存在四种映射关系:一对一映射、多对一映射、一对多映射、多对多映射。在处理的过程中,多对一和一对一映射一般采用同一种操作方式,一对多和多对多映射一般采用同一种操作方式。而且,对于Mybatis的运用,我们通常使用xml的方式。因此,本文主要介绍xml方式下一对一多表联查和一对多多表联查。

一、一对一多表联查

1.1 两表联查

在数据库中建立学生表专业表两张数据表,并假设一个学生只有一个专业。表结构大致如下

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (`id` int(11) NOT NULL AUTO_INCREMENT,`name` varchar(20) DEFAULT NULL,`birthday` date DEFAULT NULL,`phonenum` varchar(20) DEFAULT NULL,`mid` int(11) NOT NULL,PRIMARY KEY (`stu_id`) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;DROP TABLE IF EXISTS `major`;
CREATE TABLE `major` (`mid` int(11) NOT NULL AUTO_INCREMENT,`mname` varchar(255) DEFAULT NULL,PRIMARY KEY (`oid`) USING BTREE,
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC;

在运用Mybatis进行数据查询前,需要先建立实体类模型。在实体类建立的过程中,应当注意:在假设前提下,每名学生有一个专业,在类属性中需包含专业对象。学生实体类大致如下:

public class Student {private Integer id;private String name;private Date birthday;private String phonenum;private Major major;/*此处省略get、set、toString以及构造函数*/
}
public class Major{/*暂不考虑专业与学生的对应关系*/private Integer mid;private String mname;/*此处省略get、set、toString以及构造函数*/
}

分别在StudentMapper接口和MajorMapper接口中添加方法

public interface StudentMapper {//按id查询Student信息public Student findStudentById(Integer id);
}
public interface MajorMapper {//按id查询专业信息public Major findMajorById(Integer mid);
}

1.1.1 方法一:基于嵌套select的查询

关键来了,我们需要在StudentMapper.xml中的结果映射集中,使用标签来建立学生与专业的对应关系。

DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.StudentMapper"><resultMap id="stuResult" type="com.domain.Student" autoMapping="true"><id property="id" column="id">id><result colunm="name" property="name" /><result colunm="birthday" property="birthday" /><result colunm="phonenum" property="phonenum" /><association property="major" javaType="major" column="mid"  select="com.mapper.MajorMapper.findMajorById" fetchType="eager" >association>resultMap><select id="findStudentByid" resultMap="stuResult">select * from student where id = #{id}select>
mapper>

MajorMapper.xml文件如下

DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.MajorMapper"><resultMap id="majResult" type="com.domain.Major" autoMapping="true"><id property="mid" column="mid">id><result colunm="mname" property="mname" />resultMap><select id="findMajorById" resultMap="majResult">select * from student where mid = #{mid}select>
mapper>

此时,我们调用方法,就能实现两表联查

Student stu = studentMapper.findStudentById(1);

说明:标签中的fetchType为是否启用延迟加载(eager为立即加载,lazy为延迟加载)

1.1.2 方法二:基于join语句的查询

考虑到基于select的两表联查,一次查询需要两条语句。并且与延迟加载一同使用将会产生严重的性能缺陷。下面我们介绍基于join语句的两表联查。

基于join语句的查询,与基于select的查询主要区别在mapper文件,代码如下

DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.StudentMapper"><resultMap id="stuResult" type="com.domain.Student" autoMapping="true"><result colunm="name" property="name" /><result colunm="birthday" property="birthday" /><result colunm="phonenum" property="phonenum" /><association property="major" javaType="major" resultMap="com.mapper.MajorMapper.majResult">association>resultMap><select id="findStudentByid" resultMap="stuResult">select s.id id, s.name name, s.birthday birthday, s.phonenum phonenum, s.mid midfrom student sjoin major mon s.mid = m.midwhere s.id = #{id}select>
mapper>

1.2 Mybatis两张表以上多表联查

对于Mybatis两张表以上的多表联查方法,为以上两种方式的拓展

例如:学生还与家庭住址存在一对一关系,那么我们只需要在实体类中增加地址的实体类对象作为属性,在StudentMapper.xml文件的标签中按照方法一、方法二各自的形式,增加相应的标签,修改select语句,即可实现多表联查。但基于select嵌套的查询存在一定的性能缺陷,而基于join 的查询需要书写较为复杂的SQL语句,因此各有优劣,一般推荐使用基于join的查询方式

二、一对多的多表联查

一对多的多表查询方法,与一对一查询的两种方法较为相似。不同的地方在于(此处假定一个专业对应多名学生):1.一对多情况下,实体类中需要声明对应多个实体的集合属性,如Major实体类中需声明List属性,而Student实体类中声明的是Major属性(详见1.1中Student实体类定义);2.在MajorMapper.xml文件内的标签内部增加标签,而StudentMapper.xml相应位置增加的是标签

以基于join语句的查询为例,Major实体类修改为

public class Major{private Integer mid;private String mname;/*建立关联关系*/private List<Student> students;/*此处省略get、set、toString以及构造函数*/
}

MajorMapper.xml修改为

DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.MajorMapper"><resultMap id="majResult" type="com.domain.Major" autoMapping="true"><id property="mid" column="mid">id><result colunm="mname" property="mname" /><collection property="students" javaType="ArrayList" ofType="student" resultMap="com.mapper.StudentMapper.stuResult" /> resultMap><select id="findMajorById" resultMap="majResult">select m.*,s.*from major mjoin student son m.mid = s.midwhere m.mid = #{mid}select>
mapper>

对于基于select的查询在此不在过多介绍,可参考上文举一反三

三、基于Mybatis-plus的多表联查

众所周知,Mybatis-plus在Mybatis的基础上只做增强,不做修改。因此,以上的方法同样适用。但是,plus可以说将原先的半ORM增强为了真正的ORM,使得对于单表的操作更加方便快捷。基于此,plus的多表查询除以上方法外,还开考虑将单表查询进行组合运用,从而达到多表查询的效果,这有点类似于基于select的查询方法

总结

我个人更偏向于基于join语句的查询方式,在这种方式下,我们可以结合Mybatis的动态SQL编写出强大的数据库交互代码,具有很强的灵活性。另外,如果项目需求较为简单,运用Mybatis-plus搭建项目也是不错的选择


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部