第一步:在porm.xml文件中引入下面的jar
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
第二步:写配置文件(local-redis.properties)
#master IP
redis.master.host=192.168.4.92
#master Port
redis.master.port=6379
#slave IP
redis.slaver.host=192.168.4.92
#slave Port
redis.slaver.port=6379
#最大分配的对象数
redis.pool.maxTotal=1024
#最大能够保持idel状态的对象数
redis.pool.maxIdle=200
#当池内没有返回对象时,最大等待时间
redis.pool.maxWaitMillis=60000
#当调用borrow Object方法时,是否进行有效性检查
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)
<?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: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配置文件 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath*:redis.properties</value>
</list>
</property>
</bean> -->
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
<property name="maxTotal" value="${redis.pool.maxTotal}" />
<property name="maxIdle" value="${redis.pool.maxIdle}" />
<property name="numTestsPerEvictionRun" value="${redis.pool.numTestsPerEvictionRun}" />
<property name="timeBetweenEvictionRunsMillis" value="${redis.pool.timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${redis.pool.minEvictableIdleTimeMillis}" />
<property name="softMinEvictableIdleTimeMillis" value="${redis.pool.softMinEvictableIdleTimeMillis}" />
<property name="maxWaitMillis" value="${redis.pool.maxWaitMillis}" />
<property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
<property name="testWhileIdle" value="${redis.pool.testWhileIdle}" />
<property name="testOnReturn" value="${redis.pool.testOnReturn}" />
<property name="jmxEnabled" value="${redis.pool.jmxEnabled}" />
<property name="blockWhenExhausted" value="${redis.pool.blockWhenExhausted}" />
</bean>
<bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool">
<constructor-arg index="0" ref="jedisPoolConfig" />
<constructor-arg index="1">
<list>
<bean name="slaver" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.slaver.host}" />
<constructor-arg index="1" value="${redis.slaver.port}"
type="int" />
</bean>
<bean name="master" class="redis.clients.jedis.JedisShardInfo">
<constructor-arg index="0" value="${redis.master.host}" />
<constructor-arg index="1" value="${redis.master.port}"
type="int" />
</bean>
</list>
</constructor-arg>
</bean>
</beans>
第四步:依赖注入(在spring.xml中注入redis的对象)
//注入
<import resource="redis.xml"/>
//扫描
<context:component-scan base-package="com.hiersun.ecommerce.redis">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
第五步:编写redis的servicejava文件(RedisBaseService.java,实现类RedisBaseServiceImpl.java)
测试示例:(将session对象插入到redis中(就是存到内存中))
//SET Session
HttpSession session = request.getSession();
session.setAttribute(WebCnts.SESSION_KEY_LOGINUSER, user);
redisBaseService.setObj(String.valueOf(user.getId()), user);
SessionUser sUser = (SessionUser)redisBaseService.getObj(String.valueOf(user.getId()));
logger.debug("Login>>>>>>>sUserName = "+sUser.getUname());
spring mvc+mybaties+maven+redis
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=327106345&siteId=291194637
Recommended
Ranking