web项目是采用maven构建的,使用的是spring boot
1.下载redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.配置文件properties,配置redis
spring.redis.database=1
spring.redis.host=101.201.110.197
spring.redis.port=6379
spring.redis.password=scmsafecachezyyw
spring.redis.timeout=0
spring.redis.pool.max-active=8
spring.redis.pool.max-idle=8
spring.redis.pool.max-wait=-1
spring.redis.pool.min-idle=0
3.重写实体类的toStirng 方法
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
4.在Dao层加入缓存注解@Cacheable(“类名”)
@MyBatisDao
@Cacheable("OrderTrafficDao")
public interface OrderTrafficDao extends CrudDao<OrderTraffic>{
@Cacheable(value = "OrderTraffics", unless = "#result == null")
List<OrderTraffic> findTrafficAndMembers(OrderTraffic entity);
List<Member> findMemberByTraffic(OrderTraffic ot);
}