Mybatis联表查询

1 使用注解方式

1.1 一对一

ClazzModel.java (返回前端的模型)

1
2
3
4
5
6
7
8
9
10
public class ClazzModel {

private Long id;
private String name;
private String description;
private Grade grade; //联表
private User user;// 联表
//setter、getter

}

ClazzMapper.java

1
2
3
4
5
6
7
8
@Select("select * from poll_clazz")
@Results({
//一对一 one = @One()
@Result(property="id",column="id"),//不加这个查出来的id会为null
@Result(property = "grade",column = "grade_id",one = @One(select="com.pibigstar.dao.GradeMapper.selectByPrimaryKey")),
@Result(property = "user",column = "user_id",one = @One(select="com.pibigstar.dao.UserMapper.selectByPrimaryKey"))
})
List<ClazzModel> findAll();

1.2 一对多

QuestionModel.java

1
2
3
4
5
6
7
8

public class QuestionModel{
private Long id;
private String name;
private String questionType;
private List<Options> options;
//省略 setter、getter方法
}

QuestionMapper.java

1
2
3
4
5
6
7
    @Select("select * from poll_questions")
@Results({
// 一对多 many = @Many()
@Result(property="id",column="id"),//不加这个查出来的id会为null
@Result(property="options",column="id",many=@Many(select="com.pibigstar.dao.OptionsMapper.findAllByQuestionId"))
})
List<QuestionModel> findAll();

1.3 多对多

业务介绍: 有一张问卷表(questionnaire),一张题库表(question),一张问卷与题库的桥表(qq)

希望能查出所有的问卷,每个问卷有对应的题

思路:我们先查出所有的问卷,然后通过问卷id 在桥表中查找出此问卷中的所有问题

QuestionnaireModel.java

1
2
3
4
5
6
7
public class QuestionnaireModel{
private Long id;
private String name;
private String description;
private List<QuestionModel> questions;
//省略setter、getter方法
}

QuestionnaireMapper.java

1
2
3
4
5
6
@Select("select * from poll_questionnaire")
@Results({
@Result(property="id",column="id"),
@Result(property="questions",column="id",many=@Many(select="com.pibigstar.dao.QQMapper.selectAllQuestions")),
})
List<QuestionnaireModel> findAllModel();

QQMapper.java

1
2
3
4
5
6
@Select("select * from poll_questions where id in (select questions_id from poll_qq where questionnaire_id=#{id})")
@Results({
@Result(property="id",column="id"),
@Result(property="options",column="id",many=@Many(select="com.pibigstar.dao.OptionsMapper.findAllByQuestionId"))
})
List<QuestionModel> selectAllQuestions(Long id);

1.4 注意:

分步查询有时候非常浪费数据库资源,我们可以采用懒加载方式,当有需要的时候才进行分步查询
使用:fetchType=FetchType.LAZY

1
2
3
4
5
6
7
8

@Select("select * from poll_clazz")
@Results({
//一对一 one = @One()
@Result(property="id",column="id"),//不加这个查出来的id会为null
@Result(property = "grade",column = "grade_id",one = @One(fetchType=FetchType.LAZY,select="com.pibigstar.dao.GradeMapper.selectByPrimaryKey")),

List<ClazzModel> findAll();

2 使用xml配置方式

2.1 一对一

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.briup.apps.poll.dao.extend.ClazzVMMapper">

<resultMap id="clazzVMResultMap" type="com.briup.apps.poll.bean.extend.ClazzVM">
<id column="id" jdbcType="BIGINT" property="id" />
<!-- 联表查询 association -->
<association column="grade_id" property="grade" select="com.briup.apps.poll.dao.GradeMapper.selectByPrimaryKey"></association>
<association column="charge_id" property="charge" select="com.briup.apps.poll.dao.UserMapper.selectByPrimaryKey"></association>
</resultMap>

<select id="selectById" parameterType="java.lang.Long" resultMap="clazzVMResultMap">
select * from poll_clazz where id=#{id};
</select>

</mapper>

2.2 一对多

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
<resultMap type="com.User" id="userAndItemsResultMap">
<!-- 用户信息 -->
<id column="user_id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="address" property="address"/>
<!-- 一个用户对应多个订单 -->
<collection property="ordersList" ofType="com.Orders">
<id column="id" property="id"/>
<result column="user_id" property="userid"/>
<result column="number" property="number"/>
<result column="createtime" property="createTime"/>
<result column="note" property="note"/>
<!-- 一个订单对应多个订单明细 -->
<collection property="orderdetails" ofType="com.OrderDetail">
<id column="orderdetail_id" property="id"/>
<result column="items_id" property="itemsId"/>
<result column="items_num" property="itemsNum"/>
<result column="orders_id" property="ordersId"/>
<!-- 一个订单明细对应一个商品 -->
<association property="items" javaType="com.Items">
<id column="items_id" property="id"/>
<result column="items_name" property="itemsName"/>
<result column="items_detail" property="detail"/>
<result column="items_price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
-------------本文结束感谢您的阅读-------------