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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java?spring?boot實現(xiàn)批量刪除功能詳細示例
這篇文章主要給大家介紹了關于Java?spring?boot實現(xiàn)批量刪除功能的相關資料,文中通過代碼以及圖文將實現(xiàn)的方法介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2023-08-08Java調(diào)用wsdl接口的兩種方法(axis和wsimport)
本文主要介紹了Java調(diào)用wsdl接口的兩種方法(axis和wsimport),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-03-03SpringBoot整合Java DL4J實現(xiàn)文本分類系統(tǒng)
在當今信息爆炸的時代,自然語言處理領域中的文本分類顯得尤為重要,文本分類能夠高效地組織和管理海量的文本數(shù)據(jù),隨著互聯(lián)網(wǎng)的飛速發(fā)展,我們每天都被大量的文本信息所包圍,本文將介紹如何使用 Spring Boot 整合 Java Deeplearning4j 來構建一個文本分類系統(tǒng)2024-10-10Java中Comparator與Comparable排序的區(qū)別詳解
這篇文章主要介紹了Java中Comparator與Comparable排序的區(qū)別詳解,如果你有一個類,希望支持同類型的自定義比較策略,可以實現(xiàn)接口Comparable,如果某個類,沒有實現(xiàn)Comparable,但是又希望對它進行比較,則可以自定義一個Comparator,需要的朋友可以參考下2024-01-01JPA如何使用entityManager執(zhí)行SQL并指定返回類型
這篇文章主要介紹了JPA使用entityManager執(zhí)行SQL并指定返回類型的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06