SpringBoot如何同时整合mybatis和mybatisplus
时间: 2024-10-06 13:06:31 浏览: 44
Spring Boot 整合 MyBatis 和 MyBatisPlus 可以通过以下几个步骤完成:
1. **添加依赖**:
- 首先,在你的`pom.xml`文件中添加Spring Boot对MyBatis和MyBatisPlus的支持依赖。对于MyBatis,你需要 `spring-boot-starter-data-jpa` 或者 `spring-boot-starter-jdbc`,以及MyBatis本身的依赖。对于MyBatisPlus,需要引入 `mybatis-plus-spring-boot-starter`。
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 或者 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot-starter</artifactId>
<version>版本号</version>
</dependency>
```
记得替换`版本号`为实际的MyBatisPlus最新稳定版版本。
2. **配置数据库连接**:
在`application.properties`或`application.yml`中设置JDBC相关的配置,包括数据源、驱动程序等信息。
```properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
3. **创建实体和Mapper**:
- 定义实体类,让它们继承自`BaseEntity`(MyBatisPlus的基类),并保持一致的命名规则。
- 创建Mapper接口,继承自MyBatisPlus提供的`BaseMapper`。
```java
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
public interface UserMapper extends BaseMapper<User> {
}
```
4. **扫描Mapper和Repository**:
在Spring Boot的配置类上使用`@MapperScan`和`@ComponentScan`注解,指定扫描Mapper和Repository的位置。
```java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@Configuration
@EnableJpaRepositories(basePackages = "com.example.demo.repository")
@MapperScan("com.example.demo.mapper") // 替换为你自己的Mapper包名
public class AppConfig {
@Bean
public DataSource dataSource() {
// 数据源配置...
}
}
```
5. **注入Repository**:
在服务类中,你可以直接注入实现了对应接口的`UserRepository`,然后像操作普通Spring Data JPARepository那样使用。
```java
@Service
public class UserService {
private final UserRepository userRepository;
@Autowired
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}
public User getUserById(Long id) {
return userRepository.findById(id).orElse(null);
}
}
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](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)
![](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)
![](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)
![](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)
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)