使用mybatisplus的service.page()分页时报错Creating a new SqlSession SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@5c5500f3] was not registered for synchronization because synchronization is not active Closing non transactional SqlSession [org.apache.ibatis.ses
时间: 2023-08-15 22:07:45 浏览: 110
sion.defaults.DefaultSqlSession@5c5500f3] was not registered for synchronization because synchronization is not active. This error typically occurs when using MyBatis-Plus's `service.page()` method for pagination without an active transaction.
To resolve this issue, you can try the following steps:
1. Make sure you have a transaction management mechanism in place. You can use Spring's `@Transactional` annotation or any other transaction management framework to manage transactions.
2. Ensure that the method calling `service.page()` is within a transactional context. If it's not, you can annotate the method with `@Transactional` or wrap the method call in a transactional block.
Here's an example of using `@Transactional` annotation:
```java
@Service
@Transactional
public class MyService {
@Autowired
private MyMapper myMapper;
public IPage<MyEntity> getPageData() {
Page<MyEntity> page = new Page<>(1, 10);
return myMapper.selectPage(page, null);
}
}
```
In this example, the `@Transactional` annotation ensures that the `selectPage` method is executed within a transactional context, allowing MyBatis-Plus to register the SqlSession for synchronization.
Remember to configure your transaction management mechanism properly based on your application's requirements and dependencies.
阅读全文