先根据 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 { @Autowired private 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; import com.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;