使用的是maven,所以直接上依赖包了
Web pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.tgb</groupId> <artifactId>dui1dui.parent</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath>../dui1dui.parent/pom.xml</relativePath> </parent> <artifactId>dui1dui.user.web</artifactId> <packaging>war</packaging> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- 表示开发的时候引入,发布的时候不会加载此包 --> </dependency> <!-- spring核心包 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> </dependency> <!-- mybatis/spring包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency> <!-- 导入java ee jar 包 --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <scope>provided</scope> </dependency> <!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> </dependency> <!-- 日志文件管理包 --> <!-- log start --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </dependency> <!-- 格式化对象,方便输出日志 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </dependency> <!-- log end --> <!-- 映入JSON --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> </dependency> <!-- 上传组件包 --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> </dependency> <dependency> <!-- 通用的编码解码算法。包括一些语音编码器,Hex,Base64 --> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <!-- redis整合包 --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.6.RELEASE</version> </dependency> <!-- 客户端依赖 --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.5.0</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> </build> </project>
有了依赖包接下来就要进行集成配置了
Spring-context配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自动扫描 --> <context:component-scan base-package="dui1dui.merchant.core" /> <!-- 启用缓存注解功能,这个是必须的,否则注解不会生效,另外,该注解一定要声明在spring主配置文件中才会生效 --> <cache:annotation-driven cache-manager="cacheManager" /> <!--读取属性文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:redis.properties</value> <value>classpath:jdbc.properties</value> </list> </property> </bean> <!-- redis --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxWaitMillis" value="${redis.maxWait}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> </bean> <!-- redis 连接工厂,可配置多个redis --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:host-name="${redis.host}" p:port="${redis.port}" p:pool-config-ref="poolConfig" p:database="${redis.database}" /> <!-- spring 对客户端redis实现 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!--缓存管理实现,使用的是jedis客户端实现类--> <bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager"> <constructor-arg index="0" ref="redisTemplate"/> </bean> </beans>
redis.properties文件
# Redis settings # server IP redis.host=localhost # server port redis.port=6379 # use dbIndex redis.database=0 redis.maxIdle=300 redis.maxWait=3000 redis.testOnBorrow=true // #指明是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个
如何使用缓存呢,这里我们根据上篇Spring缓存注解进行使用,同时也要对SPEL表达式有一点的了解
Service操作
/** * 商家操作类 */ package dui1dui.merchant.core; import java.util.List; import javax.annotation.Resource; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service; import dui1dui.merchant.dao.UserInfoMapper; import dui1dui.merchant.entity.User; /** * * 商家业务操作类 * @author hanyk * @version 1.0.0 , 下午2:28:15 */ @Service public class D1dMerchantServiceImpl implements D1dMerchantService { @Resource private UserInfoMapper userinfoMapper; @Override @Cacheable(value="username") public User selectByPrimaryKey(int userId) { System.out.println("qingqiu"); return userinfoMapper.selectByPrimaryKey(userId); } @Override @Cacheable("selectall") public List<User> selectAll() { List<User> users = this.userinfoMapper.selectAll(); System.out.println("查询所有"); return users; } //清空username 缓存 @CacheEvict(value="username",allEntries=true) public void diselect(){ System.out.println("清空username"); } //对名称是hanhan 的进行缓存 @Cacheable(value="name",condition="#name=='hanhan'") public User selectByName(String name) { User user =userinfoMapper.selectByName(name); System.out.println(user); return user; } }
controller
@Controller public classScoreManagerController { @Autowired private D1dMerchantServiced1dMerchantService; JacksonJsonUtil jsonutill = newJacksonJsonUtil(); /** * 根据主键查询 * @return * @throws NamingException */ @RequestMapping("/querylist") public String querylist2(HttpServletRequestrequest, HttpServletResponse response,Modelmodel) throws NamingException { Useruser = d1dMerchantService.selectByPrimaryKey(3); model.addAttribute("user",user); return "/showInfo"; } //查询所有内容 @RequestMapping("/selectall") public String selectall(HttpServletRequestrequest, HttpServletResponse response)throws NamingException { List<User>users = d1dMerchantService.selectAll(); System.out.println(users); return "/showInfo"; } //清空缓存 @RequestMapping("/diselect") public String disname() { d1dMerchantService.diselect(); System.out.println("sdf");; return"/showInfo"; } //根据用户名称查询用户 @RequestMapping("/getname") public String getname(){ User user =d1dMerchantService.selectByPrimaryKey(3); d1dMerchantService.selectByName(user.getUsername()); return"/showInfo"; } }
运行效果
为了确保运行效果,我们现将查询主键的保存的缓存清楚
1 执行diselect方法
2 在querylist查询数据
打印控制台如下
3 再次执行在querylist查询数据
没有调用后台数据库方法
该jedis与spring集成是通过配置文件以及客户端实现类进行缓存,减少了多余的实现代码,同时也省去了缓存的业务逻辑,基本实现了Aop的效果。