Mybatis實現(xiàn)聯(lián)表查詢并且分頁功能
今天同學突然問我這個怎么搞。
然后自己搞了一下發(fā)現(xiàn)這個玩意有坑。。就記錄一下
0. 表結構
person表

cat表

一個person有多個cat
實體類就這么寫
1. 實體類
Person實體類
@Data
public class Person implements Serializable {
private static final long serialVersionUID = -70682701290685641L;
private Integer personid;
private String firstname;
private String lastname;
private List<Cat> cats;
}
Cat實體類
@Data
public class Cat implements Serializable {
private static final long serialVersionUID = -39783356260765568L;
private Integer id;
private String name;
/**
* 貓的歷史
*/
private String history;
/**
* 貓的特征
*/
private String features;
/**
* 大概的樣子
*/
private String imgurl;
private Integer personId;
}
2. Mapper配置文件
PersonDao.xml
<?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.liliya.dao.PersonDao">
<!-- 返回的Person映射Map -->
<resultMap type="com.liliya.entity.Person" id="PersonMap">
<result property="personid" column="PersonId" jdbcType="INTEGER"/>
<result property="firstname" column="FirstName" jdbcType="VARCHAR"/>
<result property="lastname" column="LastName" jdbcType="VARCHAR"/>
<collection property="cats" ofType="Cat">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="history" column="history" jdbcType="VARCHAR"/>
<result property="features" column="features" jdbcType="VARCHAR"/>
<result property="imgurl" column="imgUrl" jdbcType="VARCHAR"/>
<result property="personId" column="person_id" jdbcType="INTEGER"/>
</collection>
</resultMap>
<!--查詢單個-->
<select id="queryById" resultMap="PersonMap">
select personid, firstname, lastname, id, name, history, features, imgurl, person_id
from mybatis.person p inner join mybatis.cat c on p.PersonId = c.person_id
and PersonId= #{PersonId}
</select>
</mapper>
上面這個是查詢單個人的,但是分頁沒有這么簡單
一開始我以為是這么寫的,就簡單的加一個limit
<select id="queryById" resultMap="PersonMap">
select personid, firstname, lastname, id, name, history, features, imgurl, person_id
from mybatis.person p inner join mybatis.cat c on p.PersonId = c.person_id
limit #{page},#{step}
</select>
然后查出來就是這個吊樣。。。很明顯嘛,不對我的胃口。。

后面我準備放棄的時候突然想到可以來一個子查詢。。。
然后我就寫出了這樣的sql
,在子查詢里面套子查詢。。。實現(xiàn)limit分頁的效果

select personid, firstname, lastname, id, name, history, features, imgurl, person_id from mybatis.person p left join mybatis.cat c on p.PersonId = c.person_id where PersonId in (select pp.PersonId from (select person.PersonId from person limit 2,2) as pp);
然后sql放到上面就搞好了
跑出來的效果就非常nice

到此這篇關于Mybatis聯(lián)表查詢并且分頁的文章就介紹到這了,更多相關Mybatis查詢分頁內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
ConstraintValidator類如何實現(xiàn)自定義注解校驗前端傳參
這篇文章主要介紹了ConstraintValidator類實現(xiàn)自定義注解校驗前端傳參的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
Spring中@DependsOn注解的作用及實現(xiàn)原理解析
這篇文章主要介紹了Spring中@DependsOn注解的作用及實現(xiàn)原理解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03
springboot 在idea中實現(xiàn)熱部署的方法
這篇文章主要介紹了springboot 在idea中實現(xiàn)熱部署的方法,實現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下2018-10-10
java web學習_淺談request對象中get和post的差異
下面小編就為大家?guī)硪黄猨ava web學習_淺談request對象中get和post的差異。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05

