一、创建项目
添加application.yml配置文件
添加启动类
在resources中添加mappers文件夹
在java中添加entity、dao、service、controller包
二、在pom.xml中添加依赖
<!-- MyBatisPlus依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!--LomBok依赖-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId> <optional>true</optional>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
三、在application.yml中配置数据源
server:
port: 8080
spring:
# 配置数据源信息
datasource:
# 配置数据源类型
type: com.zaxxer.hikari.HikariDataSource
# 配置连接数据库信息
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/practice?characterEncoding=utf-8&useSSL=false
username: root
password: root
#mybatisPlus相关配置
mybatis-plus:
mapper-locations: classpath:mappers/*.xml
#配置默认实体类包名
type-aliases-package: demo.entity
configuration:
# 配置MyBatis日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
四、在启动类Demo.java中扫描dao
package demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("demo.dao")
public class Demo {
public static void main(String[] args) {
SpringApplication.run(Demo.class, args);
}
}