具体步骤:
- 创建一个Mysql数据库,并导入数据;
- 新建一个springboot工程;
- 引入相关依赖;
- 如果第一次使用Lombok还需在IDEA中安装对应插件;
- 在application.properties配置文件中添加Mysql数据库相关的配置信息;
- 编写一个实体类;
- 编写一个映射接口;
- 在spring boot启动类上添加
@MapperScan
注解,扫描Mapper文件夹; - 编写测试用例。
工程结构
详细内容
创建一个Mysql数据库,并导入数据;
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 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');
新建一个springboot工程,引入相关依赖;
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>mybatis_plus_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis_plus_demo</name>
<description>Demo project for Spring Boot</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
如果第一次使用Lombok还需在IDEA中安装对应插件;
在application.properties配置文件中添加Mysql数据库相关的配置信息;
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8
spring.datasource.password=1234
spring.datasource.username=root
# 配置sql输出日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
编写一个实体类;
package com.example.mybatis_plus_demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
//@TableId(type = IdType.ID_WORKER) mp自带策略,生成19位值,数字类型使用这种策略,比如long
//@TableId(type = IdType.ID_WORKER_STR) mp自带策略,生成19位值,字符串类型使用这种策略
//@TableId(type = IdType.INPUT) 设置id值
//@TableId(type = IdType.UUID) 随机唯一值
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
private String email;
}
编写一个映射接口;
package com.example.mybatis_plus_demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatis_plus_demo.entity.User;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
}
在spring boot启动类上添加@MapperScan
注解,扫描Mapper文件夹;
package com.example.mybatis_plus_demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.example.mybatis_plus_demo.mapper")
@SpringBootApplication
public class MybatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemoApplication.class, args);
}
}
编写测试用例。
package com.example.mybatis_plus_demo;
import com.example.mybatis_plus_demo.entity.User;
import com.example.mybatis_plus_demo.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class MybatisPlusDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void findAll() {
List<User> users = userMapper.selectList(null);
System.out.println(users);
}
@Test
void addUser() {
User user = new User();
user.setName("lucy");
user.setAge(30);
user.setEmail("lucy@qq.com");
int insert = userMapper.insert(user);
System.out.println("insert:" + insert);
}
}
运行查询全部的测试用例