我的环境是:
Spring Boot + Mybatis + Oracle 10g + Jdk1.8
我在甲骨文中得到了一个名为 book 的表,如下所示:
+---------+------+--------+
| book_id | name | number |
+---------+------+--------+
| 1 | b1 | 123 |
| 2 | b2 | 123 |
| 3 | b3 | 2343 |
+---------+------+--------+
3 rows in set (0.00 sec)
我让它工作并用我编写的映射器成功显示它.但是在我使用 plsql 在此表中再插入 2 条记录后,当我使用 mybatis 映射器查询它时,我仍然得到了相同的 3 条记录而不是所有 5 条记录(如下所示)。
+---------+------+--------+
| book_id | name | number |
+---------+------+--------+
| 1 | b1 | 123 |
| 2 | b2 | 123 |
| 3 | b3 | 2343 |
| 4 | b4 | 22343 |
| 5 | b5 | 43 |
+---------+------+--------+
5 rows in set (0.00 sec)
然后我像这样改变预言机中的缓存策略。
alter table book nocache
它又起作用了!所有 5 条记录都使用 mybatis 映射器成功显示。但是为什么?我应该每次都清除预言机缓存吗?这感觉不对。有没有更好的解决方案?任何答案都会有所帮助。感谢
应用程序属性
# Spring
spring.resources.static-locations=classpath:/static/
# MyBatis
mybatis.configuration.map-underscore-to-camel-case=true
mybatis.configuration.local-cache-scope=statement
# DataSource 1
spring.datasource.db1.url=jdbc:oracle:thin:@*******
spring.datasource.db1.username=***
spring.datasource.db1.password=***
spring.datasource.db1.driver-class-bookName=oracle.jdbc.OracleDriver
映射
@Mapper
@Qualifier("bookMapper")
public interface BookMapper {
@Select({"select * from book"})
@Options(useCache = false)
@Results({
@Result(property = "bookId",column = "book_id"),
@Result(property = "bookName",column = "book_name"),
@Result(property = "bookNumber",column = "book_number")
})
List<BookEntity> getALL();
// insertBook();
}
实体
public class BookEntity {
long bookId;
String bookName;
int bookNumber;
//getters and setters
}
该问题与缓存清除无关。
我认为除了在你insert( a DML )
声明后发布commit
之外没有问题。
如果您没有发出commit
则只能从另一个会话中获取 3 条记录。
但是在你发出之后,
,更改表簿无缓存
( a DDL )
则会发生隐式提交,您可以看到所有 5 条记录。