1,在pom中导入依赖
<!--分页依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
2.在application.properties中配置相关属性
#pagehelper分页插件配置
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
3.Controller层
/**
*用户信息查询 分页显示
* */
@RequestMapping(value = "/pageSelectUserInfo")
public Result pageSelectUserInfo(@RequestBody Map<String, Object> queryMap){
if( queryMap.get("pageNum")==null && queryMap.get("pageSize")==null){
return Result.faild();
}
int pageNum = (int)queryMap.get("pageNum");
int pageSize = (int)queryMap.get("pageSize");
if(pageNum<=0){
pageNum = 1;
}
if(pageSize>=5){
pageSize = 5;
}
return Result.ok(userService.pageSelectUser(queryMap,pageNum,pageSize));
}
4.service层
PageInfo<User> pageSelectUser(Map queryMap,int pageNum,int PageSize); //查询,分页显示
5.serviceImpl层
public PageInfo<User> pageSelectUser(Map queryMap, int pageNum, int PageSize) {
PageHelper.startPage(pageNum,PageSize);
PageInfo<User> pageInfo = new PageInfo<User>(userMapper.queryUserInfo(queryMap));
return pageInfo;
}
6.dao层
List<User> queryUserInfo(Map<String, Object> queryMap);
7.mapper中sql语句
column对应的是数据库中的名称,property对应的是User实体中的User属性名称
<mapper namespace="com.example.springbootDemo.dao.UserMapper">
<resultMap id="userMapper" type="com.example.springbootDemo.user.User">
<result column="user_id" property="userId" jdbcType="VARCHAR"/>
<result column="user_name" property="userName" jdbcType="VARCHAR"/>
<result column="user_telephone" property="userTelephone" jdbcType="VARCHAR"/>
<result column="user_address" property="userAddress" jdbcType="VARCHAR"/>
</resultMap>
<select id="queryUserInfo" resultMap="userMapper" parameterType="java.util.Map">
SELECT
u.user_id,
u.user_name,
u.user_address,
u.user_telephone
FROM tuser u
where 1=1
<if test="userId != null and userId !='' ">
and u.user_id= #{userId}
</if>
<if test="userName != null and userName != ''">
and u.user_name like concat('%',#{userName},'%')
</if>
<if test="userTelephone != null and userTelephone != ''">
and u.user_telephone = #{userTelephone}
</if>
</select>
</mapper>