springboot整合mybatis
时间: 2023-08-24 07:08:10 浏览: 59
Spring Boot 整合 MyBatis 的方法如下:
1. 在 pom.xml 文件中添加 MyBatis 的依赖:
```
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
```
2. 配置 MyBatis 的相关参数:
在 application.properties 文件中添加以下配置:
```
# MyBatis 配置
mybatis.type-aliases-package=com.example.demo.model
mybatis.mapper-locations=classpath:mapper/*.xml
```
3. 编写 Mapper 接口:
```
public interface UserMapper {
User findById(Integer id);
}
```
4. 编写 Mapper XML 文件:
```
<mapper namespace="com.example.demo.mapper.UserMapper">
<select id="findById" resultType="com.example.demo.model.User">
select * from user where id = #{id}
</select>
</mapper>
```
5. 在启动类中使用 Mapper 接口:
```
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/user/{id}")
public User getUserById(@PathVariable Integer id) {
return userMapper.findById(id);
}
}
```
以上就是 Spring Boot 整合 MyBatis 的过程,有任何问题欢迎继续提问!
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)