springboot整合MyBatis-Plus

一、添加依赖

pom.xml中添加:

<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>

二、配置MyBatis-Plus使用分页插件

  1. 新建MybatisPlusConfig类,并在MybatisPlusConfig.java文件中进行配置:

    import com.baomidou.mybatisplus.annotation.DbType;
    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;

    /**
    * MybatisPlus配置
    */
    @Configuration
    @MapperScan("com.qiancheng.springboot.mapper")//配置扫描mapper包
    public class MybatisPlusConfig {

    /**
    * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
    */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
    return interceptor;
    }

    }
  2. 新建一个 xxxMapper 接口继承 BaseMapper<实体类>,例如:

    import com.baomidou.mybatisplus.core.mapper.BaseMapper;
    import com.qiancheng.springboot.entity.User;
    import org.apache.ibatis.annotations.*;

    import java.util.List;

    //@Mapper
    public interface UserMapper extends BaseMapper<User> {

    @Select("SELECT * FROM sys_user")
    List<User> findAll();

    }

  3. 新建xxxService类继承ServiceImpl,例如:

    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.qiancheng.springboot.entity.User;
    import com.qiancheng.springboot.mapper.UserMapper;
    import org.springframework.stereotype.Service;

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

    public boolean saveUser(User user) {
    // if (user.getId() == null){
    // return save(user);//mybatis-plus提供的方法
    // }else {//user有id则表示修改
    // return updateById(user);//mybatis-plus提供的方法
    // }
    return saveOrUpdate(user);//mybatis-plus提供的方法
    }
    }

  4. 使用MyBatis-Plus提供的简单单表操作方法,例如:

    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.qiancheng.springboot.entity.User;
    import com.qiancheng.springboot.mapper.UserMapper;
    import com.qiancheng.springboot.service.UserService;
    import org.apache.logging.log4j.util.Strings;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.*;
    import java.util.List;

    @RestController
    //@CrossOrigin //临时解决跨域
    @RequestMapping("/user")
    public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> findAll(){
    // 使用原始MyBatis方法查询
    // List<User> all = userMapper.findAll();
    // return all;

    // 使用mybatis-plus方法查询
    return userService.list();//list方法为mybatis-plus提供
    }

    @PostMapping("/save")
    public boolean save(@RequestBody User user){
    //新增或者更新
    return userService.saveUser(user);
    }

    //传统手写分页查询
    //接口路径: /user/page?pageNum=1&pageSize=10
    //@RequestParam注解接收: ?pageNum=1&pageSize=10
    //limit第一个参数:(pageNum-1)*pageSize
    //limit第二个参数:pageSize
    // @GetMapping("/page")
    // public Map<String,Object> findPage(@RequestParam Integer pageNum,
    // @RequestParam Integer pageSize,
    // @RequestParam String username){
    // Integer pageIndex = (pageNum - 1) * pageSize;
    // List<User> data = userMapper.selectPage(pageIndex,pageSize,username);
    // Integer total = userMapper.selectTotal(username);
    // Map<String,Object> res = new HashMap<>();
    // res.put("data",data);
    // res.put("total",total);
    // return res;
    // }

    //使用MyBatis-Plus分页查询
    @GetMapping("/page")
    public IPage<User> findPage(@RequestParam(required = true) Integer pageNum,
    @RequestParam(required = true) Integer pageSize,
    @RequestParam(required = false) String username,
    @RequestParam(defaultValue = "") String nickname,
    @RequestParam(defaultValue = "") String address){
    IPage<User> page = new Page<>(pageNum,pageSize);
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();//分页条件
    queryWrapper.like(!Strings.isEmpty(username),"username",username);//!Strings.isEmpty(username)表示username不为null时候才加入这个模糊查询条件
    queryWrapper.like(!Strings.isEmpty(nickname),"nickname",nickname);
    queryWrapper.like(!Strings.isEmpty(address),"address",address);
    IPage<User> userPage = userService.page(page,queryWrapper);
    return userPage;
    }
    }

备注:(MyBatis-Plus官网:https://baomidou.com/)