SpringBoot 整合 MyBatisPlus框架入门
步骤1:创建 maven 工程
创建一个空 Maven 工程,如下:
步骤2:pom.xml 文件中添加 MyBatisPlus 相关依赖
<dependencies>
<!-- mybatispulus 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.7</version>
</dependency>
<!-- mysql 驱动依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- SpringBoot 单元测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>2.7.16</version>
<scope>test</scope>
</dependency>
<!-- junit测试框架依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--lombok插件依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.30</version>
<optional>true</optional>
</dependency>
</dependencies>
步骤3:创建数据库表 employee
使用数据库 GUI 工具 Navicat 新建数据库 mybatisplus
, 如下:
创建数据库员工表 employee
,如下
建表的 SQL 语句如下:
CREATE TABLE employee(
emp_id BIGINT(20) NOT NULL,
name VARCHAR(30),
emp_gender VARCHAR(6),
age INT,
email VARCHAR(50),
PRIMARY KEY(emp_id)
);
INSERT INTO employee(emp_id,name,emp_gender,age,email)
VALUES(1367686308726788098,'刘晓娟','女',20,'liuxianjuan@qq.com');
INSERT INTO employee(emp_id,name,emp_gender,age,email)
VALUES(1367709299695099906,'张春雨','男',28,'zhangchunyu@sina.com');
INSERT INTO employee(emp_id,name,emp_gender,age,email)
VALUES(1367717669156028418,'何雨柱','男',23,'heyuzhu@126.com');
步骤4:构建数据模型 Employee 类
创建 com.binge.model 包用于存放数据模型类,如下:
Employee 类代码如下:
package com.binge.model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
private Long empId;
private String name;
private String empGender;
private Integer age;
private String email;
}
步骤5:创建 application.properties 文件配置 MyBatisPlus
在 resources 文件下创建 springboot 的全局配置文件 application.properties ,如下:
配置内容如下:
### 数据库配置
#设置数据库URL地址
spring.datasource.url=jdbc:mysql://localhost:3306/mybatisplus?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
#设置数据库用户名
spring.datasource.username=root
#设置数据库密码
spring.datasource.password=password
#设置数据库驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
### mybatisPlus配置
#设置数据模型所在的包
mybatis-plus.type-aliases-package=com.binge.model
#设置SQL语句控制台日志打印
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
#设置驼峰命名转换
mybatis-plus.configuration.map-underscore-to-camel-case=true
注意:请设置自己的数据库用户名和密码
步骤6:编写启动类
- com.binge 包下新建 mapper 包如下:
- com.binge 包下新建 springboot 启动类 MybatisPlusDemoApplication.java,如下:
MybatisPlusDemoApplication.java 如下:
@SpringBootApplication
@MapperScan("com.binge.mapper")
public class MybatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemoApplication.class, args);
}
}
步骤7:编写 Mapper 接口
在 mapper 包中创建 EmployeeMapper.java 接口,该接口继承 BaseMapper<Employee>
,如下:
EmployeeMapper.java 接口如下:
public interface EmployeeMapper extends BaseMapper<Employee> {
}
步骤8:启动服务测试类
在 test/java 文件夹下新建 com.binge.MybatisPlusTests.java 测试类,如下:
MybatisPlusTests.java 如下:
@RunWith(SpringRunner.class)
@SpringBootTest
public class MybatisPlusTests {
@Resource
private EmployeeMapper employeeMapper;
@Test
public void testSelect(){
List<Employee> employeeList = employeeMapper.selectList(null);
employeeList.forEach( System.out::println);
}
}
执行测试方法 testSelect() 查询 employee 表中所有信息,结果如下:
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a6c18ad] was not registered for synchronization because synchronization is not active
2024-07-10 09:38:28.503 INFO 13164 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2024-07-10 09:38:29.208 INFO 13164 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
JDBC Connection [HikariProxyConnection@542598487 wrapping com.mysql.cj.jdbc.ConnectionImpl@574cd322] will not be managed by Spring
==> Preparing: SELECT emp_id,name,emp_gender,age,email FROM employee
==> Parameters:
<== Columns: emp_id, name, emp_gender, age, email
<== Row: 1367686308726788098, 刘晓娟, 女, 20, liuxianjuan@qq.com
<== Row: 1367709299695099906, 张春雨, 男, 28, zhangchunyu@sina.com
<== Row: 1367717669156028418, 何雨柱, 男, 23, heyuzhu@126.com
<== Total: 3
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4a6c18ad]
Employee(empId=1367686308726788098, name=刘晓娟, empGender=女, age=20, email=liuxianjuan@qq.com)
Employee(empId=1367709299695099906, name=张春雨, empGender=男, age=28, email=zhangchunyu@sina.com)
Employee(empId=1367717669156028418, name=何雨柱, empGender=男, age=23, email=heyuzhu@126.com)
测试成功:)