31,springboot 整合 Mybatis-Plus

Mybatis-Plus是一个Mybatis的增强工具,它在Mybatis的基础上做了增强,却不做改变。我们在使用Mybatis-Plus之后既可以使用Mybatis-Plus的特有功能,又能够正常使用Mybatis的原生功能。Mybatis-Plus(以下简称MP)是为简化开发、提高开发效率而生,但它也提供了一些很有意思的插件,比如SQL性能监控、乐观锁、执行分析等。

mybatis-plus 官网 https://baomidou.oschina.io/mybatis-plus-doc/#/

1 集成依赖,pom中加入依赖即可



com.baomidou
mybatisplus-spring-boot-starter
1.0.5



com.baomidou
mybatis-plus
2.3


org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1

2,application.yml 添加 Mybatis-Plus 配置

spring:
  datasource:
   # type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/sd_roulette?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=tru
   username: root
    password: root
mybatis-plus:
  global-config:
    #主键类型  0:"数据库ID自增", 1:"用户输入ID",2:"全局唯一ID (数字类型唯一ID)", 3:"全局唯一ID UUID";
    id-type: 0

3,实体类Users代码如下

package com.weiye.mapperplus.entity;
import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import javax.persistence.*;
@TableName("users")   //@TableName 是指与数据库的关联,意味着表对应的数据库表名是users
public class Users {
    @TableId(value="id",type= IdType.AUTO)    //确认为主键
    private Integer id;
    @TableField("username")   //@TableField  表示表中的有个属性
    private String username;
    @TableField("password")  //@TableField  表示表中的有个属性
    private  String password;
 public  Users () {
    }
 public Users(Integer id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }
 public Users(String username, String password) {
        this.username = username;
        this.password = password;
    }

,,,,,,,,//省去set/get
}

4,新增一个MybatisPlus配置类,放在config包下,其中没有做过多的设置,只是设置了一下方言,代码如下:

package com.weiye.mapperplus.config;
import com.baomidou.mybatisplus.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan(value = "com.weiye.mapperplus.mapper")   //扫描的mapper包,必须写
public class MybatisPlusConfig {
 @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor page = new PaginationInterceptor();
        //设置方言类型
        page.setDialectType("mysql");
        return page;
    }
}

5, 在mapper里添加UserMapper接口 继承了MybatisPlus的BaseMapper

package com.weiye.mapperplus.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.weiye.mapperplus.entity.Users;
public interface UserMapper extends BaseMapper {
    @Select("select * from users")
    List getUserList();  //普通的mybatis 查询 Mybatis-Plus 只是对mybatis进行增强
}

6,新建UsersController 类进行测试

package com.weiye.mapperplus.controller;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weiye.mapperplus.entity.Users;
import com.weiye.mapperplus.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class UserController {
/*

*思路:1,配置一个config ,2.添加2个mybatis-plus的jar包 ,和mybatis的jar ,3,maopper借口继承BaseMapper接口

* 4,controller  写接口代码

*/
    @Autowired
    private UserMapper userDao;
  //  http://localhost:8080/getUserList
    @RequestMapping("/getUserList")   //普通的mybatis 查询 Mybatis-Plus 只是对mybatis进行增强
    public List getUserList(){
        return userDao.getUserList();
    }
    //http://localhost:8080/getUserListByName?username=xiaoli
    //条件查询
    @RequestMapping("/getUserListByName")
    public List getUserListByName(String username) { 
        Map map = new HashMap();
        map.put("username", username);
        return userDao.selectByMap(map);
    }
//http://localhost:8080/saveUser?username=xiaoli&userPassword=111
    //保存用户
    @RequestMapping("/saveUser")
    public Users saveUser(Integer id, String username, String password){
   Users user = new Users(id,username,password);
         userDao.insert(user);
        return  user;
    }
//http://localhost:8080/updateUser?id=5&username=xiaoli&userPassword=111
    //修改用户
    @RequestMapping("/updateUser")
    public String updateUser(Integer id,String userName,String userPassword) {
        Users user = new Users(id,userName,userPassword);
        Integer index = userDao.updateById(user);
        if(index>0){
            return "修改用户成功,影响行数"+index+"行。";
        }else{
            return "修改用户失败,影响行数"+index+"行。";
        }
    }
  //http://localhost:8080/getUserById?userId=1
    //根据Id查询User
    @RequestMapping("/getUserById")
    public Users getUserById(Integer userId){
        return userDao.selectById(userId);
    }
  //http://localhost:8080/getUserListByPage?pageNumber=1&pageSize=2
    //条件分页查询
    @RequestMapping("/getUserListByPage")
    public List getUserListByPage(Integer pageNumber,Integer pageSize){
//方法一:
   List userList = userDao.selectPage(
                new Page(1, 10),
                new EntityWrapper().eq("username", "li")
        );
             return  userList;
//方法二
Page page =new Page<>(pageNumber,pageSize);
        EntityWrapper entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("username", "xiaoli");
        return userDao.selectPage(page,entityWrapper);*/
    }
    //http://localhost:8080/deleteUserById?userId=1
    //根据Id进行删除
    @RequestMapping("/deleteUserById")
    public Integer deleteUserById(Integer userId) {
        return userDao.deleteById(userId);
    }

 //http://localhost:8080/deleteUserListByName?username=xiaoyi

    //条件删除

    @RequestMapping("/deleteUserListByName")

    public Integer deleteUserListByName(String username)

    { Map map = new HashMap();

        map.put("username", username);

        return userDao.deleteByMap(map);

    }

}

你可能感兴趣的:(31,springboot 整合 Mybatis-Plus)