Skip to content

Commit 81f1ee9

Browse files
committed
❄️ feat(分页插件): 添加Mybatis分页插件PageHelper
1 parent 8a92cc9 commit 81f1ee9

File tree

7 files changed

+80
-13
lines changed

7 files changed

+80
-13
lines changed

service-customer/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@
100100
<version>1.3.2</version>
101101
</dependency>
102102

103+
<!-- 引入PageHelper依赖 -->
104+
<dependency>
105+
<groupId>com.github.pagehelper</groupId>
106+
<artifactId>pagehelper-spring-boot-starter</artifactId>
107+
<version>1.2.3</version>
108+
</dependency>
109+
103110
<!-- 引入Mybatis-Plus依赖 -->
104111
<!--<dependency>-->
105112
<!--<groupId>com.baomidou</groupId>-->

service-customer/src/main/java/com/coderqian/eurekacustomer/controller/TestController.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.coderqian.eurekacustomer.service.TestService;
55
import io.swagger.annotations.Api;
66
import io.swagger.annotations.ApiOperation;
7+
import io.swagger.annotations.ApiParam;
78
import org.springframework.beans.factory.annotation.Autowired;
89
import org.springframework.beans.factory.annotation.Value;
910
import org.springframework.cloud.context.config.annotation.RefreshScope;
@@ -26,33 +27,33 @@
2627
@Api(value = "测试", description = "测试模块", position = 1)
2728
public class TestController {
2829

29-
// @Value("${profile}")
30+
// @Value("${profile}")
3031
private String profile;
3132

3233
@Autowired
3334
private TestService testService;
3435

3536
@ApiOperation(value = "返回用户输入的结果", notes = "返回用户输入的结果")
3637
@RequestMapping(value = "/result", method = RequestMethod.GET)
37-
public String test(@RequestParam(value = "text") String text) {
38+
public String test(@ApiParam(value = "测试内容", required = true) @RequestParam(value = "text") String text) {
3839
return testService.test(text);
3940
}
4041

4142
@ApiOperation(value = "测试全局异常捕获", notes = "测试全局异常捕获")
4243
@RequestMapping(value = "/exception", method = RequestMethod.GET)
43-
public void testException(@RequestParam(value = "text") String text) {
44+
public void testException(@ApiParam(value = "测试内容", required = true) @RequestParam(value = "text") String text) {
4445
testService.testException(text);
4546
}
4647

4748
@ApiOperation(value = "返回统一的baseResult", notes = "返回统一的baseResult,推荐使用这种方式,所有的返回结果统一用baseResult组装")
48-
@RequestMapping(value = "/baseresult", method = RequestMethod.GET)
49-
public BaseResult testBaseResult(@RequestParam(value = "text") String text) {
49+
@RequestMapping(value = "/baseResult", method = RequestMethod.GET)
50+
public BaseResult testBaseResult(@ApiParam(value = "测试内容", required = true) @RequestParam(value = "text") String text) {
5051
return testService.testBaseResult(text);
5152
}
5253

5354
@ApiOperation(value = "根据用户id获取用户信息", notes = "根据用户id获取用户信息")
5455
@RequestMapping(value = "/find", method = RequestMethod.GET)
55-
public BaseResult testMybatis(@RequestParam("id") String id) {
56+
public BaseResult testMybatis(@ApiParam(value = "用户id", required = true) @RequestParam("id") String id) {
5657
return testService.testMybatis(id);
5758
}
5859

@@ -61,4 +62,11 @@ public BaseResult testMybatis(@RequestParam("id") String id) {
6162
public String testBus() {
6263
return profile;
6364
}
65+
66+
@ApiOperation(value = "测试分页插件", notes = "测试分页插件")
67+
@RequestMapping(value = "/page", method = RequestMethod.GET)
68+
public BaseResult testPage(@ApiParam(value = "当前页码", required = true) @RequestParam("pageNum") Integer pageNum,
69+
@ApiParam(value = "每页长度", required = true) @RequestParam("pageSize") Integer pageSize) {
70+
return testService.testPageHelper(pageNum, pageSize);
71+
}
6472
}

service-customer/src/main/java/com/coderqian/eurekacustomer/mapper/UserMapper.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.coderqian.eurekacustomer.entity.User;
44
import org.apache.ibatis.annotations.Mapper;
55

6+
import java.util.List;
7+
68
/**
79
* @author qianliqing
810
* @date 2018-10-02 下午2:31
@@ -12,11 +14,18 @@
1214
@Mapper
1315
public interface UserMapper {
1416

17+
/**
18+
* 查询所有用户
19+
*
20+
* @return List<User>
21+
*/
22+
List<User> findAll();
23+
1524
/**
1625
* 根据用户id查询
1726
*
1827
* @param id 用户id
19-
* @return
28+
* @return User
2029
*/
2130
User findUserById(String id);
2231
}

service-customer/src/main/java/com/coderqian/eurekacustomer/service/TestService.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,40 @@ public interface TestService {
1414
* 测试接口
1515
*
1616
* @param text 测试数据
17-
* @return
17+
* @return String
1818
*/
1919
String test(String text);
2020

2121
/**
2222
* 测试接口
2323
*
2424
* @param text 测试数据
25-
* @return
25+
* @return void
2626
*/
2727
void testException(String text);
2828

2929
/**
3030
* 测试接口
3131
*
3232
* @param text 测试数据
33-
* @return
33+
* @return BaseResult
3434
*/
3535
BaseResult testBaseResult(String text);
3636

3737
/**
3838
* 测试mybatis接口
3939
*
4040
* @param id 用户id
41-
* @return
41+
* @return BaseResult
4242
*/
4343
BaseResult testMybatis(String id);
44+
45+
/**
46+
* 测试Mybatis分页插件
47+
*
48+
* @param pageNum 当前页码
49+
* @param pageSize 每页长度
50+
* @return BaseResult
51+
*/
52+
BaseResult testPageHelper(Integer pageNum, Integer pageSize);
4453
}

service-customer/src/main/java/com/coderqian/eurekacustomer/service/impl/TestServiceImpl.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import com.coderqian.eurekacustomer.common.BaseResultFactory;
55
import com.coderqian.eurekacustomer.common.constant.Code;
66
import com.coderqian.eurekacustomer.common.exception.BusinessException;
7+
import com.coderqian.eurekacustomer.entity.User;
78
import com.coderqian.eurekacustomer.mapper.UserMapper;
89
import com.coderqian.eurekacustomer.service.TestService;
10+
import com.github.pagehelper.PageHelper;
11+
import com.github.pagehelper.PageInfo;
912
import org.springframework.beans.factory.annotation.Autowired;
1013
import org.springframework.stereotype.Service;
1114

15+
import java.util.List;
16+
1217
/**
1318
* @author qianliqing
1419
* @date 2018-09-28 下午2:39
@@ -40,4 +45,12 @@ public BaseResult testBaseResult(String text) {
4045
public BaseResult testMybatis(String id) {
4146
return BaseResultFactory.createSuccessResult(userMapper.findUserById(id));
4247
}
48+
49+
@Override
50+
public BaseResult testPageHelper(Integer pageNum, Integer pageSize) {
51+
PageHelper.startPage(pageNum, pageSize);
52+
List<User> users = userMapper.findAll();
53+
PageInfo<User> page = new PageInfo<>(users);
54+
return BaseResultFactory.createSuccessResult(page);
55+
}
4356
}

service-customer/src/main/resources/application.properties

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,9 @@ spring.datasource.filters=stat,wall
2424
logging.config=classpath:log4j2.xml
2525

2626
mybatis.mapper-locations=classpath:mapper/*.xml
27-
mybatis.config-location=classpath:config/mybatis-config.xml
27+
mybatis.config-location=classpath:config/mybatis-config.xml
28+
29+
pagehelper.helperDialect=mysql
30+
pagehelper.reasonable=true
31+
pagehelper.supportMethodsArguments=true
32+
pagehelper.params=count=countSql

service-customer/src/main/resources/mapper/UserMapper.xml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
33
<mapper namespace="com.coderqian.eurekacustomer.mapper.UserMapper">
44

5+
<sql id="user">
6+
id id,
7+
name name
8+
</sql>
9+
10+
<select id="findAll" resultType="User">
11+
select
12+
<include refid="user"/>
13+
from user
14+
</select>
15+
516
<select id="findUserById" parameterType="java.lang.String" resultType="User">
6-
SELECT * FROM user WHERE id = #{id}
17+
select
18+
<include refid="user"/>
19+
from user
20+
<where>
21+
id = #{id}
22+
</where>
723
</select>
824
</mapper>

0 commit comments

Comments
 (0)