赞
踩
MyBatis-Plus 是一个 Mybatis 增强版工具,在 MyBatis 上扩充了其他功能没有改变其基本功能,为了简化开发提交效率而存在。
官网文档地址:https://mp.baomidou.com/guide/
MyBatis-Plus 特性:https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7
(1)准备工作
(2)创建一个 SpringBoot 项目。
方式一:去官网 https://start.spring.io/ 初始化一个,然后导入 IDE 工具即可。
方式二:直接使用 IDE 工具创建一个。 Spring Initializer。
推荐使用国内:https://start.aliyun.com/
(3)添加 MyBatis-Plus 依赖(mybatis-plus-boot-starter)
- <!--mybatis-plus依赖-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.2</version>
- </dependency>
(4)为了测试开发,此处使用 mysql8,需要引入 mysql 相关依赖。
为了简化代码,引入 lombok 依赖(减少 getter、setter 等方法)。
- <!--mysql依赖-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!--lombok依赖-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
(5)在 <build> </build>里面添加如下进行yml、properties、xml自动扫描
- <build>
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.yml</include>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.yml</include>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
- </build>
(6)完整依赖文件(pom.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>
- <groupId>com.zwl</groupId>
- <artifactId>springboot_04</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>springboot_04</name>
- <description>Demo project for Spring Boot</description>
-
- <properties>
- <java.version>1.8</java.version>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
- <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
- </properties>
-
- <dependencies>
- <!--mybatis-plus-->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.2</version>
- </dependency>
-
- <!--mybatis-->
- <!-- <dependency>-->
- <!-- <groupId>org.mybatis.spring.boot</groupId>-->
- <!-- <artifactId>mybatis-spring-boot-starter</artifactId>-->
- <!-- <version>2.2.0</version>-->
- <!-- </dependency>-->
- <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.2.8</version>
- </dependency>
- <!-- log4j-->
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- <!-- jdbc-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <!-- web-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <!--mysql-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>8.0.26</version>
- </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>
- <!--lombok-->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </dependency>
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-extension</artifactId>
- <version>3.4.3.4</version>
- </dependency>
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <build>
- <!--配置扫描文件-->
- <resources>
- <resource>
- <directory>src/main/java</directory>
- <includes>
- <include>**/*.yml</include>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- <resource>
- <directory>src/main/resources</directory>
- <includes>
- <include>**/*.yml</include>
- <include>**/*.properties</include>
- <include>**/*.xml</include>
- </includes>
- <filtering>false</filtering>
- </resource>
- </resources>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.8.1</version>
- <configuration>
- <source>1.8</source>
- <target>1.8</target>
- <encoding>UTF-8</encoding>
- </configuration>
- </plugin>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <version>2.3.7.RELEASE</version>
- <configuration>
- <mainClass>com.zwl.Springboot04Application</mainClass>
- </configuration>
- <executions>
- <execution>
- <id>repackage</id>
- <goals>
- <goal>repackage</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
- </plugins>
- </build>
-
- </project>
(6)使用一个表进行测试。
仅供参考,可以定义 创建时间、修改时间等字段。
- DROP DATABASE IF EXISTS testMyBatisPlus;
- CREATE DATABASE testMyBatisPlus;
- USE testMyBatisPlus;
- DROP TABLE IF EXISTS user;
- CREATE TABLE user
- (
- id BIGINT(20) NOT NULL COMMENT '主键ID',
- name VARCHAR(30) NULL DEFAULT NULL COMMENT '姓名',
- age INT(11) NULL DEFAULT NULL COMMENT '年龄',
- email VARCHAR(50) NULL DEFAULT NULL COMMENT '邮箱',
- PRIMARY KEY (id)
- );
-
- INSERT INTO user (id, name, age, email) VALUES
- (1, 'Jone', 18, 'test1@qq.com'),
- (2, 'Jack', 20, 'test2@qq.com'),
- (3, 'Tom', 28, 'test3@qq.com'),
- (4, 'Sandy', 21, 'test4@qq.com'),
- (5, 'Billie', 24, 'test5@qq.com');
(7)在 application.yml 文件中配置 mysql 数据源信息。
mysql5如下配置
- spring:
- datasource:
- driver-class-name: com.mysql.jdbc.Driver
- url: jdbc:mysql://localhost:3306/testMyBatisPlus?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
- username: root
- password: root
- thymeleaf:
- cache: false #设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
- #修改端口号默认是8080
- server:
- port: 8888
- #mybatis-plus相关配置
- mybatis-plus:
- mapper-locations: classpath:mapper/*.xml #扫描mapper下的所有xml文件
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- type-aliases-package: com.zwl.entity #扫描实体类包/配置别名
mysql8如下配置
- # mysql连接信息
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/testMyBatisPlus?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true
- username: root
- password: root
- thymeleaf:
- cache: false #设置为false,否则会有缓存,导致页面没法及时看到更新后的效果。
- #修改端口号默认是8080
- server:
- port: 8888
- #mybatis-plus相关配置
- mybatis-plus:
- mapper-locations: classpath:mapper/*.xml #扫描mapper下的所有xml文件
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- type-aliases-package: com.zwl.entity #扫描实体类包/配置别名
(8)编写实体类、Service、ServiceImpl、UserMapper、UserMapper.xml等文件文件
方法一:手动编写(容易出错)
(1)编写表对应的 实体类User。
- import lombok.Data;
-
- @Data
- public class Users {
- private Long id;
- private String name;
- private int age;
- private String email;
- }
(2)编写操作实体类的 Mapper 类。
直接继承 BaseMapper,这是 mybatis-plus 封装好的类。
- import com.baomidou.mybatisplus.core.mapper.BaseMapper;
- import com.zx.mybatis_plus.bean.Users;
-
- public interface UsersMapper extends BaseMapper<Users> {
- }
BaseMapper封装了CRUD相关的方法
(3)实体类、Mapper 类都写好了,就可以使用了。
Step1:先得在启动类里扫描 Mapper 类,即添加 @MapperScan 注解
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- @MapperScan("com.zwl.mapper") //填写对应mapper存放位置,自动识别mapper下的所有**Mapper
- @SpringBootApplication
- public class MybatisplusDemoApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(MybatisplusDemoApplication.class, args);
- }
-
- }
Step2:写一个测试类测试一下。
- import com.zwl.entity.User;
- import com.zwl.mapper.UserMapper;
- 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 Springboot05ApplicationTests {
-
-
- @Autowired
- UserMapper userMapper;
-
- //查询所有用户
- @Test
- void contextLoads() {
- List<User> users = userMapper.selectList(null);
- for (User user : users) {
- System.out.println(user);
- }
- }
-
- //根据ID查询当前用户
- @Test
- void test1() {
- User user = userMapper.selectById(2);
- System.out.println(user);
- }
- }
方法二:通过 IDEA 的Database与Mybatisx插件自动生成
(9)总结:通过以上简单操作,就能对 user 表进行 CRUD 操作,不需要去编写 xml 文件。
注:若遇到 @Autowired 标记的变量出现 红色下划线,但是不影响 正常运行。
可以进入 Settings,找到 Inspection,并选择其中的 Spring Core -> Code -> Autowiring for Bean Class,将 Error 改为 Warning,即可。
如下:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。