这两天经过网上各种搜索,终于成功的写出了一个关于redis缓存的demo。
第一步,准备所需的jar包;
jar包版本不匹配会出现各种各样的错误,经过多次尝试,最终成功运行的jar包版本:
jedis-2.4.2.jar,
spring-data-redis-1.3.4.RELEASE.jar,
spring-data-commons-1.8.0.RELEASE.jar,
commons-pool2-2.4.2.jar,
spring相关jar包用的是4.1.7.RELEASE版本,
全部的jar包(可能有些没用,懒的去整理了)
第二步,配置redis.properties和springmvc.xml文件;
redis.properties代码如下,主要配置了MySQL数据库连接和redis连接的属性值:
username=root
password=root
url=jdbc:mysql://localhost:3306/xk?characterEncoding=utf-8
driverClassName=com.mysql.jdbc.Driver
redis.hostName=127.0.0.1
redis.port=6379
redis.timeout=15000
redis.usePool=true
redis.maxIdle=300
redis.maxTotal=1024
redis.maxWaitMillis=1000
redis.testOnBorrow=true
springmvc.xml代码如下,我是写到了一起(在网上见到好多都是分开写的):
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:redis="http://www.springframework.org/schema/redis"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis-1.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="${url}"></property>
<property name="driverClassName" value="${driverClassName}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 扫描dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zh.dao"></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/mapper/*.xml"></property>
</bean>
<!-- 开启扫描 -->
<context:component-scan base-package="com"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix