这篇主要介绍Springboot整合MybatisPlus,另外介绍一个插件JBLSpringbootAppGen,以及一个经常用于测试的基于内存的h2数据库。
Mybatisplus是mybatis的增强工具,和tk-mybatis相似,但功能更强大,可避免重复CRUD语句,先来一个简单入门:
1.安装一个插件:JBLSpringbootAppGen:
File->Setting->Plugin: 搜索点击Install,重启IDEA
这个工具主要用于创建启动类时自动创建配置文件。
2.新建一个modle: mybatis-plus-demo;
3.添加pom依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lxj</groupId>
<artifactId>mybatis-plus-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<mybatisplus.version>3.3.2</mybatisplus.version>
<skipTests>true</skipTests>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency><!--基于内存的数据库-->
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<dependency><!--测试框架-->
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency><!--简化实体类开发,加@Data注解不用写get、set方法-->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
4.通过工具添加启动类:
5.设置配置项:(注意每一项冒号后面有个空格!)
自动创建的配置文件为yml,和properties文件比结构层级更清晰一些:
#Datasource Configspring:spring:
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
#y用于测试可随便写
url: jdbc:h2:mem:test
username: roottest
passworld: test123
#Logger Config
logging:
level:
com.lxj.quickstart: debug
6.引入测试用的数据库:
一个是创建表的sql文件:
DROP TABLE IF EXISTS user;
CREATE TABLE user
(
id BIGINT(20) NOT NULL COMMENT '主键ID',
name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
age INT(11) NULL DEFAULT NULL COMMENT '年龄',
email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
PRIMARY KEY (id)
)
一个是插入数据的sql文件:
DELETE FROM user;
INSERT INTO user (id,name,age,email) VALUES
(1,'Jone',18,'test1@abc.com'),
(2,'Jack',19,'test2@abc.com'),
(3,'Tom',20,'test3@abc.com'),
(4,'Sandy',18,'test4@abc.com'),
(5,'hahaha',19,'test5@abc.com'),
(6,'Billie',18,'test6@abc.com');
将两个文件放到classpath下:
7.编写实体类:
package com.lxj.quickstart.entity;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
@Data注解可给类中属性自动添加get/set方法。
8.编写dao类:
package com.lxj.quickstart.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lxj.quickstart.entity.User;
public interface UserMapper extends BaseMapper<User> {
}
9.启动类中别忘了添加扫描包路径:
@MapperScan("com.lxj.quickstart.mapper")
10.添加测试类测试:
package com.lxj.quickstart.mapper;
import com.lxj.quickstart.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.lxj.quickstart.mapper.UserMapper;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect(){
List<User> list = userMapper.selectList(null);
list.forEach(System.out::println);
}
}
11.结果: