springboot 3配置mybaits3
时间: 2023-05-26 11:03:52 浏览: 114
把mybatis3整合到springboot3中,需要3个步骤:
1.添加相关的依赖包
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
```
2.配置数据源
在application.properties或application.yaml文件中配置数据源,例如:
```
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
```
3.配置MapperScan和Mapper接口
在主程序中添加`@MapperScan`注解,指定Mapper接口的路径,例如:
```
@SpringBootApplication
@MapperScan("com.example.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
```
同时,在Mapper接口上添加`@Mapper`注解,例如:
```
@Mapper
public interface UserMapper {
List<User> getAllUsers();
}
```
完成以上三个步骤后,就可以在Controller里面使用Mapper接口了:
```
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/users")
public List<User> getAllUsers() {
return userMapper.getAllUsers();
}
}
```
其中,User是一个实体类。
阅读全文
相关推荐
















