Mybatis Plus#
约 65 个字 6 行代码 预计阅读时间 2 分钟
入门案例#
创建项目,导入相关依赖:
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 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>3.3.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.luguosong</groupId>
<artifactId>mybatis-plus-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mybatis-plus-hello</name>
<description>mybatis-plus-hello</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--MyBatis-Plus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.7</version>
</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>
application.yml文件配置数据库连接:
YAML
spring:
datasource:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/learn_mybatis
username: root
password: 12345678
在 Spring Boot 启动类中添加 @MapperScan 注解,扫描 Mapper 文件夹:
Java
package com.luguosong.mybatisplushello;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.luguosong.mybatisplushello.mapper")
public class MybatisPlusHelloApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusHelloApplication.class, args);
}
}
编写实体类:
Java
package com.luguosong.mybatisplushello.entity;
import lombok.Data;
import java.sql.Date;
/**
* @author luguosong
*/
@Data
public class Employees {
private Integer id;
private String firstName;
private String lastName;
private String position;
private Date hireDate;
private Integer departmentId;
}
编写 Mapper 接口类:
Java
package com.luguosong.mybatisplushello.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.luguosong.mybatisplushello.entity.Employees;
/**
* @author luguosong
*/
public interface EmployeesMapper extends BaseMapper<Employees> {
}
编写测试类:
Java
package com.luguosong.mybatisplushello;
import com.luguosong.mybatisplushello.entity.Employees;
import com.luguosong.mybatisplushello.mapper.EmployeesMapper;
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 MybatisPlusHelloApplicationTests {
@Autowired
private EmployeesMapper employeesMapper;
@Test
void testSelect() {
List<Employees> employees = employeesMapper.selectList(null);
System.out.println(employees);
}
}