如果不涉及分页,无需配置
https://baomidou.com/pages/2976a3/#spring-boot
从上面网址粘贴下来 代码即可
package com.atguigu.ssyx.common.config;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author shen
* @date 2023/7/23
* @description : MybatisPlus分页插件配置
* @version: v1.0
**/
@Configuration
@MapperScan("com.atguigu.ssyx.*.mapper")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// DbType.MYSQL 连接的数据库
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
@Bean
public ConfigurationCustomizer configurationCustomizer() {
return configuration -> configuration.setUseDeprecatedExecutor(false);
}
}
package com.atguigu.ssyx.model.acl;
import com.atguigu.ssyx.model.base.BaseEntity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 用户表
*/
@Data // 通过lombok自动生成 setter,getter, toString等方法
@ApiModel(description = "用户") // swagger2表注释
@TableName("admin") // 对应数据库表名
public class Admin extends BaseEntity {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "用户名")
@TableField("username")
private String username;
@ApiModelProperty(value = "密码")
@TableField("password")
private String password;
@ApiModelProperty(value = "昵称")
@TableField("name")
private String name;
@ApiModelProperty(value = "手机")
@TableField("phone")
private String phone;
@ApiModelProperty(value = "仓库id") // swagger2字段注释
@TableField("ware_id") // 对应数据库字段名
private Long wareId;
@ApiModelProperty(value = "角色名称")
@TableField(exist = false) // 表示该属性可以在表中不存在
private String roleName;
}
package com.atguigu.ssyx.acl.mapper;
import com.atguigu.ssyx.model.acl.Admin;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
/**
* @author shen
* @date 2023/7/29
* @description :
* @version: v1.0
**/
@Repository
public interface AdminMapper extends BaseMapper {
}
package com.atguigu.ssyx.acl.service;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
/**
* @author shen
* @date 2023/7/29
* @description :
* @version: v1.0
**/
public interface AdminService extends IService {
IPage selectPage(Page pageParam, AdminQueryVo adminQueryVo);
}
package com.atguigu.ssyx.acl.service.impl;
import com.atguigu.ssyx.acl.mapper.AdminMapper;
import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.atguigu.ssyx.vo.user.UserQueryVo;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
/**
* @author shen
* @date 2023/7/29
* @description :
* @version: v1.0
**/
@Service
@Slf4j
public class AdminServiceImpl extends ServiceImpl implements AdminService {
// 要使用 AdminService 操作数据库,有两种
// 1.注入service
@Autowired
private AdminService adminService;
// 2.使用baseMapper
// baseMapper 等价于 adminService,只是方法不太相同
// baseMapper.selectList();
// 3.使用 adminMapper
// 与上面两个等价
@Autowired
private AdminMapper adminMapper;
@Override
public IPage selectPage(Page pageParam, AdminQueryVo adminQueryVo) {
String username = adminQueryVo.getUsername();
String name = adminQueryVo.getName();
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
if(!StringUtils.isEmpty(name)){
log.info("username : " + name);
queryWrapper.like(Admin::getUsername, name);
}else if(!StringUtils.isEmpty(username)){
log.info("name : " + username);
queryWrapper.like(Admin::getName, username);
}
Page datas = baseMapper.selectPage(pageParam, queryWrapper);
// 等价于
// Page datas = adminService.page(pageParam,queryWrapper);
return datas;
}
}
package com.atguigu.ssyx.acl.controller;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.atguigu.ssyx.vo.user.UserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* @author shen
* @date 2023/7/29
* @description :
* @version: v1.0
**/
@RestController
@Api(tags = "用户管理")
@CrossOrigin // 跨域,解决前后端工程访问协议,ip,端口号不一致问题
@Slf4j
@RequestMapping("/admin/acl/user")
public class AdminController {
@Resource
private AdminService adminService;
@ApiOperation(value = "获取用户分页列表")
@GetMapping("{page}/{limit}")
public Result index(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit,
@ApiParam(name = "adminQueryVo", value = "查询对象", required = false)
AdminQueryVo adminQueryVo) {
Page pageParam = new Page<>(page, limit);
IPage pageModel = adminService.selectPage(pageParam, adminQueryVo);
return Result.ok(pageModel);
}
}