- 1、开发环境
- 2、创建数据库表
- a> 创建数据库和表
- b> 添加数据
- 3、创建SpringBoot工程
- a> 初始化工程
- b> 引入依赖
- c> IDEA中安装lombok插件
- 4、编写代码
- a> 编写application.yml
- b> 启动类
- c> 添加实体
- d> 添加mapper
- e> 测试
- f> 添加日志
- IDE:IDEA 2020.3
- JDK:JDK8
- 构建工具:maven 3.6.3
- MySQL:MySQL8.0
- SpringBoot:2.6.6
- MyBatis-Plus:3.5.1
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;b> 添加数据
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' );3、创建SpringBoot工程 a> 初始化工程
使用Spring Initializr快速初始化一个Spring Boot工程。
c> IDEA中安装lombok插件 4、编写代码 a> 编写application.ymlorg.springframework.boot spring-boot-starter org.springframework.boot spring-boot-starter-test test com.baomidou mybatis-plus-boot-starter 3.5.1 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=GMT+8&characterset=utf-8&useSSL=false
username: root
password: wangyu123456
注意:
b> 启动类1、驱动类driver-class-name
spring boot 2.0(内置jdbc5驱动),驱动类使用:
driver-class-name:com.mysql.jdbc.Driver
spring boot 2.1及(内置jdbc5驱动),驱动类使用:
driver-class-name:com.mysql.jdbc.Driver
否则运行测试用例的时候会有WARN信息
2、连接地址url
MySQL5.7的版本的url:
jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&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包
@SpringBootApplication
@MapperScan("com.sweet.mybatisplus.mapper")
public class MybatisPlusApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusApplication.class, args);
}
}
c> 添加实体
@Data // lombok注解
public class User {
private Integer id;
private String name;
private Integer age;
private String email;
}
User类编译之后的结果:
BaseMapper是Mybatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的实体类型
public interface UserMapper extends BaseMappere> 测试{ }
@SpringBootTest
class MybatisPlusApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testSelectList() {
// selectList()根据MP内置的条件构造器查询一个list集合,null表示没有条件,即查询所有
userMapper.selectList(null).forEach(System.out :: println);
}
}
结果:
注意:
f> 添加日志IDEA在userMapper处报错,因为找不到注入的对象,因为类是动态创建的,但是程序可以正确的执行。
为了避免报错,可以在mapper接口上加上@Repository注解
在application.yml中配置日志输出
# 配置MyBatis日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl



