最近剛入職新公司,項目是從零開始搭建的項目。我覺得是時候考驗是驢還是千里馬的時候。都是淚就不多說了。
附上一篇Mybatis常用的分頁案例。這次要做的是最常見的分頁效果,也是基礎功能。但是很多人都做不好的。這次采用Mybatis分頁插件PageHelper。
僅獻給伸手黨的大爺們。大爺們好!拿代碼記得扣666!!小的在這給感謝了!!
一、項目結構
按照controller,service,mapper(也叫dao)來建立項目,utils里面是兩個自己寫的分頁工具類。看前
端需要什么分頁參數,你方便添加。并能返回給他
二、插件引入
1.pom.xml。引入PageHelper插件
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
<?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 2.5 . 6 </version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.simons</groupId> <artifactId>SimonsStudio</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>SimonsStudio</name> <description>SimonsStudio</description> <properties> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!--Mybatis框架--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version> 2.2 . 0 </version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--pageHelper分頁插件--> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version> 1.2 . 3 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
2.application.xml配置。我的*maper.xml都是放在resource文件夾里面的。這里有個 mapper-locations一定要配你項目的,不懂的看我的項目結構。PageHelper照著配置就行了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
server: port: 8080 spring: datasource: name: test url: jdbc:mysql: //127.0.0.1:3306/simonsdb username: root password: root driver- class -name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml #注意了!這個是mapper.xml的路徑。要按照目錄文件夾來命名。 type-aliases- package : com.smions.entity #實體類的文件夾 pagehelper: # helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql |
三、代碼
1.mapper。習慣上從dao層寫起來。@Mapper@Component兩個注解要寫上,不然后面springboot啟動的時候掃描不到。在Mybatis中。mapper相當于dao層。只有接口。selectPage這個是我們要做的分頁,這個名字和Maper.xml里面的id對應。要寫對了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
package com.simons.mapper; import com.simons.entity.Student; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; @Mapper @Component public interface StudentMapper { /** * 查詢全部用戶 * @return */ List<Map<String,Object>> selectAll(); /** * 分頁查詢用戶 * @return */ List<Student> selectPage(); } |
2.*mapper.xml。namespace對應Java中的mapper類。id = "Base_Column_List",是你要查詢的表里面的所有字段。記得username不要寫成name.這個我踩過好多坑。運行的時候一直報mapper找不到對應的方法。原因是name會被識別為關鍵字!!不要取名sql或者java的關鍵字,不然你會哭的!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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.simons.mapper.StudentMapper" > <sql id= "Base_Column_List" > id, username, password </sql> <select id= "selectAll" resultType= "map" > SELECT <include refid= "Base_Column_List" /> FROM student </select> <select id= "selectPage" resultType= "map" > SELECT <include refid= "Base_Column_List" /> FROM student </select> </mapper> |
3.service.java。getPageInfo方法是重點。PageHelper的核心代碼就只有一句:
PageHelper.startPage(pageNum,pageSize);//開始分頁
List<Student> students = studentMapper.selectPage();//查詢數據庫返回數據
PageInfo<Student> pageInfo = new PageInfo<>(students);//得到數據,里面還帶有分頁的各個參數
很簡單是不是。接下來介紹一下pageInfo自帶的分頁參數:
pageNum=5, //查詢的頁數(第5頁)
pageSize=3, //每頁的大小
size=3,
startRow=13,
endRow=15,
total=22,//總數
pages=8, //總頁數:22/3=8
list=Page{count=true, pageNum=5, pageSize=3, startRow=12, endRow=15, total=22, pages=8, reasonable=true, pageSizeZero=false},
prePage=4, //前一頁
nextPage=6, //后一頁
isFirstPage=false, //是否是第一頁
isLastPage=false, //是否是最后一頁
hasPreviousPage=true, //是否有上一頁
hasNextPage=true, //是否有下一頁
navigatePages=8, //導航總頁數
navigateFirstPage=1, //導航第一頁
navigateLastPage=8, //導航最后一頁
navigatepageNums=[1, 2, 3, 4, 5, 6, 7, 8]//頁數碼
看完后,是不是一個很完美的插件(狗頭)。相關的分頁參數都是很齊全的。
service代碼。這里我先用getInfo拿到PageInfo。再把相關的參數和list數據拿出來。返回給controller即可。
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.simons.service; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.simons.entity.Student; import com.simons.mapper.StudentMapper; import com.simons.util.PageRequest; import com.simons.util.PageResult; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; @Service public class StudentService{ @Autowired private StudentMapper studentMapper; public List<Map<String,Object>> findAll() { // List<Student> students = List<Map<String,Object>> map = studentMapper.selectAll(); return map; } public PageResult findPage(PageRequest pageRequest) { PageInfo<Student> pageInfo = getPageInfo(pageRequest); PageResult pageResult = new PageResult(); pageResult.setPageNum(pageInfo.getPageNum()); pageResult.setPageSize(pageInfo.getPageSize()); pageResult.setTotalPages(pageInfo.getPages()); pageResult.setContent(pageInfo.getList()); return pageResult; } /** * 調用分頁插件完成分頁 * */ public PageInfo<Student> getPageInfo(PageRequest pageRequest) { int pageNum = pageRequest.getPageNum(); int pageSize = pageRequest.getPageSize(); PageHelper.startPage(pageNum,pageSize); //核心代碼:pageHelper查詢分頁 List<Student> students = studentMapper.selectPage(); PageInfo<Student> pageInfo = new PageInfo<>(students); return pageInfo; } } |
4.controller層。findpage方法。查找分頁的方案。因為分頁往往帶有查詢參數。這邊沒寫。
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
28
29
30
31
32
33
34
35
36
37
38
|
package com.simons.controller; import com.simons.service.StudentService; import com.simons.util.PageRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Map; //@RestController //@RequestMapping("/student") @Controller @RequestMapping (value = { "/student" }) public class StudentController { @Autowired private StudentService studentService; @ResponseBody @RequestMapping (value= "/findAll" ) public List<Map<String,Object>> findAll() { return studentService.findAll(); } @ResponseBody @RequestMapping (value = { "/findPage" }) public Object findPage( @RequestParam (name = "pageNum" )String pageNum , @RequestParam (name = "pageSize" ) String pageSize) { PageRequest pageQuery = new PageRequest(); pageQuery.setPageNum(Integer.parseInt(pageNum)); pageQuery.setPageSize(Integer.parseInt(pageSize)); return studentService.findPage(pageQuery); } // @PostMapping(value="/findPage") // public Object findPage(@RequestBody PageRequest pageQuery) { // return studentService.findPage(pageQuery); // } } |
5.utils類。一個是pageRequest請求分頁,一個是pageResult返回分頁的實體類
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package com.simons.util; import java.util.List; /** * 分頁返回結果 */ public class PageResult { /** * 當前頁碼 */ private int pageNum; /** * 每頁數量 */ private int pageSize; /** * 記錄總數 */ private long totalSize; /** * 頁碼總數 */ private int totalPages; /** * 數據模型 */ private List<?> content; public int getPageNum() { return pageNum; } public void setPageNum( int pageNum) { this .pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize( int pageSize) { this .pageSize = pageSize; } public long getTotalSize() { return totalSize; } public void setTotalSize( long totalSize) { this .totalSize = totalSize; } public int getTotalPages() { return totalPages; } public void setTotalPages( int totalPages) { this .totalPages = totalPages; } public List<?> getContent() { return content; } public void setContent(List<?> content) { this .content = content; } } |
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
|
package com.simons.util; public class PageRequest { /** * 當前頁碼 */ private int pageNum; /** * 每頁數量 */ private int pageSize; public int getPageNum() { return pageNum; } public void setPageNum( int pageNum) { this .pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize( int pageSize) { this .pageSize = pageSize; } } |
6.spring項目啟動 application.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.simons; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication @MapperScan ( "com.simons.mapper" ) //@ComponentScan(basePackages={"com.simons.*"}) public class SimonsStudioApplication { public static void main(String[] args) { SpringApplication.run(SimonsStudioApplication. class , args); } } |
四、測試:
啟動spring。輸入測試地址:
http://localhost:8080/student/findPage?pageNum=5&pageSize=3
到此這篇關于SpringBoot+Mybatis實現分頁效果的文章就介紹到這了,更多相關SpringBoot Mybatis分頁內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/u012184326/article/details/121064659