SpringBoot整合MybatisPlus,并实现新增、修改、删除、查看、分页

在pom.xml中引入mybatis plus的jar包

		<!-- mybatis plus依赖包 -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.3.0</version>
		</dependency>

在resources/application.yml配置文件中配置数据库

#配置数据库
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jeff?characterEncoding=utf8
    username: root
    password: mysql

创建BaseEntity实体类

package com.jeff.mybatis.plus.entity;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;

import lombok.Data;

@Data
public class BaseEntity {

	// 主键
	@TableId(type = IdType.AUTO)
	private Long id;

	// 创建时间
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date createTime;

	// 创建人
	private String createName;

	// 修改时间
	@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
	private Date modifyTime;

	// 修改人
	private String modifyName;

}

创建User实体类

package com.jeff.mybatis.plus.entity;

import java.util.Date;

import org.springframework.format.annotation.DateTimeFormat;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jeff.mybatis.plus.utils.DateUtils;

import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@TableName("sys_user")
@EqualsAndHashCode(callSuper = false)
public class User extends BaseEntity {

	// 登陆名
	private String loginName;

	// 密码
	private String password;

	// 盐
	private String salt;

	// 性别(0:保密;1:男;2:女)
	private Integer sex;

	// 状态(0:有效;1:无效)
	private Integer status;

	// 真实姓名
	private String name;

	// 昵称
	private String nickName;

	// 手机号
	private String phone;

	// 邮箱
	private String email;

	// 生日
	@TableField(updateStrategy = FieldStrategy.IGNORED)
	@DateTimeFormat(pattern = DateUtils.SHORT_TIME_FORMAT)
	@JsonFormat(pattern = DateUtils.SHORT_TIME_FORMAT, timezone = "GMT+8")
	private Date birthday;

	// 用户头像
	private String headimgUrl;

}

创建UserMapper

package com.jeff.mybatis.plus.mapper;

import org.apache.ibatis.annotations.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jeff.mybatis.plus.entity.User;

@Mapper
public interface UserMapper extends BaseMapper<User> {

}

创建UserService

package com.jeff.mybatis.plus.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.jeff.mybatis.plus.entity.User;
import com.jeff.mybatis.plus.entity.request.PageEntity;
import com.jeff.mybatis.plus.entity.request.UserParam;

public interface UserService extends IService<User> {

	boolean isExist(User user);

	Page<User> getList(PageEntity page, UserParam param);

}

创建UserServiceImpl

package com.jeff.mybatis.plus.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeff.mybatis.plus.entity.User;
import com.jeff.mybatis.plus.entity.request.PageEntity;
import com.jeff.mybatis.plus.entity.request.UserParam;
import com.jeff.mybatis.plus.mapper.UserMapper;
import com.jeff.mybatis.plus.service.UserService;
import com.jeff.mybatis.plus.utils.DateUtils;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

	@Override
	public Page<User> getList(PageEntity pageEntity, UserParam param) {
		Page<User> page = new Page<User>(pageEntity.getPage(), pageEntity.getRows());
		QueryWrapper<User> wrapper = new QueryWrapper<User>();
		if (StringUtils.isNotBlank(param.getLoginName())) {
			wrapper.eq("login_name", param.getLoginName());
		}
		if (StringUtils.isNotBlank(param.getName())) {
			wrapper.like("name", param.getName());
		}
		if (param.getStartDate() != null) {
			wrapper.ge("create_time", DateUtils.getStartOfDay(param.getStartDate()));
		}
		if (param.getEndDate() != null) {
			wrapper.lt("create_time", DateUtils.getEndOfDay(param.getEndDate()));
		}
		wrapper.orderByDesc("id");
		return page(page, wrapper);
	}

	@Override
	public boolean isExist(User user) {
		QueryWrapper<User> wrapper = new QueryWrapper<User>();
		wrapper.eq("login_name", user.getLoginName());
		if (user.getId() != null) {
			wrapper.ne("id", user.getId());
		}
		return count(wrapper) > 0;
	}

}

创建UserController

package com.jeff.mybatis.plus.controller;

import java.util.Date;

import org.apache.commons.lang3.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeff.mybatis.plus.common.Constant;
import com.jeff.mybatis.plus.entity.User;
import com.jeff.mybatis.plus.entity.request.PageEntity;
import com.jeff.mybatis.plus.entity.request.UserParam;
import com.jeff.mybatis.plus.enums.ResponseCodes;
import com.jeff.mybatis.plus.service.UserService;
import com.jeff.mybatis.plus.utils.ResultsUtils;

@CrossOrigin("*")
@RestController
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService service;

	/**
	 * 
	 * @description: 新增用户
	 * @author: Jeff
	 * @param user
	 * @return
	 */
	@PostMapping("add")
	public Object add(User user) {
		if (service.isExist(user)) {
			return ResultsUtils.fail(ResponseCodes.PARAMTER_ERROR, "登录名重复,请重新输入");
		}
		user.setCreateTime(new Date());
		user.setCreateName("Jeff");
		service.save(user);
		return ResultsUtils.success();
	}

	/**
	 * 
	 * @description: 修改用户
	 * @author: Jeff
	 * @param user
	 * @return
	 */
	@PostMapping("update")
	public Object update(User user) {
		Validate.notNull(user.getId(), Constant.CHECK_MESSAGE, "用户id");
		if (service.isExist(user)) {
			return ResultsUtils.fail(ResponseCodes.PARAMTER_ERROR, "登录名重复,请重新输入");
		}
		user.setModifyTime(new Date());
		user.setModifyName("Jeff");
		service.updateById(user);
		return ResultsUtils.success();
	}

	/**
	 * 
	 * @description: 删除用户
	 * @author: Jeff
	 * @param id
	 * @return
	 */
	@RequestMapping("delete")
	public Object delete(Long id) {
		Validate.notNull(id, Constant.CHECK_MESSAGE, "用户id");
		service.removeById(id);
		return ResultsUtils.success();
	}

	/**
	 * 
	 * @description: 查看用户
	 * @author: Jeff
	 * @param id
	 * @return
	 */
	@RequestMapping("getById")
	public Object getById(Long id) {
		Validate.notNull(id, Constant.CHECK_MESSAGE, "用户id");
		return ResultsUtils.success(service.getById(id));
	}

	/**
	 * 
	 * @description: 获取用户列表(分页)
	 * @author: Jeff
	 * @param page
	 * @return
	 */
	@RequestMapping("getList")
	public Object getList(PageEntity pageEntity, UserParam param) {
		Page<User> page = service.getList(pageEntity, param);
		return ResultsUtils.success(page.getRecords(), page.getTotal());
	}

}

创建MybatisPlus分页配置类MybatisPlusConfig

package com.jeff.mybatis.plus.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;

/**
 * 
 * @description: MybatisPlus分页配置类
 * @author: Jeff
 */
@Configuration
public class MybatisPlusConfig {

	@Bean
	public PaginationInterceptor paginationInterceptor() {
		return new PaginationInterceptor();
	}

}

打开浏览器访问 http://localhost:9001/user/getList?page=1&rows=5

posted @ 2020-03-16 22:50  知识追求者  阅读(3759)  评论(0编辑  收藏  举报