SpringBoot2.x模块系列:Mybatis Plus

1 Mybatis Plus

  • Java Web ORM工具。
  • 可以省略大量Mybatis原生语句,提高CURD效率。
  • 可以结合原生Mybatis语句使用,提高开发效率。
  • 个人不喜欢,但是,项目上有使用。

2 Usage

  • mybatis的mapper层,可以不用写任何Mybatis原生语句,实现mapper(dao)接口。
  • 直接在mapper(dao)接口继承BaseMapper,可以实现基本的CURD操作。
  • service层继承IService,可单独实现CURD,也可重写符合要求的CURD,通过wrapper的lambda表达式实现。
  • controller直接使用Service。

2.1 模块

2.1.0 mybatis/mapper

2.1.2 mapper(dao)

package com.company.springbootmybatisplus.dao;

import com.company.springbootmybatisplus.po.UserPO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

/**
 * @author xindaqi
 * @since 2020-08-15
 */
@Mapper
public interface UserDAO extends BaseMapper<UserPO>{
     
}

2.1.3 servcie

package com.company.springbootmybatisplus.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.springbootmybatisplus.dto.UserQueryInputDTO;
import com.company.springbootmybatisplus.po.UserPO;
import com.baomidou.mybatisplus.extension.service.IService;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import java.util.List;

/**
 * @author xindaqi
 * @since 2020-08-15
 */

public interface UserService extends IService<UserPO> {
     
    IPage<UserPO> pageQuery(UserQueryInputDTO params);
}

2.1.4 serviceImpl

package com.company.springbootmybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.springbootmybatisplus.dto.UserQueryInputDTO;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.company.springbootmybatisplus.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.springbootmybatisplus.dao.UserDAO;
import com.company.springbootmybatisplus.po.UserPO;
import java.util.List;
import org.springframework.util.StringUtils;
/**
 * @author xindaqi
 * @since 2020-08-15
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserDAO, UserPO> implements UserService{
     
    @Override
    public IPage<UserPO> pageQuery(UserQueryInputDTO params){
     
        IPage<UserPO> page = new Page<>(params.getPageStart(), params.getPageSize());
        QueryWrapper<UserPO> wrapper = new QueryWrapper<>();
        wrapper.select("id", "name", "sex", "uid")
                .lambda()
                .eq(UserPO::getIsDelete, 0)
                .like(!StringUtils.isEmpty(params.getUserName()), UserPO::getName, params.getUserName());
        this.baseMapper.selectPage(page, wrapper);
        return page;
    }
}

2.2 Controller

2.2.1增

package com.company.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.company.springbootmybatisplus.dto.*;
import com.company.springbootmybatisplus.po.UserPO;
import com.company.springbootmybatisplus.utils.ResultJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.springbootmybatisplus.service.UserService;
import org.springframework.beans.BeanUtils;
import com.company.springbootmybatisplus.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.company.springbootmybatisplus.vo.UserDetailsVO;

/**
 * @author xindaqi
 * @since 2020-08-15
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/v1")
@Api(tags = "人员配置")
public class UserInfosController{
     

    static Logger logger = LoggerFactory.getLogger(UserInfosController.class);

    @Autowired
    private UserService userService;

    @Value("${third.url}")
    private String url;

    @RequestMapping(value="/user/add", method=RequestMethod.POST)
    @ApiImplicitParam(name = "userAddInputDTO", value = "用户信息", dataType = "UserAddInputDTO", paramType = "body")
    @ApiOperation("新增人员")
    public ResultJson addUser(@RequestBody UserAddInputDTO userAddInputDTO){
     
        logger.info("Add user");
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userAddInputDTO, userPO);
        LocalDateTime ldt = LocalDateTime.now();
        userPO.setCreateAt(ldt);
        userService.save(userPO);
        return new ResultJson(true, CodeEnum.SUCCESS.getCode());
    }
}

2.2.2 删

package com.company.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.company.springbootmybatisplus.dto.*;
import com.company.springbootmybatisplus.po.UserPO;
import com.company.springbootmybatisplus.utils.ResultJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.springbootmybatisplus.service.UserService;
import org.springframework.beans.BeanUtils;
import com.company.springbootmybatisplus.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.company.springbootmybatisplus.vo.UserDetailsVO;

/**
 * @author xindaqi
 * @since 2020-08-15
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/v1")
@Api(tags = "人员配置")
public class UserInfosController{
     

    static Logger logger = LoggerFactory.getLogger(UserInfosController.class);

    @Autowired
    private UserService userService;

    @Value("${third.url}")
    private String url;

    @RequestMapping(value="/user/delete", params={
     "id"}, method=RequestMethod.DELETE)
    @ApiImplicitParam(name = "id", value = "用户ID", dataType = "Integer", paramType = "query")
    @ApiOperation("删除人员")
    public ResultJson deleteUser(@RequestParam("id") Integer id){
     
        userService.removeById(id);
        return new ResultJson(true, CodeEnum.SUCCESS.getCode());
    }
}

2.2.3 改

package com.company.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.company.springbootmybatisplus.dto.*;
import com.company.springbootmybatisplus.po.UserPO;
import com.company.springbootmybatisplus.utils.ResultJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.springbootmybatisplus.service.UserService;
import org.springframework.beans.BeanUtils;
import com.company.springbootmybatisplus.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.company.springbootmybatisplus.vo.UserDetailsVO;

/**
 * @author xindaqi
 * @since 2020-08-15
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/v1")
@Api(tags = "人员配置")
public class UserInfosController{
     

    static Logger logger = LoggerFactory.getLogger(UserInfosController.class);

    @Autowired
    private UserService userService;

    @Value("${third.url}")
    private String url;

    @RequestMapping(value="/user/update", method=RequestMethod.POST)
    @ApiImplicitParam(name = "userUpdateInputDTO", value = "更新人员信息", dataType = "UserUpdateInputDTO", paramType = "body")
    @ApiOperation("修改人员信息")
    public ResultJson updateUser(@RequestBody UserUpdateInputDTO userUpdateInputDTO){
     
        UserPO userPO = new UserPO();
        BeanUtils.copyProperties(userUpdateInputDTO, userPO);
        userService.updateById(userPO);
        return new ResultJson(true, CodeEnum.SUCCESS.getCode());
    }  
}

2.2.4 查

2.2.4.1 查详情

package com.company.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.company.springbootmybatisplus.dto.*;
import com.company.springbootmybatisplus.po.UserPO;
import com.company.springbootmybatisplus.utils.ResultJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.springbootmybatisplus.service.UserService;
import org.springframework.beans.BeanUtils;
import com.company.springbootmybatisplus.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.company.springbootmybatisplus.vo.UserDetailsVO;


/**
 * @author xindaqi
 * @since 2020-08-15
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/v1")
@Api(tags = "人员配置")
public class UserInfosController{
     

    static Logger logger = LoggerFactory.getLogger(UserInfosController.class);

    @Autowired
    private UserService userService;

    @Value("${third.url}")
    private String url;

    @RequestMapping(value = "/user/details", method=RequestMethod.GET)
    @ApiOperation("查询人员详情")
    @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer", paramType = "query")
    public ResultJson<UserDetailsVO> detailsUser(@RequestParam("id") Integer id){
     
        UserPO userPO = userService.getById(id);

        if(null == userPO){
     
            return new ResultJson(true, CodeEnum.NOT_FOUND.code, CodeEnum.NOT_FOUND.description);
        }
        UserDetailsVO userDetailsVO = new UserDetailsVO();
        BeanUtils.copyProperties(userPO, userDetailsVO);
        return new ResultJson(true, CodeEnum.SUCCESS.getCode(), userDetailsVO);
    }

}

2.2.4.2 查分页-1

package com.company.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.company.springbootmybatisplus.dto.*;
import com.company.springbootmybatisplus.po.UserPO;
import com.company.springbootmybatisplus.utils.ResultJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.springbootmybatisplus.service.UserService;
import org.springframework.beans.BeanUtils;
import com.company.springbootmybatisplus.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.company.springbootmybatisplus.vo.UserDetailsVO;

/**
 * @author xindaqi
 * @since 2020-08-15
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/v1")
@Api(tags = "人员配置")
public class UserInfosController{
     

    static Logger logger = LoggerFactory.getLogger(UserInfosController.class);

    @Autowired
    private UserService userService;

    @Value("${third.url}")
    private String url;

    @RequestMapping(value="/user/query/page", method=RequestMethod.POST)
    @ApiImplicitParam(name = "userQueryInputDTO", value = "分页查询人员信息", dataType = "UserQueryInputDTO", paramType = "body")
    @ApiOperation("分页查询人员信息")
    public ResultJson queryUserInPage(@RequestBody UserQueryInputDTO userQueryInputDTO){
     
        IPage<UserPO> page  = new Page<UserPO>(userQueryInputDTO.getPageStart(), userQueryInputDTO.getPageSize());
        QueryWrapper<UserPO> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(userQueryInputDTO.getUserName())){
     
            wrapper.like("name", userQueryInputDTO.getUserName());
        }
        IPage<UserPO> usersPage = userService.page(page, wrapper);
        List<UserPO> userInfos = usersPage.getRecords();
        long total = usersPage.getTotal();

        return ResultJson.success(total, userInfos);
    }
}

2.2.4.2 查分页-2

package com.company.springbootmybatisplus.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.springbootmybatisplus.vo.UserDetailsVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import com.company.springbootmybatisplus.dto.*;
import com.company.springbootmybatisplus.po.UserPO;
import com.company.springbootmybatisplus.utils.ResultJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.company.springbootmybatisplus.service.UserService;
import org.springframework.beans.BeanUtils;
import com.company.springbootmybatisplus.enums.CodeEnum;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
import java.util.ArrayList;
import java.util.stream.Collectors;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import com.company.springbootmybatisplus.vo.UserDetailsVO;


/**
 * @author xindaqi
 * @since 2020-08-15
 */

@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/api/v1")
@Api(tags = "人员配置")
public class UserInfosController{
     

    static Logger logger = LoggerFactory.getLogger(UserInfosController.class);

    @Autowired
    private UserService userService;

    @Value("${third.url}")
    private String url;

    @RequestMapping(value="/user/query/pageli", method=RequestMethod.POST)
    public ResultJson queryUserInPageLi(@RequestBody UserQueryInputDTO params){
     
        IPage<UserPO> usersPage = userService.pageQuery(params);
        List<UserPO> userLi = usersPage.getRecords();

        long total = usersPage.getTotal();
        return ResultJson.success(total, userLi);
    }
}

【参考文献】
[1]https://www.cnblogs.com/yizhixiang/p/13657830.html
[2]https://blog.csdn.net/womenyiqilalala/article/details/95073892
[3]https://www.jianshu.com/p/07be9ccb3306
[4]https://www.cnblogs.com/2393920029-qq/p/13496810.html

你可能感兴趣的:(#,单体服务SpringBoot2,Mybatis,Mybatis,Plus)