springboot + mybatis 引包
时间: 2023-12-07 08:40:01 浏览: 87
以下是在Spring Boot项目中引入MyBatis的步骤:
1.在pom.xml文件中添加MyBatis和MyBatis-Spring的依赖:
```xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
```
2.在application.properties或application.yml文件中配置数据源信息:
```yaml
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
```
3.在MyBatis的Mapper接口中编写SQL语句,并在Mapper.xml文件中进行映射。
4.在Spring Boot的启动类中添加@MapperScan注解,指定Mapper接口所在的包路径:
```java
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
阅读全文