1. MyBatis-Plus快速入门
- 版本 3.5.31
- 并非另起炉灶 , 而是MyBatis的增强 , 使用之前依然要导入MyBatis的依赖 , 且之前MyBatis的所有功能依然可以使用.
- 局限性是仅限于单表操作, 对于多表仍需要手写
项目结构:
先导入依赖,比之前多了一个mybatis-plus-boot-starter
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
</parent>
<groupId>com.sunsplanter</groupId>
<artifactId>Spring-With-MybatisPlus</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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>
</dependency>
<!-- mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- 数据库相关配置启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- druid启动器的依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.20</version>
</dependency>
<!-- 驱动类-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
连接池配置:
# 连接池配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
url: jdbc:mysql://localhost:3306/carreseller
username: root
password: ???
driver-class-name: com.mysql.cj.jdbc.Driver
实体类:
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mapper接口:文章来源:https://www.toymoban.com/news/detail-808349.html
package com.sunsplanter.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunsplanter.pojo.User;
//
public interface UserMapper extends BaseMapper<User> {
//之前,mapper接口内要声明抽象方法,然后再mapper.xml中具体写查询语句
//List<User> queryAll();
//现在,继承自BaseMapper,其内已定义了几乎所有单表查询语句
//故UserMapper也拥有了这些方法,无需再写
}
采用测试类的方法,不需要定义main方法启动类文章来源地址https://www.toymoban.com/news/detail-808349.html
package com.sunsplanter.test;
import com.sunsplanter.mapper.UserMapper;
import com.sunsplanter.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
//SpringBoot的专有测试注解,可以帮我们自动初始化IoC容器,想要什么直接拿即可
@SpringBootTest
public class SpringWithMyBatisPlusTest {
@Autowired
private UserMapper userMapper;
//加Test注解,为了能执行单个测试方法
@Test
public void test(){
//该方法查询全部
//参数是一个Wrapper,条件结构器,这里先不用 填null
List<User> user = userMapper.selectList(null);
System.out.println(user);
}
}
到了这里,关于01 MyBatisPlus快速入门的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!