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)

#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());
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值