Spring Boot MyBatis 及 MyBatis-Plus 禁用一级缓存

MyBatis MyBatis-Plus Spring Boot About 650 words

场景

在同一个session中(大多场景是同一次请求中),需要多次执行相同的查询语句,来判断是否有数据变更。

一级缓存

在以上场景下,MyBatis/MyBatis-Plus因为默认开启了一级缓存,所以执行相同查询SQL(大多是同一个Mapper的同一个方法)时便直接从Java本地缓存中查询了,不再发送SQL从数据库读取最新数据。

相关配置

application.yaml配置文件中添加相关配置。

MyBatis

mybatis:
  configuration:
    local-cache-scope: statement

MyBatis-Plus

mybatis-plus:
  configuration:
    local-cache-scope: statement

备注

排查一级缓存是否禁用可以配合SQL日志输出,如果命中了一级缓存则日志没有输出。

MyBatis

mybatis:
  configuration:
    local-cache-scope: statement
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

MyBatis-Plus

mybatis-plus:
  configuration:
    local-cache-scope: statement
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Views: 230 · Posted: 2024-08-29

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb/LiteNote

扫描下方二维码关注公众号和小程序↓↓↓

扫描下方二维码关注公众号和小程序↓↓↓


Today On History
Browsing Refresh