java - Mybatis 數(shù)據(jù)庫(kù)多表關(guān)聯(lián)分頁(yè)的問(wèn)題
問(wèn)題描述
舉個(gè)例子:有兩個(gè)實(shí)體類 User 和 Address
public class User { private int id; private String username; // 用戶名 private List<Address> addresses; // getter setter...}public class Address { private int id; private String detail; // 詳細(xì)地址 private User user; //所屬用戶 // getter setter...}
數(shù)據(jù)庫(kù):
create table t_user( id int(10) primary key auto_increment, username varchar(50));create table t_address( id int(10) primary key auto_increment, detail varchar(255), user_id int(10), CONSTRAINT FOREIGN KEY (user_id) REFERENCES t_user(id));
mybatis映射配置:
<?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='org.mkh.shop.model.User'> <resultMap type='User' autoMapping='true'><id property='id' column='u_id'/><collection property='address' ofType='Address'> <id property='id' column='a_id'/> <result property='detail' column='detail'/></collection> </resultMap> <select resultType='User' parameterType='map'>select *,ta.id as ’a_id’,tu.id as ’u_id’ from t_user tu left join t_address ta on ta.user_id=tu.id <where> <if test='name != null'>(username like #{name}) </if></where><if test='sort != null'> order by ${sort} <choose><when test='order != null'>${order}</when><otherwise>asc</otherwise> </choose> </if>limit #{pageOffset},#{pageSize} </select><select resultType='int' parameterType='map'>select count(*) from t_user tu left join t_address ta on ta.user_id=tu.id <where> <if test='name != null'>(username like #{name}) </if></where> </select> </mapper>
用戶和地址的關(guān)系為:一個(gè)用戶有多個(gè)地址,一個(gè)地址只能屬于一個(gè)用戶,一對(duì)多假設(shè)現(xiàn)在的需求是,分頁(yè)查詢用戶,用表格顯示,并把每個(gè)用戶的所有地址顯示出來(lái)那么問(wèn)題來(lái)了按照上面的查詢返回的分頁(yè)的數(shù)據(jù)是沒(méi)有問(wèn)題的,但是分頁(yè)的總記錄數(shù)卻是不對(duì)的。比如查出來(lái)的數(shù)據(jù)(數(shù)據(jù)庫(kù)數(shù)據(jù),并非頁(yè)面顯示)如下:
u_idusernamea_iddetail 1user11北京市海淀區(qū) 1user12北京市朝陽(yáng)區(qū) 2user23天津市因?yàn)槲业男枨笫欠猪?yè)顯示用戶,所以一個(gè)用戶就是一條數(shù)據(jù)在頁(yè)面顯示大概是這樣子,理論上是兩條數(shù)據(jù),
用戶id用戶名地址 1user1 1. 北京市海淀區(qū) 2. 北京市朝陽(yáng)區(qū) 2user21. 天津市 共1頁(yè),共2條數(shù)據(jù),每頁(yè)顯示10條但是,按mybatis的find_count配置 查出來(lái)是3條,這種問(wèn)題如何解決?查詢count(*)的時(shí)候?qū)⑺衛(wèi)eft join 后面關(guān)聯(lián)的表去除嗎?這樣會(huì)不會(huì)導(dǎo)致返回的數(shù)據(jù)不準(zhǔn)確
補(bǔ)充說(shuō)明:感覺(jué)大家是理解錯(cuò)我的意思了,其實(shí)我這個(gè)問(wèn)題主要是在SQL 上,而不再mybatis上,因?yàn)槲也樵兂鰜?lái)的數(shù)據(jù)映射完畢之后是完全沒(méi)有問(wèn)題的,只是在分頁(yè)的總記錄數(shù)上出現(xiàn)了問(wèn)題,導(dǎo)致分頁(yè)不正確
問(wèn)題解答
回答1:剛才寫了個(gè)例子測(cè)了一下
`
兩個(gè)實(shí)體類public class User {
private int id;private String username; // 用戶名private List<Address> addresses;
public class Address {
private int id;private String detail; // 詳細(xì)地址private int user_id; // 所屬用戶
映射文件<resultMap type='com.atguigu.mybatis.entity.User' autoMapping='true'>
<result property='id' column='u_id'/> <collection property='addresses' ofType='com.atguigu.mybatis.entity.Address' autoMapping='true'><result property='id' column='a_id'/><result property='user_id' column='u_id'/> </collection></resultMap><select resultMap='userMap' >
<!-- select tu.,ta., --><!-- ta.id as ’a_id’, --><!-- tu.id as ’u_id’ --><!-- from t_user tu , --><!-- t_address ta where ta.user_id=tu.id -->
select tu.*,ta.*, ta.id as ’a_id’, tu.id as ’u_id’ from t_user tu left join t_address ta on ta.user_id=tu.id </select>
測(cè)試結(jié)果
封裝成的 List<User>的size是沒(méi)問(wèn)題的
回答2:關(guān)鍵詞group by 自己查一下具體操作
回答3:SELECT *, ta.id AS ’a_id’, tu.id AS ’u_id’FROM t_user tuLEFT JOIN t_address ta ON ta.user_id = tu.id;
你希望想要兩條 但是你的sql查出三條,所以顯示錯(cuò)誤,應(yīng)該拆分邏輯:應(yīng)該先查出你想要的用戶
<select resultType='User' parameterType='map'>select *from t_user tu <where> <if test='name != null'>(username like #{name}) </if></where><if test='sort != null'> order by ${sort} <choose><when test='order != null'>${order}</when><otherwise>asc</otherwise> </choose> </if>limit #{pageOffset},#{pageSize} </select>
然后在
<resultMap type='User' autoMapping='true'><id property='id' column='u_id'/><collection ' property='addresses' javaType= 'ArrayList' column='u_id' ofType='Address' select= '??' /> </resultMap>
??是自己實(shí)現(xiàn)個(gè) 用userId查L(zhǎng)ist<Address>的方法
回答4:這種情況不能這么分頁(yè),你需要對(duì)主表數(shù)據(jù)進(jìn)行分頁(yè)。
本來(lái)查詢出100條數(shù)據(jù),由于一對(duì)多會(huì)折疊去重很多數(shù)據(jù),導(dǎo)致實(shí)際結(jié)果少于100條。
這種情況下可以采取嵌套查詢方式解決,就是需要N+1次執(zhí)行,可以懶加載。
或者看這里:https://my.oschina.net/flags/...
有關(guān) MyBatis 內(nèi)容可以訪問(wèn):http://mybatis.tk
回答5:<select resultType='int' parameterType='map'> select count(*) from t_user tu left join t_address ta on ta.user_id=tu.id <where> <if test='name != null'> (username like #{name})</if> </where></select>
改:
<select resultType='int' parameterType='map'> select count(*) from t_user tu <where> <if test='name != null'> (username like #{name})</if> </where> group by username</select> 回答6:
用子查詢就不會(huì)有問(wèn)題了
select count(*) from ( // query 在這里即使關(guān)聯(lián)100張表, 也不可能存在問(wèn)題)
樓主可以看一下Mybatis-PageHelper count sql 轉(zhuǎn)換實(shí)現(xiàn)
建議樓主直接使用 Mybatis-PageHelper 實(shí)現(xiàn)分頁(yè)
相關(guān)文章:
1. sql語(yǔ)句 - mysql中關(guān)聯(lián)表查詢問(wèn)題2. python - django models 為生成的html元素添加樣式。3. css - chrome下a標(biāo)簽嵌套img 顯示會(huì)多個(gè)小箭頭?4. javascript - iframe 為什么加載網(wǎng)頁(yè)的時(shí)候滾動(dòng)條這樣顯示?5. javascript - vscode alt+shift+f 格式化js代碼,通不過(guò)eslint的代碼風(fēng)格檢查怎么辦。。。6. html - vue項(xiàng)目中用到了elementUI問(wèn)題7. javascript - 原生canvas中如何獲取到觸摸事件的canvas內(nèi)坐標(biāo)?8. javascript - 如何將一個(gè)div始終固定在某個(gè)位置;無(wú)論屏幕和分辨率怎么變化;div位置始終不變9. mysql updtae追加數(shù)據(jù)sql語(yǔ)句10. javascript - 有什么比較好的網(wǎng)頁(yè)版shell前端組件?
