【Mybatis-Plus】03 SpringBoot整合

创建SpringBoot工程:

选择辅助三件套:

再导入MP相关依赖坐标:

<!-- jdbc -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

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

<!-- spring-jdbc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

<!-- spring-web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

创建MP的分页配置类:

package cn.echo42.configuration;

import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author DaiZhiZhou
 * @file MP-SpringBoot
 * @create 2020-08-06 4:38
 */

@Configuration
@ConditionalOnClass({PaginationInterceptor.class})
public class MybatisPlusConfiguration {

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

}

application.yml配置:

#数据源
spring:
  datasource:
    # 数据源类型
    type: org.springframework.jdbc.datasource.DriverManagerDataSource 
    # 驱动目录
    driver-class-name: com.mysql.cj.jdbc.Driver
    # 数据库访问url
    url: jdbc:mysql://127.0.0.1:3306/oa?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=UTC
    # 访问账号
    username: root
    # 访问密码
    password: 123456
#配置mybatis-plus
mybatis-plus:
  # mapper映射器位置
  mapper-locations:
    - classpath:mapper/*Mapper.xml
  # 全局配置
  global-config:
    # 数据库配置
    db-config:
      # 主键类型 自动设置
      id-type: auto
    banner: true

复制之前Spring整合MP的配置:

User.java、UserMapper.java、UserMapper.xml

 

注意在启动函数这里注解上@MapperScan

package cn.echo42;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("cn.echo42.mapper")
public class MpSpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(MpSpringbootApplication.class, args);
    }

}

测试类则是SpringBoot现成提供好的:

我们需要自动装配下userMapper,测试方法CV过来就行

package cn.echo42;

import cn.echo42.mapper.UserMapper;
import cn.echo42.pojo.User;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
class MpSpringbootApplicationTests {

    @Autowired
    UserMapper userMapper;

    @Test
    void contextLoads() {
    }

    @Test
    public void mpInsert() {
        int insert = userMapper.insert(new User(
                null,
                "阿伟001",
                "133778",
                1,
                0
        ));
    }

    @Test
    public void mpUpdate() {

        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();

        updateWrapper.eq("user_id", 6); // WHERE user_id = 6

        // updateWrapper.between("id", 10, 20); WHERE id BETWEEN 10 AND 20

        int update = userMapper.update(new User(
                null,
                "杰哥001",
                "133778",
                1,
                0
        ), updateWrapper);

    }

    @Test
    public void mpDelete() {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<User>();

        updateWrapper.eq("user_id", 6); // WHERE user_id = 6

        // updateWrapper.between("id", 10, 20); WHERE id BETWEEN 10 AND 20

        int delete = userMapper.delete(updateWrapper);
    }

    @Test
    public void mpQuery() {
        User user = userMapper.selectById(3);
        System.out.println(user);
    }

    @Test
    public void mpQuery2() {
        List<Serializable> list = new ArrayList<Serializable>();
        list.add(2);
        list.add(4);

        List<User> userList = userMapper.selectBatchIds(list);

        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void mpQuery3() {
        Map<String, Object> columnMapping = new HashMap<String, Object>();

        columnMapping.put("user_is_del", 0);
        columnMapping.put("user_password", "123456");

        List<User> userList = userMapper.selectByMap(columnMapping);

        for (User user : userList) {
            System.out.println(user);
        }
    }

    @Test
    public void mpQuery4() {

        QueryWrapper<User> userWrapper = new QueryWrapper<User>();

        // 假装这是从控制器传递过来的字符串参数
        String fromUrlParam = "user";

        userWrapper.like(fromUrlParam != null , "user_name", fromUrlParam);

        // 用来查询符合筛选条件的记录数量
        Integer integer = userMapper.selectCount(userWrapper);

        System.out.println("return rows : " + integer );
    }

    @Test
    public void mpQuery5() {

        IPage<User> page = new Page<User>(1, 5);

        userMapper.selectPage(page, null);

        long total = page.getTotal();

        System.out.println("TotalRecords : " + total);

        List<User> list = page.getRecords();

        for (User user : list) {
            System.out.println(user);
        }
    }

}

没打印日志,看不到SQL输出结果,但是测试通过就算有效果了

 

posted @ 2020-08-06 05:04  emdzz  阅读(149)  评论(0编辑  收藏  举报