Ehcache二级缓存使用和不使用,iterate()方法查询的sql语句对比

Hibernate: select meg0_.id as col_0_0_ from Meg meg0_
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
java
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
.net
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
1 day
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
2 days
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
better than .net!
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
well!
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
just so so!
Hibernate: select meg0_.id as id2_2_, meg0_.content as content2_2_, meg0_.topic_id as topic3_2_2_, topic1_.id as id1_0_, topic1_.category_id as category3_1_0_, topic1_.title as title1_0_, category2_.id as id0_1_, category2_.name as name0_1_ from Meg meg0_ left outer join Topic topic1_ on meg0_.topic_id=topic1_.id left outer join Category category2_ on topic1_.category_id=category2_.id where meg0_.id=?
good!

Hibernate: select meg0_.id as id2_, meg0_.content as content2_, meg0_.topic_id as topic3_2_ from Meg meg0_
Hibernate: select topic0_.id as id1_1_, topic0_.category_id as category3_1_1_, topic0_.title as title1_1_, category1_.id as id0_0_, category1_.name as name0_0_ from Topic topic0_ left outer join Category category1_ on topic0_.category_id=category1_.id where topic0_.id=?
Hibernate: select topic0_.id as id1_1_, topic0_.category_id as category3_1_1_, topic0_.title as title1_1_, category1_.id as id0_0_, category1_.name as name0_0_ from Topic topic0_ left outer join Category category1_ on topic0_.category_id=category1_.id where topic0_.id=?
Hibernate: select topic0_.id as id1_1_, topic0_.category_id as category3_1_1_, topic0_.title as title1_1_, category1_.id as id0_0_, category1_.name as name0_0_ from Topic topic0_ left outer join Category category1_ on topic0_.category_id=category1_.id where topic0_.id=?
java
.net
1 day
2 days
better than .net!
well!
just so so!
good!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Spring Boot整合Ehcache解决Mybatis二级缓存数据脏读的详细步骤: 1. 添加依赖 首先,需要在pom.xml文件中添加Ehcache和Spring Boot的相关依赖: ``` <!-- Ehcache依赖 --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.6</version> </dependency> <!-- Spring Boot依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> ``` 2. 配置Ehcache 在src/main/resources目录下创建ehcache.xml文件,并添加以下配置: ``` <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <!-- 默认缓存配置 --> <defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true"> </defaultCache> <!-- Mybatis二级缓存配置 --> <cache name="com.example.demo.mapper.UserMapper" maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" statistics="true"> </cache> </ehcache> ``` 在配置文件中,我们定义了默认缓存配置和针对UserMapper的缓存配置。 3. 配置Mybatis 在application.properties中添加Mybatis相关配置: ``` # Mybatis配置 mybatis.type-aliases-package=com.example.demo.entity mybatis.mapper-locations=classpath:mapper/*.xml mybatis.configuration.cache-enabled=true mybatis.configuration.local-cache-scope=session mybatis.configuration.cache-ref=ehcache ``` 注意,cache-enabled属性设置为true,local-cache-scope属性设置为session,并指定了使用缓存提供者为Ehcache。 4. 配置缓存管理器 在启动类中配置缓存管理器: ``` @Configuration @EnableCaching public class CacheConfig { @Bean public CacheManager cacheManager() { EhCacheCacheManager cacheManager = new EhCacheCacheManager(); cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject()); return cacheManager; } @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean(); cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); cacheManagerFactoryBean.setShared(true); return cacheManagerFactoryBean; } } ``` 在这里,我们通过EhCacheCacheManager和EhCacheManagerFactoryBean来配置缓存管理器。 5. 开启缓存 最后,在Mapper接口中开启缓存: ``` @CacheNamespace(implementation = net.sf.ehcache.Cache.class, eviction = FifoPolicy.class) public interface UserMapper { @Select("SELECT * FROM user WHERE id = #{id}") @Options(useCache = true, flushCache = Options.FlushCachePolicy.FALSE, timeout = 10000) User selectById(Integer id); @Update("UPDATE user SET name = #{name} WHERE id = #{id}") void updateNameById(@Param("id") Integer id, @Param("name") String name); } ``` 使用@CacheNamespace注解来指定缓存实现类和缓存策略,在每个查询方法使用@Options注解来开启缓存。 至此,Spring Boot整合Ehcache解决Mybatis二级缓存数据脏读的配置就完成了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值