Springboot练习——分页查询
Emp类
package com.wzb.pojo20240930;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private Integer id;
private String username;
private String password;
private String name;
private Short gender;
private String image;
private Short job;
private LocalDate entrydate;
private Integer deptId;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
查询封装的PageBean类
package com.wzb.pojo20240930;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class PageBean {
private Long total;
private List<Emp> rows;
}
查询返回的Result类
package com.wzb.pojo20240930;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private Integer code; // 响应码,1是响应成功,2是响应失败
private String message; // 响应信息,描述响应结果的字符串
private Object data; // 响应返回的数据
// 响应成功,但不返回数据的方法
public static Result success() {
return new Result(1, "success", null);
}
// 响应成功,并且返回数据的方法
public static Result success(Object data) {
return new Result(1,"success", data);
}
// 响应失败的方法
public static Result fail(String message) {
return new Result(0, message, null);
}
}
Controller
package com.wzb.controller;
import com.wzb.pojo20240930.Emp;
import com.wzb.pojo20240930.PageBean;
import com.wzb.pojo20240930.Result;
import com.wzb.service.EmpService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.util.List;
@RestController
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("/emps")
// @RequestParam接收前端传递的想要查询的页数和每一页的记录数
public Result pageSelect(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,
String name, Short gender,
// 因为Http请求的路径全部都是字符串,所以说要用@DateTimeFormat注解并限定其格式将String解析为LocalDate
@DateTimeFormat(pattern = ("yyyy-MM-dd")) LocalDate begin,
@DateTimeFormat(pattern = ("yyyy-MM-dd")) LocalDate end) {
// 日志记录
System.out.println("进行了一次条件查询");
// 通过Service层,操作数据库,并将查询的数据封装到PageBean中返回
PageBean pageBean = empService.pageSelect(page, pageSize, name, gender, begin, end);
return Result.success(pageBean);
}
}
Service
package com.wzb.service;
import com.wzb.pojo20240930.Emp;
import com.wzb.pojo20240930.PageBean;
import java.time.LocalDate;
import java.util.List;
public interface EmpService {
PageBean pageSelect(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);
}
实现类
package com.wzb.service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wzb.mapper.EmpMapper;
import com.wzb.pojo20240930.Emp;
import com.wzb.pojo20240930.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
@Service
public class EmpServiceImplement implements EmpService{
@Autowired
private EmpMapper empMapper;
@Override
public PageBean pageSelect(Integer page, Integer pageSize, String name, Short gender,
LocalDate begin, LocalDate end) {
// 使用分页插件
PageHelper.startPage(page, pageSize);
// 调用mapper中的方法查询并返回
List<Emp> empList = empMapper.pageSelect(name, gender, begin, end);
Page<Emp> p = (Page<Emp>) empList;
return new PageBean(p.getTotal(), p.getResult());
}
}
Mapper
package com.wzb.mapper;
import com.wzb.pojo20240930.Emp;
import org.apache.ibatis.annotations.Mapper;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
List<Emp> pageSelect(String name, Short gender, LocalDate begin, LocalDate end);
}
XML配置文件
<?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.wzb.mapper.EmpMapper">
<!-- 执行条件分页查询 -->
<!-- 条件分页查询 -->
<select id="pageSelect" resultType="com.wzb.pojo20240930.Emp">
select * from emp
<where>
<if test="name != null and name != ''">
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>
标签:LocalDate,Springboot,wzb,练习,private,import,com,public From: https://blog.csdn.net/Aishangyuwen/article/details/142665335