使用mybatis插件完成springboot的分页插件
时间: 2023-10-31 20:13:47 浏览: 85
mybatis的分页插件
好的,这里是使用Mybatis插件完成Spring Boot的分页插件的方法:
1. 首先,在pom.xml文件中添加Mybatis分页插件的依赖:
```xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
```
2. 接下来,在application.properties文件中添加Mybatis分页插件的配置:
```properties
# 开启分页插件
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
```
3. 在Mapper.xml中编写SQL语句,并添加分页插件的配置:
```xml
<!-- 查询用户列表 -->
<select id="selectUsers" resultMap="userMap">
select * from user
<where>
<if test="username != null and username != ''">
and username like concat('%',#{username},'%')
</if>
<if test="email != null and email != ''">
and email like concat('%',#{email},'%')
</if>
</where>
<if test="sort != null and sort != ''">
order by ${sort}
</if>
limit #{pageStart},#{pageSize}
</select>
<!-- 分页插件配置 -->
<select id="selectUsers" resultMap="userMap">
select count(*) from user
<where>
<if test="username != null and username != ''">
and username like concat('%',#{username},'%')
</if>
<if test="email != null and email != ''">
and email like concat('%',#{email},'%')
</if>
</where>
</select>
```
4. 最后,在Service中调用Mapper方法进行分页查询:
```java
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public Page<User> selectUsers(String username, String email, String sort, int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> users = userMapper.selectUsers(username, email, sort);
PageInfo<User> pageInfo = new PageInfo<>(users);
return PageConverter.convert(pageInfo);
}
}
```
这样,就可以使用Mybatis插件完成Spring Boot的分页插件了。
阅读全文