springboot整合mybatis的基本步骤
时间: 2023-11-24 11:52:13 浏览: 40
以下是springboot整合mybatis的基本步骤:
1.在pom.xml文件中添加mybatis-spring-boot-starter依赖:
```xml
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
```
2.在application.properties文件中配置数据库连接信息:
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
3.创建Mapper接口和Mapper.xml文件,Mapper接口中定义SQL语句,Mapper.xml文件中编写SQL语句。
4.在启动类中添加@MapperScan注解,指定Mapper接口所在的包路径:
```java
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```