utils
OrderItemUtils (分頁資料庫排序欄位,可以不要)
@Data
@ToString
public class OrderItemUtils implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 需要進行排序的欄位
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/需要進行排序的欄位")
private String column;
/**
* 是否正序排列,默認 true
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/是否正序排列,默認true")
private Boolean asc = true;
}
RequestPageUtils (分頁請求基類)
@Data
public class RequestPageUtils implements Serializable {
private static final long serialVersionUID = 8545996863226528798L;
/**
* 每頁顯示條數,默認 10
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/每頁顯示條數,默認10")
protected Integer size = 10;
/**
* 當前頁
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/當前頁,默認為1")
protected Integer current = 1;
/**
* 排序欄位資訊
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/排序欄位資訊")
protected OrderItemUtils orders = new OrderItemUtils();
/**
* 設定起始頁 1開始
*
* @param current
*/
public void setCurrent(Integer current) {
this.current = Math.max(current, this.current);
}
/**
* 設定起始頁大小
*
* @param size
*/
public void setSize(Integer size) {
this.size = Math.max(size, 1);
}
}
ResponsePageUtils (分頁回應基類)
@Data
public class ResponsePageUtils<T> implements Serializable {
private static final long serialVersionUID = 8545996863226528798L;
/**
* 查詢資料串列
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/查詢資料串列")
protected List<T> entities = Collections.emptyList();
/**
* 總數
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/總數")
protected Long total = 0L;
/**
* 每頁顯示條數,默認 10
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/每頁顯示條數,默認10")
protected Integer size = 10;
/**
* 當前頁
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/當前頁,默認為1")
protected Integer current = 1;
/**
* 排序欄位資訊
*/
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/排序欄位資訊")
protected OrderItemUtils orders = new OrderItemUtils();
}
DTO
UserQueryPageInputDTO (獲取前端傳來的資料,同時可以在其中進行校驗)
@ApiModel(value = "https://www.cnblogs.com/purple910/p/UserQueryPageInputDTO", description = "串列查詢引數")
@Data
@ToString
public class UserQueryPageInputDTO extends RequestPageUtils {
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/名稱", example = "test")
private String mc;
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/學號", example = "...")
private String xh;
@ApiModelProperty(value = "https://www.cnblogs.com/purple910/p/年級", example = "2017")
@NotBlank(message = "年級不能為空")
private String nj;
/**
* 把請求dto轉換為do,用戶操作資料庫
*
* @return
*/
public UserQueryRequestPage convertToUser() {
return new UserQueryPageInputDTO.UserQueryPageInputDTOConvert().convert(this);
}
private static class UserQueryPageInputDTOConvert implements DTOConvert<UserQueryPageInputDTO, UserQueryRequestPage> {
@Override
public UserQueryRequestPage convert(UserQueryPageInputDTO userQueryPageInputDTO) {
UserQueryRequestPage userQueryPage = new UserQueryRequestPage();
BeanUtils.copyProperties(userQueryPageInputDTO, userQueryPage);
return userQueryPage;
}
}
/**
* 請求dto與回應dto的都需要有(current,size,orders),可以通過類轉換來得到回應dto
*/
public UserQueryPageOutputDTO convert(UserQueryPageInputDTO inputDTO) {
UserQueryPageOutputDTO outputDTO = new UserQueryPageOutputDTO();
BeanUtils.copyProperties(inputDTO, outputDTO);
if (StringUtils.isEmpty(inputDTO.getOrders().getColumn())){
outputDTO.setOrders(null);
}
return outputDTO;
}
}
UserQueryPageOutputDTO (回傳前端資料)
@Data
public class UserQueryPageOutputDTO extends ResponsePageUtils<UserGetOutputDTO> {
/**
* 請求dto與回應dto的都需要有(current,size,orders),可以通過類轉換來得到回應dto
*/
public UserQueryPageOutputDTO convert(UserQueryPageInputDTO inputDTO){
BeanUtils.copyProperties(inputDTO, this);
if (StringUtils.isEmpty(inputDTO.getOrders().getColumn())){
this.setOrders(null);
}
return this;
}
}
DO
@ToString
@Data
public class UserQueryRequestPage extends RequestPageUtils {
/**
* 名稱
*/
private String mc;
/**
* 學號
*/
private String xh;
/**
* 年級
*/
private String nj;
/**
* 開始
*/
private Integer start;
/**
* 結束
*/
private Integer end;
/**
* 獲取開始位置
*
* @return
*/
public Integer getStart() {
return (this.current - 1) * this.size;
}
/**
* 獲取結束位置
*
* @return
*/
public Integer getEnd() {
return this.current * this.size;
}
}
DAO
UserMapper
@Mapper
public interface UserMapper {
/**
* 根據引數進行分頁查詢
* @param entity
* @return
*/
public List<User> listByPage(UserQueryRequestPage entity);
/**
* 獲取總條數
* @param entity
* @return
*/
public int countByPage(UserQueryRequestPage entity);
}
UserMapper.xml
<!-- 獲取分頁查詢的總條數 -->
<select id="countByPage" resultType="int" parameterType="com.example.demo.service.params.UserQueryRequestPage">
SELECT COALESCE(COUNT(1),0)
FROM tb_user
WHERE nj= #{nj}
AND sfsc = 'N'
<if test="mc != null and mc != ''">
AND mc LIKE '%${mc}%'
</if>
<if test="xh!= null and xh!= ''">
AND xhLIKE '${xh}%'
</if>
</select>
<!-- 通過條件查詢分頁 -->
<select id="listByPage" resultType="com.example.demo.domain.User" parameterType="com.example.demo.service.params.UserQueryRequestPage">
SELECT * FROM (
SELECT tmp.*, rownum row_id FROM (
SELECT *
FROM tb_user
AND nj= #{nj}
<if test="mc != null and mc != ''">
AND mc LIKE '%${mc}%'
</if>
<if test="xh!= null and xh!= ''">
AND xhLIKE '${xh}%'
</if>
) tmp
WHERE #{end} >= rownum)
WHERE row_id > #{start}
<!-- 如果沒有排序,可以不寫,或者寫死 -->
<if test="orders.column != null and orders.column != ''">
<if test="orders.asc == true">
ORDER BY ${orders.column} ASC
</if>
<if test="orders.asc == false">
ORDER BY ${orders.column} DESC
</if>
</if>
</select>
Service
UserServiceImpl
public class UserServiceImpl implements UserService {
@Resource
private UserMapper mapper;
@Override
public List<User> listByPage(UserQueryRequestPage param) {
// 回傳查詢資料
return mapper.listByPage(param);
}
@Override
public int countByPage(UserQueryRequestPage param) {
// 獲取單位串列查詢的總條數
return mapper.countByPage(param);
}
}
controller
UserController
@RestController
@RequestMapping("/user")
@Api(tags = "人員管理")
public class UserController{
@Autowired
private UserService userService;
@ApiOperation("獲取人員串列")
@ApiOperationSupport(order = 1)
@PostMapping("/query")
public R<UserQueryPageOutputDTO > query(@RequestBody @Valid UserQueryPageInputDTO requestDTO, BindingResult validateResult){
String msg= null;
if (result.hasErrors()) {
List<ObjectError> objectErrors = result.getAllErrors();
msg= String.valueOf(objectErrors.get(0).getDefaultMessage());
}
if (!StringUtils.isEmpty(msg)) {
return R.failed(msg);
}
// 定義回傳類
UserQueryPageOutputDTO outputDTO = queryPageInputDTO.convert(queryPageInputDTO);
// 獲取條數
UserQueryRequestPage user = queryPageInputDTO.convertToUser();
long count = userService.countByPage(user);
if (count == 0) {
return R.ok(outputDTO);
}
outputDTO.setTotal(count);
// 獲取資料
List<User> queryPage = userService.listByPage(user);
// 資料轉換
if (!queryPage.isEmpty()){
List<UserGetOutputDTO> list = new ArrayList<>();
queryPage.forEach(item -> list.add(new UserGetOutputDTO().convert(item)));
outputDTO.setEntities(list);
}
// 資料回傳
return R.ok(outputDTO);
}
}
注: @ApiModelProperty, @Api, @ApiOperation等是屬于swagger的注解,如果不要可以去掉; @NotBlank, @Valid是用來校驗引數的,要添加validation依賴
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/238871.html
標籤:Java
