myabtis-plus使用
官网地址https://baomidou.com/guide/
1.导入官网的数据库
2.创建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.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mybatis-plus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatis-plus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <!--导入mp的依赖 mybatis-plus 就不需要导入 mybatis! --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <!-- spring2.x 都是默认配置mysql8的驱动 <mysql.version>8.0.19</mysql.version> 8.0 以上的驱动,需要配置时区! --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <!-- IDEA中需要安装lombok插件! --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </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> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.编写数据库对象的实体user对象,编写userMapper接口,userMapper继承BaseMapper<User> ,其中User为对应的实体
package com.example.dao; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.pojo.User; import org.springframework.stereotype.Repository; // 使用了 MP 之后,我们就不用在编写万恶的CRUD了,只需要继承一个类别即可! // BaseMapper<T> 泛型T 就是表示你要操作的类 // 这里内置几乎所有的查询,除了十分特殊的查询之外! // MP,对于复杂的SQL 依旧需要自己编写! @Repository public interface UserMapper extends BaseMapper<User> { // 链表查询 // 1、编写接口 // 2、编写接口对应的Mapper,这个就是和MyBatis一样的操作! }
4.测试使用即可!