1.MyBatisPlus简介
2. SpringBoot整合Mybatis-Plus
1. Spring初始化向导创建工程
2. 引入依赖
<!-- mybatis启动器-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- 数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3. 编写代码
1. 配置application.yaml
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?serverTimezone=GMT%2B8&characterEncoding=utf-8&useSSL=false
username: root
password: root
===========================
连接地址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
2. 启动类扫描
@SpringBootApplication
@MapperScan("com.atguigu.mybatisplusdemo1.mapper")
public class MybatisPlusDemo1Application {
public static void main(String[] args) {
SpringApplication.run(MybatisPlusDemo1Application.class, args);
}
}
3. 添加实体类
@Data
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
4. 添加mapper
BaseMapper是MyBatis-Plus提供的模板mapper,其中包含了基本的CRUD方法,泛型为操作的
实体类型
public interface UserMapper extends BaseMapper<User> {
}
3. 具体看文档
MyBatisPlus(SpringBoot版).pdf