mybatis配合pageHelper一對多查詢分頁問題處理
阿新 • • 發佈:2021-08-24
使用mybatis結合pageHelper實現分頁查詢,但查詢多表的時候資料就會比真實的要少,如下面程式。
需求是:tab_question是題目表,tab_option是選項表,查詢一道題目下面4個選項,一頁有5個題目。
//controller層 @RestController @RequestMapping("/question") public class QuestionController extends BaseController { @Autowired private QuestionService questionService; @GetMapping public PageInfo findAll(){ PageHelper.startPage(1, 5); List<Question> questions = questionService.findAll(); PageInfo pageInfo = new PageInfo(questions); return pageInfo; } }
mapper配置
<resultMap id="questionMap" type="question"> <id column="qid" property="qid"/> <result column="question" property="question"/> <result column="qtype" property="qtype"/> <collection property="options" ofType="option" > <id column="oid" property="oid"/> <result column="optionDec" property="optionDec"/> <result column="qid" property="qid"/> </collection> </resultMap> <select id="findAll" resultMap="questionMap"> select * from tab_question q left join tab_option o on q.qid = o.qid </select>
查詢結果,一頁只有一個題目,而且第二個題目還不全
{ "total": 36, "list": [ { "qid": 1, "question": "這個問題有幾個字", "qtype": 1, "options": [ { "oid": 1, "optionDec": "1", "qid": 1 }, { "oid": 2, "optionDec": "2", "qid": 1 }, { "oid": 3, "optionDec": "3", "qid": 1 }, { "oid": 4, "optionDec": "8", "qid": 1 } ] }, { "qid": 2, "question": "大明王朝1566有哪些角色", "qtype": 2, "options": [ { "oid": 5, "optionDec": "嘉靖", "qid": 2 } ] } ] }
原因是:此時的分頁是多表查詢後得到的記錄再進行分頁(相當於按照選項進行分頁),但實際要查詢的是按照題目進行分頁。
解決辦法:使用子查詢
修改mapper配置,在resultMap中巢狀子查詢
<!--此處省略了id,result標籤-->
<resultMap id="questionMap" type="question">
<collection property="options" ofType="option" column="qid" select="findOptionById"/>
</resultMap>
<!--主查詢-->
<select id="findAll" resultMap="questionMap">
select * from tab_question
</select>
<!--子查詢-->
<select id="findOptionById" parameterType="int" resultType="option">
select * from tab_option where qid = #{qid}
</select>
這次結果就對了
{
"total": 9,
"list": [
{
"qid": 0,
"question": "這個問題有幾個字",
"qtype": 1,
"options": [
{
"oid": 1,
"optionDec": "1",
"qid": 1
},
{
"oid": 2,
"optionDec": "2",
"qid": 1
},
{
"oid": 3,
"optionDec": "3",
"qid": 1
},
{
"oid": 4,
"optionDec": "8",
"qid": 1
}
]
},
{
"qid": 0,
"question": "大明王朝1566有哪些角色",
"qtype": 2,
"options": [
{
"oid": 5,
"optionDec": "嘉靖",
"qid": 2
},
{
"oid": 6,
"optionDec": "海瑞",
"qid": 2
},
{
"oid": 7,
"optionDec": "張居正",
"qid": 2
},
{
"oid": 8,
"optionDec": "徐階",
"qid": 2
}
]
},
{
"qid": 0,
"question": "海鋼鋒叫什麼",
"qtype": 1,
"options": [
{
"oid": 10,
"optionDec": "海瑞",
"qid": 26
},
{
"oid": 11,
"optionDec": "海筆架",
"qid": 26
},
{
"oid": 12,
"optionDec": "潤蓮",
"qid": 26
},
{
"oid": 13,
"optionDec": "白圭",
"qid": 26
}
]
},
{
"qid": 0,
"question": "海鋼鋒叫什麼",
"qtype": 1,
"options": [
{
"oid": 14,
"optionDec": "海瑞",
"qid": 28
},
{
"oid": 15,
"optionDec": "海筆架",
"qid": 28
},
{
"oid": 16,
"optionDec": "潤蓮",
"qid": 28
},
{
"oid": 17,
"optionDec": "白圭",
"qid": 28
}
]
},
{
"qid": 0,
"question": "海鋼鋒叫什麼",
"qtype": 1,
"options": [
{
"oid": 18,
"optionDec": "海瑞",
"qid": 29
},
{
"oid": 19,
"optionDec": "海筆架",
"qid": 29
},
{
"oid": 20,
"optionDec": "潤蓮",
"qid": 29
},
{
"oid": 21,
"optionDec": "白圭",
"qid": 29
}
]
}
]
}