mybatis简单易学,易用,灵活度高,但是稍微显得工作量大一些,需要自己写SQL,自动生成工具生成的sql有限。
mybatis plus是一个mybatis的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
我们建一个spring boot项目,来快速跑起来一个mybatis plus demo。
1、新建pom.xml,添加的依赖如下:
<?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">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>study-mybatis-plus</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
2、application.yml添加配置信息,这里主要是数据库配置信息
# DataSource Config
spring:
datasource:
driver-class-name: org.h2.Driver
schema: classpath:db/schema-h2.sql
data: classpath:db/data-h2.sql
url: jdbc:h2:mem:test
username: root
password: test
3、初始化表和数据,在resource下新建db文件加,添加两个文件
1)添加表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)
);
2)添加表中数据sql
DELETE FROM user;
INSERT INTO user (id, name, age, email) VALUES
(1, 'Jone', 18, 'test1@baomidou.com'),
(2, 'Jack', 20, 'test2@baomidou.com'),
(3, 'Tom', 28, 'test3@baomidou.com'),
(4, 'Sandy', 21, 'test4@baomidou.com'),
(5, 'Billie', 24, 'test5@baomidou.com');
4、新建User实体类
package com.itzhimei.mybatis.plus.model;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
5、新建mapper类,mapper类继承自mybatis-plus的BaseMapper
package com.itzhimei.mybatis.plus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itzhimei.mybatis.plus.model.User;
public interface UserMapper extends BaseMapper<User> {
}
6、写测试类
package com.itzhimei.mybatis.plus.test;
import com.itzhimei.mybatis.plus.mapper.UserMapper;
import com.itzhimei.mybatis.plus.model.User;
import org.junit.Assert;
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 java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SampleTest {
@Autowired
private UserMapper userMapper;
@Test
public void testSelect() {
System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(5, userList.size());
userList.forEach(System.out::println);
}
}
输出结果:
----- selectAll method test ------
User(id=1, name=Jone, age=18, email=test1@baomidou.com)
User(id=2, name=Jack, age=20, email=test2@baomidou.com)
User(id=3, name=Tom, age=28, email=test3@baomidou.com)
User(id=4, name=Sandy, age=21, email=test4@baomidou.com)
User(id=5, name=Billie, age=24, email=test5@baomidou.com)
我们来看一下测试类中的代码:
测试类中负责查询的是这一行:userMapper.selectList(null)
但是我们实际写的UserMapper中是空的,但是可以直接进行列表查询,原因是因为继承了BaseMapper,BaseMapper中定义了增删改查的方法。
BaseMapper虽然是接口,内部只是声明了方法,但是我们在用mybatis的mapper的时候,其实也是这样,这些接口中的方法,只是做了声明,没有具体实现,因为都是框架内部来处理的,mybatis是通过mapper的方法名映射到了xml中的sql,mybatis-plus到mapper这里,剩下的工作同mybatis一样,由框架处理,只不过mybatis-plus因为功能增强了,内部的逻辑比mybatis多了一些。