Mybatis-Plus特性:
- 无侵入,即引入它不会对现有工程产生影响。
- 损耗小,启动就会自动注入基本的CRUD,内置通用Mapper、Service等,基本能满足大部分需求。
- 支持主键自动生成,其中包括分布式唯一ID生成器Sequence,内置代码生成器,采用代码或者maven插件可快速生成Mapper、Service、Controller层代码。
- 内置分页插件,无需自己计算,且支持多种数据库,还内置性能分析插件,可打印SQL语句以及执行时间,便于慢SQL查询及优化。
框架结构
从零开始使用Mybatis-Plus(基于SpringBoot)
- 使用IDE新建一个SpingBoot工程,添加相关依赖(以MySQL为例):
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/>
</parent>
<groupId>com.brevity</groupId>
<artifactId>mp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mp</name>
<description>Mybatis-Plus Demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<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>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
</dependencies>
新建数据库mp,创建user表,插入了10条数据,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)
);
DELETE FROM user;
INSERT INTO `mp`.`user` (`id`, `name`, `age`, `email`) VALUES
(1, 'Tom', 18, 'Tom@mp.com'),
(2, 'Jack', 20, 'Jack@mp.com'),
(3, 'Brevity', 28, 'Brevity@mp.com'),
(4, 'Brevity_520', 22, 'Brevity_520@mp.com'),
(5, 'Billie', 24, 'Billie@mp.com'),
(6, 'Jone', 18, 'Jone@mp.com'),
(7, 'Jack', 20, 'Jack@mp.com'),
(8, 'John', 28, 'John@mp.com'),
(9, 'Sandys', 21, 'Sandys@mp.com'),
(10, 'Bill', 24, 'Bill@mp.com');
在application.properties配置文件中添加数据库配置,具体内容如下所示:
spring.datasource.url=jdbc:mysql://localhost:3306/mp?serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
在SpringBoot启动类中添加@MapperScan注解,用来扫描Mapper文件夹,如下所示:
@SpringBootApplication
@MapperScan("com.brevity.mp.mapper")
public class MpApplication {
public static void main(String[] args) {
SpringApplication.run(MpApplication.class, args);
}
}
编写User实体类以及UserMapper接口:
@Data // Lombok注解
public class User implements Serializable {
private static final long SerialVersionUID = 1L;
private Long id;
private String name;
private Integer age;
private String email;
}
public interface UserMapper extends BaseMapper<User> {
}
编写测试类开始测试代码:
@SpringBootTest
class MpApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void testMp() {
System.err.println("-------select method--------");
List<User> userList = userMapper.selectList(null);
Assert.assertEquals(10, userList.size());
userList.forEach(System.out::println);
}
}
至此,入门代码已经编写完毕,且测试成功,可以看到,没有编写任何xml文件就实现了查询,仅需要添加maven依赖,配置扫描注解即可,这就是它的强大之处之一。