【【尚硅谷】2022版MyBatisPlus教程(一套玩转mybatis-plus)】
IDE:IDEA 2022.2.2
JDK:JDK8+
构建工具:Maven 3.8.6
MySQL 版本:MySQL5.7.38
SpringBoot:2.3.7.RELEASE
Mybatis-Plus:官方最新版3.5.2
create database `mybatis_plus`;
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');
select * from user;
使用 Spring Initializr 快速初始化一个 Spring Boot 工程
依赖不勾
Create
一个全新的Springboot 工程
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&characterEncoding=utf8&useSSL=false
username: root
password: 200039
【注意】
1 驱动类driver-class-name
spring boot 2.0(内置jdbc5驱动),驱动类使用:
driver-class-name: com.mysql.jdbc.Driver
spring boot 2.1及以上(内置jdbc8驱动),驱动类使用:
driver-class-name: com.mysql.cj.jdbc.Driver
【否则运行测试用例会有WARN 信息】
2 连接地址URL
MySQL5.7版本的url:jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=UTC&characterEncoding=utf8&useSSL=false
MySQL8.0版本的url:jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
否则运行测试用例报告如下错误:
java.sql.SQLException: The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more
在Spring Boot启动类中添加@MapperScan注解,扫描mapper包:
package com.dingjiaxiong.mybatisplus;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.dingjiaxiong.mybatisplus.mapper")
public class MybatisPlusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemoApplication.class, args);
}
}
package com.dingjiaxiong.mybatisplus.domain;
import lombok.Data;
/**
* ClassName: User
* date: 2022/10/12 19:38
*
* @author DingJiaxiong
*/
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
User类编译之后的结果:
BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型
package com.dingjiaxiong.mybatisplus.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dingjiaxiong.mybatisplus.domain.User;
/**
* ClassName: UserMapper
* date: 2022/10/12 19:39
*
* @author DingJiaxiong
*/
public interface UserMapper extends BaseMapper<User> {
}
package com.dingjiaxiong.mybatisplus;
import com.dingjiaxiong.mybatisplus.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class MybatisPlusDemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList(){
userMapper.selectList(null).forEach(System.out::println);
}
}
结果:
OK
IDEA在 userMapper 处报错,
因为找不到注入的对象,因为类是动态创建的,但是程序可以正确
的执行。
为了避免报错,可以在mapper接口上添加 @Repository 注解
在application.yml中配置日志输出
# 配置mybatis 日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
OK。