黑马程序员JavaWeb开发教程
文章目录
- 一、分页查询-分析
- 二、分页查询-实现
- 1. 实现思路
- 1.1 controller
- 1.2 service
- 1.3 mapper
- 1.4 postman测试接口
- 三、分页查询-PageHelper插件
- 1. 引入pageHelper插件的依赖
- 2. 修改原来的代码
- 2.1 mapper
- 2.2 serviceimpl
- 2.3 postman测试接口
- 四、分页查询-条件查询
- 1. 首先根据分页查询的需求写出查询的SQL语句
- 2. 创建动态SQL
- 2.1 创建xml映射文件
- 2.2 controller
- 2.3 service
- 2.4 mapper
- 2.5 postman测试接口
一、分页查询-分析
- 实体类PageBean
package com.itheima.mytlias.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;@Data
@AllArgsConstructor
@NoArgsConstructor
public class PageBean {private Long total;//总记录数private List rows;//当前页的数据
}
二、分页查询-实现
1. 实现思路
- 请求参数:页码、每页展示记录数
- 响应结果:总记录数、结果列表
1.1 controller
package com.itheima.mytlias.controller;import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.pojo.Result;
import com.itheima.mytlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping//@RequestParam:为了给形参指定默认值public Result page(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer pageSize) {//打印日志//调用servicePageBean pageBean = empService.page(page, pageSize);//返回结果return Result.success(pageBean);}
}
1.2 service
- service
package com.itheima.mytlias.service;import com.itheima.mytlias.pojo.PageBean;public interface EmpService {/*** 分页查询* @return*/PageBean page(Integer page,Integer pageSize);
}
- impl
package com.itheima.mytlias.service.impl;import com.itheima.mytlias.mapper.EmpMapper;
import com.itheima.mytlias.pojo.Emp;
import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;/*** 分页查询** @return*/@Overridepublic PageBean page(Integer page, Integer pageSize) {//调用mapper进行分页查询//列表数据List<Emp> empList = empMapper.list(page, pageSize);//总数Long count = empMapper.count();PageBean pageBean = new PageBean(count, empList);return pageBean;}
}
1.3 mapper
package com.itheima.mytlias.mapper;import com.itheima.mytlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@Mapper
public interface EmpMapper {/*** 查询总记录数** @return*/@Select("select count(*) from emp")public Long count();/*** 查询本页员工列表** @param start* @param pageSize* @return*/@Select("select * from emp limit #{start},#{pageSize}")public List<Emp> list(@Param("start") Integer start, @Param("pageSize") Integer pageSize);
}
1.4 postman测试接口
三、分页查询-PageHelper插件
1. 引入pageHelper插件的依赖
- 在pom.xml中添加以下代码
<!-- pagehelper分页插件--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.2</version></dependency>
2. 修改原来的代码
- 除了EmpMapper和EmpServiceImpl中的代码意外不用修改其他的代码
2.1 mapper
package com.itheima.mytlias.mapper;import com.itheima.mytlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;import java.util.List;@Mapper
public interface EmpMapper {/*** 使用pageHelper的话,只需要定义一个简单的查询就可以* @return*/@Select("select * from emp")public List<Emp> list();
}
2.2 serviceimpl
package com.itheima.mytlias.service.impl;import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mytlias.mapper.EmpMapper;
import com.itheima.mytlias.pojo.Emp;
import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;/*** 分页查询** @return*/@Overridepublic PageBean page(Integer page, Integer pageSize) {//使用pagehelper指定查询页码,和每页查询数据PageHelper.startPage(page,pageSize);//执行查询List<Emp> empList = empMapper.list();Page<Emp> p= (Page<Emp>)empList;//强制转换成Page类型Long count=p.getTotal();List<Emp> result = p.getResult();//封装为 PageBeanPageBean pageBean = new PageBean(count,result);return pageBean;}
}
2.3 postman测试接口
四、分页查询-条件查询
1. 首先根据分页查询的需求写出查询的SQL语句
select * from emp
wherename like concat('%','张','%')and gender=1and entrydate between '2000-01-01' and '2010-01-01'
order by update_time desc;
2. 创建动态SQL
2.1 创建xml映射文件
- 同包同名
- 去mybatis中文网复制配置信息,就是下边的
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!-- namespace:EmpMapper的全类名-->
<mapper namespace="org.mybatis.example.BlogMapper"><!-- id:与方法名一致resultType:返回单条记录的全类名--><select id="selectBlog" resultType="Blog">select * from Blog where id = #{id}</select>
</mapper>
- 创建动态SQL
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:EmpMapper的全类名-->
<mapper namespace="com.itheima.mytlias.mapper.EmpMapper"><!-- 带条件的分页查询-动态查询--><!-- id:与方法名一致resultType:返回单条记录的全类名--><select id="list" resultType="com.itheima.mytlias.pojo.Emp">select * from emp<where><if test="name!=null">name like concat('%',#{name},'%')</if><if test="gender!=null">and gender=#{gender}</if><if test="begin!=null and end!=null">and entrydate between #{begin} and #{end}</if></where>order by update_time desc</select>
</mapper>
2.2 controller
package com.itheima.mytlias.controller;import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.pojo.Result;
import com.itheima.mytlias.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.time.LocalDate;@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {@Autowiredprivate EmpService empService;@GetMapping//@RequestParam:为了给形参指定默认值//@DateTimeFormat:日期时间类型的参数,需要使用该注解指定格式public Result page(String name, Short gender,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "2") Integer pageSize) {//打印日志log.info("参数 {},{},{},{},{},{}", name, gender, begin, end, page, pageSize);//调用servicePageBean pageBean = empService.page(name, gender, begin, end, page, pageSize);//返回结果return Result.success(pageBean);}
}
2.3 service
- service
package com.itheima.mytlias.service;import com.itheima.mytlias.pojo.PageBean;import java.time.LocalDate;public interface EmpService {/*** 分页查询** @return*/PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize);
}
- impl
package com.itheima.mytlias.service.impl;import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mytlias.mapper.EmpMapper;
import com.itheima.mytlias.pojo.Emp;
import com.itheima.mytlias.pojo.PageBean;
import com.itheima.mytlias.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.time.LocalDate;
import java.util.List;@Service
public class EmpServiceImpl implements EmpService {@Autowiredprivate EmpMapper empMapper;/*** 分页查询** @return*/@Overridepublic PageBean page(String name, Short gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {//使用pagehelper指定查询页码,和每页查询数据PageHelper.startPage(page, pageSize);//执行查询List<Emp> empList = empMapper.list(name, gender, begin, end);Page<Emp> p = (Page<Emp>) empList;//强制转换成Page类型Long count = p.getTotal();List<Emp> result = p.getResult();//封装为 PageBeanPageBean pageBean = new PageBean(count, result);return pageBean;}
}
2.4 mapper
package com.itheima.mytlias.mapper;import com.itheima.mytlias.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.web.bind.annotation.PathVariable;import java.time.LocalDate;
import java.util.List;@Mapper
public interface EmpMapper {/*** 使用pageHelper的话,只需要定义一个简单的查询就可以** @return*/
// @Select("select * from emp")public List<Emp> list(@Param("name") String name, @Param("gender") Short gender, @Param("begin") LocalDate begin, @Param("end") LocalDate end);
}