springboot3整合mybatis
时间: 2023-09-12 15:11:12 浏览: 114
springboot与mybatis整合
在Spring Boot中整合MyBatis有几个步骤。首先,你需要在项目的pom文件中添加MyBatis的依赖项。具体来说,你需要添加以下依赖项:
```xml
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
```
接下来,你需要创建一个配置文件来配置MyBatis和数据库连接。你可以在application.yml或application.properties文件中添加以下配置项:
```yaml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/test1?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
mybatis:
mapper-locations: classpath:/mappers/*.xml
type-aliases-package: com.buba.pojo
```
然后,在启动类上添加`@MapperScan`注解,指定Mapper接口的位置。例如,如果你的Mapper接口在`com.buba.mappers`包下,你可以在启动类上添加以下注解:
```java
@MapperScan("com.buba.mappers")
@SpringBootApplication
public class YourApplication {
public static void main(String[] args) {
SpringApplication.run(YourApplication.class, args);
}
}
```
接着,你需要创建Mapper接口和对应的Mapper XML文件。在XML文件中,你可以使用`<mapper>`标签指定命名空间和SQL语句。例如,你可以创建一个名为`TestMapper`的Mapper接口和对应的XML文件,并在XML文件中编写查询语句:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.buba.mappers.TestMapper">
<select id="selectNameAll" resultType="string">
select name from user
</select>
</mapper>
```
最后,你可以在Service层中注入Mapper接口,并在Controller层中调用Service方法来使用MyBatis进行数据库操作。例如,你可以创建一个名为`TestServiceImpl`的Service实现类,并在其中注入`TestMapper`接口:
```java
@Service
public class TestServiceImpl implements TestService {
@Autowired
private TestMapper testMapper;
@Override
public List<String> selectNameAll() {
return testMapper.selectNameAll();
}
}
```
然后,你可以创建一个名为`TestController`的Controller类,并调用Service中的方法来处理请求:
```java
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestService testService;
@GetMapping("/selectNameAll")
public List<String> selectNameAll() {
return testService.selectNameAll();
}
}
```
请根据你的具体需求修改以上代码,并确保在启动应用程序之前完成数据库的配置和表的创建。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Springboot整合3.整合mybatis](https://blog.csdn.net/zhangchen124/article/details/124208920)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [SpringBoot(3)整合Mybatis](https://blog.csdn.net/weixin_38380811/article/details/129863316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文