SpringBoot-EhCache使用
来源:互联网 发布:深宫遗梦 知乎 编辑:程序博客网 时间:2024/06/17 17:29
创建SpringBoot工程
创建如下类:
@Entitypublic class User { @Id @GeneratedValue private long id; @Column(nullable = false) private String name; @Column(nullable = false) private Integer age; ......省略getter和setter方法}
jpa查询接口
public interface UserRepository extends JpaRepository<User,Long> { User findByNameAndAge(String name, Integer age); @Query("from User u where u.name=:name") User findUser(@Param("name") String name); @Cacheable User findByName(String name);}
application.yml配置
spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 123456 driver-class-name: com.mysql.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true
Test测试类
@Autowired private UserRepository userRepository; @Before public void before() { userRepository.save(new User("BBB", 20)); } @Test public void test() throws Exception { User user1 = userRepository.findByName("BBB"); System.out.println("第一次查询:" + user1.getAge()); User user2 = userRepository.findByName("BBB"); System.out.println("第二次查询:" + user2.getAge()); }
运行结果图
引入EhCache缓存依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
在入口程序加入@EnableCaching注解
@SpringBootApplication@EnableCachingpublic class SpringbootCacheApplication { public static void main(String[] args) { SpringApplication.run(SpringbootCacheApplication.class, args); }}
数据接口中加入缓存注解
@CacheConfig(cacheNames = "user")public interface UserRepository extends JpaRepository<User,Long> { @Cacheable User findByName(String name);}
测试结果
注解含义:
@CacheConfig(cacheNames =”user”)将共用的查询结果放在user对象中
@Cacheable 配置了findByName函数的返回值将被加入缓存。同时在查询时,会先从缓存中获取,若不存在才再发起对数据库的访问
具体参考:(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/cache.html)
(http://blog.didispace.com/springbootcache1)
1 0
- SpringBoot-EhCache使用
- springboot中ehcache的使用
- springboot Ehcache
- SpringBoot项目实战--ehcache
- SpringBoot集成ehcache
- SpringBoot整合Ehcache
- SpringBoot系列(7)---SpringBoot-Cache(EhCache)
- springboot【15】缓存之EhCache
- SpringBoot集成shiro和ehcache
- SpringBoot学习:整合shiro(身份认证和权限认证),使用EhCache缓存
- 使用ehcache
- 使用ehcache
- 使用ehcache
- ehcache使用
- Ehcache 使用
- Ehcache 使用
- 使用ehcache
- EHCACHE使用
- HTML实现图片360度循环旋转
- 算法设计与应用基础:第十二周(1)
- 深度增强学习David Silver(二)——马尔科夫决策过程MDP
- Groovy入门教程
- 1.二维数组中的查找
- SpringBoot-EhCache使用
- [李景山php] 深入理解PHP内核[读书笔记]--第二章:用户代码执行--opcode
- 百度2017春招笔试真题编程题集合-1
- Spring之AOP
- 2017年 部分前端面试题?
- 手游对Lua性能分析
- 简单的MyBatis demo之数据库增删改查
- 【codevs3369】膜拜(dp)
- ionic生成应用图标和启动页