
【【尚硅谷】2022版MyBatisPlus教程(一套玩转mybatis-plus)】
2 入门案例 文章目录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;2.2.2 添加数据
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;2.3 创建SpringBoot 工程 2.3.1 初始化工程
使用 Spring Initializr 快速初始化一个 Spring Boot 工程
依赖不勾
Create
一个全新的Springboot 工程
2.3.2 引入依赖2.3.3 IDEA 中安装lombok 插件 2.4 编写代码 2.4.1 配置application.ymlcom.baomidou mybatis-plus-boot-starter 3.5.2 org.projectlombok lombok true mysql mysql-connector-java runtime
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
2.4.2 启动类【注意】
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);
}
}
2.4.3 添加实体
package com.dingjiaxiong.mybatisplus.domain;
import lombok.Data;
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
2.4.4 添加mapperUser类编译之后的结果:
BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型
package com.dingjiaxiong.mybatisplus.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.dingjiaxiong.mybatisplus.domain.User; public interface UserMapper extends BaseMapper2.4.5 测试{ }
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
2.4.6 添加日志IDEA在 userMapper 处报错,
因为找不到注入的对象,因为类是动态创建的,但是程序可以正确
的执行。
为了避免报错,可以在mapper接口上添加 @Repository 注解
在application.yml中配置日志输出
# 配置mybatis 日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
OK。