springboot 整合 mybatis plus -- 分頁查詢
阿新 • • 發佈:2021-07-09
先根據springboot 整合 mybatis plus 這篇生成各種檔案
controller:
package com.lifan.controller; import com.baomidou.mybatisplus.core.metadata.IPage; import com.lifan.request.AcUserReq; import com.lifan.response.AcUserResp; import com.lifan.service.AcUserService; import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; /** * <p> * 前端控制器 * </p> * * @author lifan * @since 2021-07-09 */ @RestController public class AcUserController { @Autowiredprivate AcUserService acUserService; @PostMapping("/Query") public IPage<AcUserResp> SspFxProChannelQuery(@RequestBody AcUserReq req) { return acUserService.listPage(req); } }
service介面:
package com.lifan.service; import com.baomidou.mybatisplus.core.metadata.IPage; importcom.lifan.entity.AcUser; import com.baomidou.mybatisplus.extension.service.IService; import com.lifan.request.AcUserReq; import com.lifan.response.AcUserResp; /** * <p> * 服務類 * </p> * * @author lifan * @since 2021-07-09 */ public interface AcUserService extends IService<AcUser> { IPage<AcUserResp> listPage(AcUserReq req); }
AcUserServiceImpl 實現類:
package com.lifan.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.lifan.entity.AcUser; import com.lifan.mapper.AcUserMapper; import com.lifan.request.AcUserReq; import com.lifan.response.AcUserResp; import com.lifan.service.AcUserService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.List; /** * <p> * 服務實現類 * </p> * * @author lifan * @since 2021-07-09 */ @Service public class AcUserServiceImpl extends ServiceImpl<AcUserMapper, AcUser> implements AcUserService { @Override public IPage<AcUserResp> listPage(AcUserReq req) { Page<AcUser> page = new Page<AcUser>(req.getPage(),req.getPageSize()); LambdaQueryWrapper<AcUser> lambdaQueryWrapper = new LambdaQueryWrapper(); //如果傳了 name 屬性,可根據 name 進行模糊匹配 if(req.getName() != null){ lambdaQueryWrapper.like(AcUser::getName,req.getName()); } // Type 屬性,Y為啟用, N為凍結 。 (相當於where條件過濾) if(req.getType() != null){ lambdaQueryWrapper.eq(AcUser::getType,req.getType()); } IPage<AcUser> iPage = this.page(page,lambdaQueryWrapper); List<AcUserResp> list = new ArrayList<>(); for (AcUser acUser : iPage.getRecords ()) { AcUserResp resp = new AcUserResp (); BeanUtils.copyProperties (acUser,resp); list.add (resp); } System.out.println(list); IPage<AcUserResp> respIPage = new Page<> (); BeanUtils.copyProperties (iPage,respIPage); respIPage.setRecords (list); return respIPage; } }
Mapper 介面:
package com.lifan.mapper; import com.lifan.entity.AcUser; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import org.apache.ibatis.annotations.Mapper; /** * <p> * Mapper 介面 * </p> * * @author lifan * @since 2021-07-09 */ @Mapper public interface AcUserMapper extends BaseMapper<AcUser> { }
實體類:
package com.lifan.entity; import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.TableId; import java.io.Serializable; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.experimental.Accessors; /** * * @author lifan * @since 2021-07-09 */ @Data @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class AcUser implements Serializable { private static final long serialVersionUID = 1L; @TableId(value = "id", type = IdType.ID_WORKER_STR) private Integer id; private String name; private String type; }
AcUserReq接受請求類:
package com.lifan.request; import lombok.Data; /** * * @author lifan * @since 2021-07-09 */ @Data public class AcUserReq { private String name; private String type; private int page; private int pageSize; }
AcUserResp 請求返回類:
package com.lifan.response; import lombok.Data; /** * @author lifan * @since 2021-07-09 */ @Data public class AcUserResp{ private Integer id; private String name; private String type; }
AcUserMapper.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.lifan.mapper.AcUserMapper"> </mapper>
測試結果:
(引數也可加 name 和 type 屬性)
資料庫:
SQL語句:
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 80023
Source Host : localhost:3306
Source Schema : ac-new
Target Server Type : MySQL
Target Server Version : 80023
File Encoding : 65001
Date: 09/07/2021 18:32:50
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for ac_user
-- ----------------------------
DROP TABLE IF EXISTS `ac_user`;
CREATE TABLE `ac_user` (
`id` int(0) NOT NULL,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`type` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of ac_user
-- ----------------------------
INSERT INTO `ac_user` VALUES (1, 'li-1', 'Y');
INSERT INTO `ac_user` VALUES (2, 'li-2', 'Y');
INSERT INTO `ac_user` VALUES (3, 'li-3', 'N');
INSERT INTO `ac_user` VALUES (4, 'li-4', 'N');
INSERT INTO `ac_user` VALUES (5, 'wang-5', 'Y');
INSERT INTO `ac_user` VALUES (6, 'wang-6', 'Y');
INSERT INTO `ac_user` VALUES (7, 'wang-7', 'N');
INSERT INTO `ac_user` VALUES (8, 'wang-8', 'N');
SET FOREIGN_KEY_CHECKS = 1;