MyBatis——19实现多表查询&resultMap&AutoMapping

MyBatis实现多表查询

  1. Mybatis 实现多表查询方式
    第一种:
    业务装配,即是对两个表编写单表查询语句,在业务(Service)把查询的两个结果进行关联.
    第二种:
    使用AutoMapping特性,在实现两表联合查询时通过别名完成映射
    第三种:
    使用 MyBatis 的 resultMap 标签属性进行实现
    注:多表查询时,类中包含另一个类的对象的分类,分为单个对象和集合对象

resultMap 标签的属性

  1. resultMap 标签属性写在mapper.xml中,由程序员控制SQL查询结果与实体类的映射关系
    注:默认 MyBatis 使用 AutoMapping 特性
  2. 使用 resultMap 标签属性时,select 标签不写 resultType 属性,而是使用 resultMap 属性引用resultMap标签

使用 resultMap 实现单表映射关系

  1. 数据库设计
    id和name
  2. 实体类设计
public class Teacher{
	private int id1;
	private String name1;
}
  1. mapper.xml 代码
<resultMap type="teacher" id="mymap">
<!-- 主键使用 id 标签配置映射关系 --> 
<id column="id" property="id1" />
<!-- 其他列使用 result 标签配置映射关系 -->
<result column="name" property="name1"/> </resultMap>
<select id="selAll" resultMap="mymap"> 
select * from teacher 
</select>

使用 resultMap 实现关联单个对象

一.使用 resultMap 实现关联单个对象(N+1 方式)

  1. N+1的查询方式是先查询出某个表的全部信息,根据这个表的信息查询另一个表的信息
  2. 与业务装配的区别:
    在 service 里面写的代码,由 mybatis 完成装配

3. 实现步骤:

  1. 在 Student 实现类中包含了一个 Teacher 对象
public class Student 
{ 
private int id; 
private String name; 
private int age; 
private int tid; 
private Teacher teacher;
}
  1. 在 TeacherMapper 中提供一个查询
<select id="selById" resultType="teacher" parameterType="int"> 
select * from teacher where id=#{0} 
</select>

3.在 StudentMapper 中
①association 标签装配一个对象时使用
②property: 对象在类中的属性名
③select:通过哪个查询查询出这个对象的信息
④column: 把当前表的哪个列的值做为参数传递给另一个查询
⑤大前提使用 N+1 方式,如果列名和属性名相同可以不配置,使用 Automapping 特性.但是 mybatis 默认只会给列专配一次

<resultMap type="student" id="stuMap"> 
	<id property="id" column="id"/> 
	<result property="name" column="name"/> 
	<result property="age" column="age"/> 
	<result property="tid" column="tid"/>
<!-- 如果关联一个对象 --> 
<association property="teacher" select="com.youdian.mapper.TeacherMapper.selById" column="tid"></association> 
</resultMap> 
<select id="selAll" resultMap="stuMap"> 
select * from student 
</select>

⑥把上面代码简化成

<resultMap type="student" id="stuMap"> 
	<result column="tid" property="tid"/>
	<!-- 如果关联一个对象 --> 
	<association property="teacher" select="com.youdian.mapper.TeacherMapper.selById" column="tid"></association> 
</resultMap> 
<select id="selAll" resultMap="stuMap"> 
select * from student 
</select>

4. N+1名称由来

  1. 举例:学生中有 3 条数据
  2. 需求:查询所有学生信息级授课老师信息
  3. 需要执行的 SQL 命令
    {
    查询全部学生信息:select*from 学生
    执行 3 遍 select * from 老师 where id=学生的外键
    }
  4. 使用多条 SQl 命令查询两表数据时,如果希望把需要的数据都查询出来,需要执行 N+1 条 SQl 才能把所有数据库查询出来
  5. 缺点:效率低
  6. 优点:
    如果有的时候不需要查询学生时,同时查询老师,只需要执行一个 select * from student;
  7. 适用场景: 有的时候需要查询学生同时查询老师,有的时候只需要查询学生
  8. 如何解决 N+1 查询带来的效率低的问题?
    ①默认带的前提: 每次都是两个都查询
    ②使用两表联合查询

二.使用 resultMap 实现关联单个对象(联合查询方式)

  1. 只需要编写一个 SQL,在 StudentMapper 中添加下面效果
<association/>只要专配一个对象就用这个标签
此时把<association/>小的<resultMap>看待
javaType 属性:<association/>专配完后返回一个什么类型的对象.取值是一个类(或类的别名) 
------------------------------------------------------------------------------------------------------
<resultMap type="Student" id="stuMap1"> 
	<id column="sid" property="id"/> 
	<result column="sname" property="name"/> 
	<result column="age" property="age"/> 
	<result column="tid" property="tid"/> 
	<association property="teacher"javaType="Teacher" > 
	<id column="tid" property="id"/> 
	<result column="tname" property="name"/> 
	</association> 
</resultMap> 
	<select id="selAll1" resultMap="stuMap1"> 
	select s.id sid,s.name sname,age age,t.id tid,t.name tname from student s left outer join teacher t on s.tid=t.id 
	</select>

N+1 方式和联合查询方式对比

  1. N+1:需求不确定时
  2. 联合查询:需求中确定查询时两个表一定都查询

使用 resultMap 标签查询关联集合对象(N+1)

  1. 在 Teacher 中添加 List集合
public class Teacher 
{ 
private int id; 
private String name; 
private List<Student> list;
}
  1. 在 StudentMapper.xml 中添加通过 tid 查询
<select id="selByTid" parameterType="int" resultType="student"> 
select * from student where tid=#{0} 
</select>
  1. 在 TeacherMapper.xml 中添加查询全部
<collection/> 是当属性是集合类型时使用的标签
-----------------------------------------------------------------------------------------------------
<resultMap type="teacher" id="mymap"> 
	<id column="id" property="id"/> 
	<result column="name" property="name"/> 
<collection property="list" select="com.youdian.mapper.StudentMapper.selByTid" column="id"></collection> 
</resultMap> 
<select id="selAll" resultMap="mymap"> 
select * from teacher 
</select>

使用 resultMap 标签实现加载集合数据(联合查询方式)

1.在 teacherMapper.xml 中添加
mybatis可以通过主键判断对象是否被加载过,不需要担心创建重复 Teacher

<resultMap type="teacher" id="mymap1"> 
	<id column="tid" property="id"/> 
	<result column="tname" property="name"/>
<collection property="list" ofType="student" > 
	<id column="sid" property="id"/> 
	<result column="sname" property="name"/> 
	<result column="age" property="age"/> 
	<result column="tid" property="tid"/> 
</collection> 
</resultMap> 
<select id="selAll1" resultMap="mymap1"> 
select t.id tid,t.name tname,s.id sid,s.name sname,age,tid from teacher t LEFT JOIN student s on t.id=s.tid; 
</select>

使用 AutoMapping 结合别名实现多表查询

  1. 只能使用多表联合查询方式.
  2. 要求:查询出的列别和属性名相同.
  3. 实现方式
    在 SQL 是关键字符的解决方法是:两侧添加反单引号
<select id="selAll" resultType="student">
select t.id `teacher.id`,t.name `teacher.name`,s.id id,s.name name,age,tid from student s left join teacher t on t.id=s.tid 
</select>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于您的问题,我可以帮您解答。针对springboot整合mybatis-plus实现多表分页查询实现,可以按照以下步骤进行: 1.在pom.xml文件中添加Mybatis-Plus和Pagehelper的依赖,如下: ``` <!-- Mybatis-Plus依赖 --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- Pagehelper依赖 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.13</version> </dependency> ``` 2.在Mybatis-Plus的配置文件中,指定分页插件。如下: ``` @Configuration @MapperScan("com.example.mapper") public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInterceptor() { return new PaginationInterceptor(); } } ``` 3.编写Mapper和对应的Mapper.xml文件,进行多表联合查询,并在Mapper接口方法上添加分页参数。如下: ``` //在Mapper接口方法上添加分页参数 public interface UserMapper extends BaseMapper<User> { List<User> selectUserPage(Page<User> page); } <!-- 在Mapper.xml中编写多表联合查询SQL语句 --> <select id="selectUserPage" resultMap="BaseResultMap"> select u.*, r.role_name from user u left join user_role ur on u.id = ur.user_id left join role r on ur.role_id = r.id <where> <if test="username != null and username != ''"> and u.username like concat('%',#{username},'%') </if> </where> </select> ``` 4.在Controller层中,接受分页参数并返回分页结果。如下: ``` @RestController public class UserController { @Autowired private UserMapper userMapper; @GetMapping("/users") public Page<User> selectUserPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size, String username) { Page<User> p = new Page<>(page, size); p = userMapper.selectUserPage(p, username); return p; } } ``` 以上就是整合Mybatis-Plus和Pagehelper实现多表分页查询的具体步骤,希望能对您有所帮助!如果您有其他问题,欢迎继续提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值