spring mvc+mybaties+maven+redis
第一步:在porm.xml文件中引入下面的jar
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
第二步:写配置文件(local-redis.properties)
#masterIP
redis.master.host=192.168.4.92
#masterPort
redis.master.port=6379
#slaveIP
redis.slaver.host=192.168.4.92
#slavePort
redis.slaver.port=6379
#最大分配的对象数
redis.pool.maxTotal=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=60000
#当调用borrowObject方法时,是否进行有效性检查
redis.pool.testOnBorrow=true
#多长时间检查一次连接池中空闲的连接
redis.pool.timeBetweenEvictionRunsMillis=30000
#空闲连接多长时间后会被收回
redis.pool.minEvictableIdleTimeMillis=30000
redis.pool.softMinEvictableIdleTimeMillis=10000
redis.pool.numTestsPerEvictionRun=1024
#缓存过期时间秒1000*60*60*24*7七天
#redis.pool.expire=604800000
#是否开启Redis服务应用
redis.pool.unlock=false
redis.pool.testWhileIdle=true
redis.pool.testOnReturn=true
redis.pool.jmxEnabled=true
redis.pool.blockWhenExhausted=false
第三步:写配置文件(redis.xml)
<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"xmlns:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--加载公用的jdbc和db配置文件-->
<!--<beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="systemPropertiesModeName"value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<propertyname="ignoreResourceNotFound"value="true"/>
<propertyname="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean>-->
<beanid="jedisPoolConfig"class="redis.clients.jedis.JedisPoolConfig">
<propertyname="maxTotal"value="${redis.pool.maxTotal}"/>
<propertyname="maxIdle"value="${redis.pool.maxIdle}"/>
<propertyname="numTestsPerEvictionRun"value="${redis.pool.numTestsPerEvictionRun}"/>
<propertyname="timeBetweenEvictionRunsMillis"value="${redis.pool.timeBetweenEvictionRunsMillis}"/>
<propertyname="minEvictableIdleTimeMillis"value="${redis.pool.minEvictableIdleTimeMillis}"/>
<propertyname="softMinEvictableIdleTimeMillis"value="${redis.pool.softMinEvictableIdleTimeMillis}"/>
<propertyname="maxWaitMillis"value="${redis.pool.maxWaitMillis}"/>
<propertyname="testOnBorrow"value="${redis.pool.testOnBorrow}"/>
<propertyname="testWhileIdle"value="${redis.pool.testWhileIdle}"/>
<propertyname="testOnReturn"value="${redis.pool.testOnReturn}"/>
<propertyname="jmxEnabled"value="${redis.pool.jmxEnabled}"/>
<propertyname="blockWhenExhausted"value="${redis.pool.blockWhenExhausted}"/>
</bean>
<beanid="shardedJedisPool"class="redis.clients.jedis.ShardedJedisPool">
<constructor-argindex="0"ref="jedisPoolConfig"/>
<constructor-argindex="1">
<list>
<beanname="slaver"class="redis.clients.jedis.JedisShardInfo">
<constructor-argindex="0"value="${redis.slaver.host}"/>
<constructor-argindex="1"value="${redis.slaver.port}"
type="int"/>
</bean>
<beanname="master"class="redis.clients.jedis.JedisShardInfo">
<constructor-argindex="0"value="${redis.master.host}"/>
<constructor-argindex="1"value="${redis.master.port}"
type="int"/>
</bean>
</list>
</constructor-arg>
</bean>
</beans>
第四步:依赖注入(在spring.xml中注入redis的对象)
//注入
<importresource="redis.xml"/>
//扫描
<context:component-scanbase-package="com.hiersun.ecommerce.redis">
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/>
<context:exclude-filtertype="annotation"expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
第五步:编写redis的servicejava文件(RedisBaseService.java,实现类RedisBaseServiceImpl.java)
测试示例:(将session对象插入到redis中(就是存到内存中))
//SETSession
HttpSessionsession=request.getSession();
session.setAttribute(WebCnts.SESSION_KEY_LOGINUSER,user);
redisBaseService.setObj(String.valueOf(user.getId()),user);
SessionUsersUser=(SessionUser)redisBaseService.getObj(String.valueOf(user.getId()));
logger.debug("Login>>>>>>>sUserName="+sUser.getUname());