http://xiangai.taobao.com
http://shop148612228.taobao.com

MyBatis中的collection两种使用方法

表关系: 问题表 1==>n 问题选项表,
需求: 查询问题时候,联查出来问题选项

//问题 实体类
public class Question {
private String id; //ID
private String content; //问题
private String type; //问题类型 1:单选,2:多选,3:问答
private Integer sort; //排序
private List<QuestionOption> options; //问题选项 *** 问题表里不需要有这个属性对应的字段
//...
}
1
2
3
4
5
6
7
8
9
//问题选项 实体类
public class QuestionOption{
private String id; //ID
private String qid; //问题ID *** 问题选项表里需要有这个属性对应的字段
private String content; //选项
private Integer sort; //排序
//...
}
1
2
3
4
5
6
7
8
方式一:
代码复用性高, 主表分页查询正确
1
QuestionMapper.xml

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionMapper">

<resultMap id="BaseResultMap" type="com.xxx.modules.xxx.entity.Question" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="content" property="content" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="VARCHAR" />
<result column="sort" property="sort" jdbcType="INTEGER" />
<collection property="options" javaType="java.util.ArrayList" ofType="com.xxx.modules.xxx.entity.QuestionOption"
select="com.xxx.modules.xxx.mapper.QuestionOptionMapper.selectList" column="{qid=id,sort=sort}" />
<!-- qid/sort是定义的变量名, id/sort是主表的字段id/sort,
先查出主表的结果, 然后主表记录数是几 就执行几次 collection 的select,
javaType和ofType 写不写都行,
select的值: 对应xml的namespace + 对应xml中的代码片段的id,
column作为select语句的参数传入,如果只传一个参数id可以简写: column="id" -->
</resultMap>

<!-- 查询列表 -->
<select id="selectList" resultMap="BaseResultMap">
SELECT
pq.id, pq.content, pq.type, pq.sort
FROM
question AS pq
<where>
</where>
</select>
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
QuestionOptionMapper.xml

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionOptionMapper">

<!-- 查询列表 -->
<select id="selectList" resultType="QuestionOption">
SELECT
pqo.id, pqo.content, pqo.sort
FROM
question_option AS pqo
<where>
pqo.qid = #{qid} <!-- 变量名 qid 对应上文的 qid -->
<!-- 如果上文中 collection只传一个参数column="id",只要类型匹配,在这里随便写个变量名就可以取到值 #{xyz} -->
</where>
</select>
1
2
3
4
5
6
7
8
9
10
11
12
13
方式二:
只需要执行一次sql查询, 主表分页查询不正确
1
QuestionMapper.xml

<mapper namespace="com.xxx.modules.xxx.mapper.QuestionMapper">

<resultMap id="BaseResultMap" type="com.xxx.modules.xxx.entity.Question" >
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="content" property="content" jdbcType="VARCHAR" />
<result column="type" property="type" jdbcType="VARCHAR" />
<result column="sort" property="sort" jdbcType="INTEGER" />
<collection property="options" javaType="java.util.ArrayList" ofType="com.xxx.modules.data.entity.QuestionOption">
<id column="o_id" property="id" jdbcType="VARCHAR" />
<result column="o_content" property="content" jdbcType="VARCHAR" />
<result column="o_sort" property="sort" jdbcType="INTEGER" />
</collection>
<!-- 列的别名 o_id,o_content,o_sort , 起别名是因为主子表都有这几个字段
这里要写 ofType, javaType还是可以不写 -->
</resultMap>

<!-- 查询列表 -->
<select id="selectList" resultMap="BaseResultMap">
SELECT
pq.id, pq.content, pq.type, pq.sort
,pqo.id AS oid ,pqo.content AS ocontent ,pqo.sort AS osort <!-- 联查子表字段,起别名 -->
FROM
question AS pq
LEFT JOIN question_option pqo ON pq.id = pqo.qid <!-- 联查子表 -->
<where>
</where>
</select>
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
注意: 主子表要查询出来的字段名重复,要起别名
参考博客
————————————————
版权声明:本文为CSDN博主「lzxomg」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lzxomg/article/details/89739651

posted @ 2021-03-13 15:13  万事俱备就差个程序员  阅读(2464)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏