springboot中使用mybatis时的注意点有哪些
时间: 2024-06-11 17:07:37 浏览: 156
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分页查询
### 如何在 Spring Boot 中用 MyBatis 实现分页查询
#### 使用 MyBatis-Plus 的分页插件
为了有效管理大数据集并防止内存溢出,在 Spring Boot 项目中可以利用 MyBatis-Plus 提供的内置分页功能。这使得开发者能够轻松地对数据库记录进行分片检索,而无需手动编写复杂的 SQL 查询语句[^1]。
首先需要引入 `mybatis-plus-boot-starter` 和其他必要的依赖项到项目的构建文件(如 Maven 或 Gradle)。接着配置好数据源以及 MyBatis Plus 配置类之后就可以开始设置分页拦截器了:
```java
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
@Configuration
public class MyBatisConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
```
完成上述配置后,便可以在 Service 层定义带有分页参数的方法,并调用 Mapper 接口中的查询方法来获取分页后的结果集合。这里需要注意的是,Service 方法应当加上 `@Transactional` 注解以确保操作处于 Spring 管理下的事务环境中,从而避免不必要的 JDBC 连接问题[^2]。
下面是一个简单的例子展示怎样在一个 User 表上实施分页逻辑:
假设有一个名为 `UserMapper.xml` 文件用于映射用户实体对象与对应的 CRUD 操作,则可在其中声明如下形式的选择列表:
```xml
<select id="selectUsersByPage" resultType="com.example.demo.entity.User">
SELECT * FROM t_user WHERE deleted=0 ORDER BY create_time DESC LIMIT #{start},#{size};
</select>
```
而在 Java 代码层面则可以通过创建一个服务类来进行具体的业务处理:
```java
@Service
public class UserServiceImpl implements IUserService {
private final UserMapper userMapper;
public UserServiceImpl(UserMapper userMapper){
this.userMapper = userMapper;
}
@Override
@Transactional(rollbackFor = Exception.class)
public Page<User> getUsersByPage(int currentPage, int pageSize) throws ServiceException{
try {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
// 设置条件...
Page<User> page = new Page<>(currentPage,pageSize);
IPage<User> iPage = userMapper.selectPage(page,queryWrapper);
List<User> records = iPage.getRecords();
long totalElements = iPage.getTotal();
return new PageImpl<>(records, PageRequest.of(currentPage - 1, pageSize),totalElements );
} catch (Exception e) {
throw new ServiceException("Failed to get users by page.");
}
}
}
```
此段程序展示了如何基于传入的当前页面编号 (`currentPage`) 及每页大小(`pageSize`) 来计算起始位置,并最终返回封装好的 `Page<T>` 对象给前端应用以便进一步渲染表格或其他 UI 组件[^4]。
对于 RESTful API 开发者来说,还可以考虑直接暴露 JSON 数据作为响应体的一部分,这样客户端可以直接解析这些结构化信息而不必关心底层的具体实现细节[^5]。
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)