springboot中使用mybatis时的注意点有哪些
时间: 2024-06-11 10:07:37 浏览: 12
1. 配置mybatis的配置文件mybatis-config.xml或者mybatis-config.yml
2. 配置数据源,可以使用springboot自带的数据源,也可以使用其他第三方数据源
3. 使用@Mapper注解标注mapper接口,或者在配置类中使用@MapperScan注解扫描mapper接口
4. 在mapper接口中使用@Select、@Insert、@Update等注解定义sql语句
5. 如果需要进行分页查询,可以使用PageHelper插件来实现分页功能
6. 使用事务注解@Transactional来保证数据的一致性
7. 如果需要使用mybatis-plus来进行增删改查操作,需要在pom.xml中引入mybatis-plus的依赖,并且配置mybatis-plus的相关配置信息。
相关问题
springboot中使用mybatis拦截器
在Spring Boot中使用Mybatis拦截器,需要进行以下步骤:
1.定义拦截器类,实现Mybatis的Interceptor接口。
```java
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public class MyInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
//拦截逻辑
return invocation.proceed();
}
@Override
public Object plugin(Object target) {
return Plugin.wrap(target, this);
}
@Override
public void setProperties(Properties properties) {
//设置拦截器属性
}
}
```
2.在Spring Boot的配置文件中配置拦截器。
```yaml
mybatis:
configuration:
#配置拦截器
#注意:mybatis下的configuration属性是Mybatis的Configuration对象,不是Spring Boot的配置文件
#使用Mybatis的配置文件时需要使用mybatis.config-location属性
#使用Spring Boot的配置文件时需要使用mybatis.configuration属性
#两者不能同时使用
plugins:
- com.example.MyInterceptor
```
3.在Mybatis的Mapper接口中使用@Intercepts注解指定拦截器。
```java
@Intercepts({
@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class})
})
public interface UserMapper {
List<User> selectAll();
}
```
其中,@Intercepts注解用于指定拦截器,@Signature注解用于指定拦截的方法。
总的来说,在Spring Boot中使用Mybatis拦截器的过程和在其他环境下使用Mybatis拦截器的过程类似,只需要在Spring Boot的配置文件中配置拦截器即可。
springboot 哪有mybatis的配置文件
在SpringBoot中,可以将MyBatis的配置文件写在application.yml或application.properties文件中,也可以单独写一个mybatis-config.xml文件。如果选择将配置文件写在application.yml或application.properties文件中,可以使用以下方式进行配置:
```yaml
mybatis:
# MyBatis全局配置文件位置
config-location: classpath:mybatis/mybatis-config.xml
# MyBatis映射文件位置
mapper-locations: classpath:mybatis/mapper/*.xml
```
如果选择单独写一个mybatis-config.xml文件,可以在application.yml或application.properties文件中使用以下方式进行配置:
```yaml
mybatis:
# MyBatis全局配置文件位置
config-location: classpath:mybatis-config.xml
```
需要注意的是,如果同时在application.yml或application.properties文件中配置和单独写mybatis-config.xml文件,以单独写的mybatis-config.xml文件为准。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.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)