1.0 MyBatis-Plus 的入门案例
1.1 开发环境
IDE:2021
JDK: JDK1.8+
MySQL版本:5.7.3
SpringBoot:2.7.1
MyBatis-Plus:3.3.1
1.2 创建数据库及表:
打开Navicat运行以下SQL脚本进行建库建表
CREATE DATABASE `mybatis_plus` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; use `mybatis_plus`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL COMMENT '主键ID', `name` varchar(30) DEFAULT NULL COMMENT '姓名', `age` int(11) DEFAULT NULL COMMENT '年龄', `email` varchar(50) DEFAULT NULL COMMENT '邮箱', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入几条测试数据
INSERT INTO user (id, name, age, email) VALUES (1, 'Jone', 18, 'test1@baomidou.com'), (2, 'Jack', 20, 'test2@baomidou.com'), (3, 'Tom', 28, 'test3@baomidou.com'), (4, 'Sandy', 21, 'test4@baomidou.com'), (5, 'Billie', 24, 'test5@baomidou.com');
2.0 Springboot项目
2.1 创建springboot环境
这样我们的项目就创建完成了;
删掉多余的内容;(也可以不删除)
2.2 代码
2.2.1 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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>mybatisplus</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mybatisplus</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.29</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!--防止打war包出错--> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>
下载插件lombok:(没有的话自行下载)
配置数据源:
创建一个application.yml
spring: # 配置数据源信息 datasource: #配置数据源类型 type: com.zaxxer.hikari.HikariDataSource #配置连接数据的信息 driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&userSSL=false username: root password: root mybatis-plus: configuration: #加入mybatis 日志查看执行语句sql语句 log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
项目整体结构:
1. 创建实体类,引入lombok查看编译效果
我们通过点击右边的complie的按钮;如无报错责编已成功;
@Data
注解相当于=@Setter+@Getter(set方法,get方法;)+EqualsAndHashCode(Equals(重写)和HashCode
方法)–没有 有参构造需要可自己加(@AllArgsConstructor 有参构成+@NoArgsConstructor(无参构造)(无参构造也没有))
2.创建mapper继承BaseMapper
package com.example.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.example.pojo.User; import org.springframework.stereotype.Repository; //继承 BaseMapper<实体类> 即可调用crud方法 //标记持久层组件 @Repository public interface UserMapper extends BaseMapper<User> { }
3.写个测试类查看测试结果:
package com.example; import com.example.mapper.UserMapper; import com.example.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; @SpringBootTest public class MyBatisPlusTest { @Autowired private UserMapper userMapper; @Test public void testSelectList(){ //通过条件构造器查询一个list集合,若没有条件则可设置null List<User> users = userMapper.selectList(null); users.forEach(System.out::println); } }
4. 查看测试结果