SpringBoot学习(4)整合MyBatis-Plus

概述:MyBatis-Plus是中国开发团队对MyBatis进行增强的一个框架,使用起来非常6666,下面进行整合mybatis-plus

 

一:创建一个新工程

 

二、准备mybatis-plus依赖:

<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter -->
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>

注意此时要删除一下spring-boot-starter依赖,因为mybatis-plus-boot-starter依赖已经包含了该依赖

 

三、准备与上一章节内容一样的数据表

https://www.cnblogs.com/lrc123/p/16079415.html

 

四、编写配置文件application.yml

 

五、准备实体类

package com.lrc.domain;

import lombok.Data;
import org.springframework.stereotype.Component;

/**
 * @param
 * @author lrc
 * @create 2022/3/30
 * @return
 * @description
 **/
@Component
@Data
public class Book {
    private Integer id;
    private String type;
    private String name;
    private String description;
}

 

六:编写BookDao接口继承BaseMapper<T>类,BaseMapper<T>类就已经封装好了各种CRUD的操作,我们无需再手工编写SQL语句了

package com.lrc.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lrc.domain.Book;
import org.apache.ibatis.annotations.Mapper;


/**
 * @param
 * @author lrc
 * @create 2022/3/30
 * @return
 * @description
 **/

@Mapper
public interface BookDao extends BaseMapper<Book> {
}

BaseMapper<T>封装的操作如下:

 

 

七、编写测试类:

package com.lrc;

import com.lrc.dao.BookDao;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootWithMybatisplusApplicationTests {

    @Autowired
    private BookDao bookDao;

    @Test
    void contextLoads() {
        System.out.println(bookDao.selectById(1));
    }

}

执行测试:

下面再试下获取全表记录:再编写一个测试方法:

@Test
void testAll(){
    System.out.println(bookDao.selectList(null));
}

执行:

 

八:mybatis-plus常用的功能:

1、开启SQL执行日志,在配置文件中添加如下配置:

mabatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

现在来查询下全表记录:

可以看到,在执行SQL的时候,输出了日志信息。

 

 

2、分页查询

(1)、配置拦截器。编写一个拦截器配置类,并用@Configuration表明这是一个配置类

package com.lrc.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @param
 * @author lrc
 * @create 2022/3/31
 * @return
 * @description
 **/
@Configuration
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());//创建乐观锁拦截器 OptimisticLockerInnerInterceptor
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL)); //插件分页拦截器,我的是mysql
        return mybatisPlusInterceptor;
    }
}

(2)编写测试方法,测试分页查询功能:

@Test
void testPageSelect(){
    Page<Book> page=new Page<>(1,5);
    bookDao.selectPage(page,null);
}

执行查看日志发现分页查询功能完成。

 

3、条件查询

(3.1)使用QueryWrapper进行条件过滤

@Test
void testConditionSelect(){
    QueryWrapper<Book> wrapper=new QueryWrapper();
    wrapper.like("name","Spring");//相当于select * from book where name like "%Spring%"
    bookDao.selectList(wrapper);
}

执行:

 

(3.2)使用Lamda表达式进行条件查询:由于QueryWrapper进行条件查询时,属性名有可能会写错,那么使用LambdaQueryWrapper可以在编译时就进行校验。

@Test
void testLambdaSelect(){
    LambdaQueryWrapper<Book> lambdaQueryWrapper=new LambdaQueryWrapper<>();
    lambdaQueryWrapper.like(Book::getName,"Spring");
    bookDao.selectList(lambdaQueryWrapper);
}

 

 

4、插入操作

@Test
void testInsert(){
    Book book=new Book();
    book.setType("测试插入数据123");
    book.setName("测试插入数据123");
    book.setDescription("测试插入数据123");
    bookDao.insert(book);  //相当于 insert into book(type,name,description) values(?,?,?)
}

在执行过程中,可能会发生错误,如下图:

出现该问题的原因是,mybatis在id类型使用的是雪花算法,与我们数据库设计的字段类型不匹配,那么接下来进行下纠正。

在配置文件中添加:id-type: auto:

再来执行:

 

5、更新操作

@Test
void testUpdate(){
    Book book=new Book();
    book.setId(11);
    book.setType("测试插入数据asfs");
    book.setName("测试插入数据fdd");
    book.setDescription("测试插入数据fsd");
    bookDao.updateById(book); //相当于 update book set type="",name="",description="" where id=11
}

 

6、删除操作:

@Test
void testDelete(){
    bookDao.deleteById(10); //相当于 delete from book where id=10
}

posted @ 2022-03-30 23:56  筱筱创  阅读(242)  评论(0编辑  收藏  举报