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.DruidDataSourcedruid:url: jdbc:mysql://localhost:3306/carresellerusername: rootpassword: ???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接口:
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方法启动类
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 {@Autowiredprivate UserMapper userMapper;//加Test注解,为了能执行单个测试方法@Testpublic void test(){//该方法查询全部//参数是一个Wrapper,条件结构器,这里先不用 填nullList<User> user = userMapper.selectList(null);System.out.println(user);}
}