SSM 框架集成redis
2018-03-09 16:33
429 查看
1.添加maven依赖:(注意:spring-data-redis包的版本要与spring的版本一致哦!)
2.redis.properties 配置文件
redis.host=127.0.0.1 #IP地址
redis.port=6379 #端口号
redis.password= #密码为空
redis.maxIdle=300 #最大空闲连接数
redis.maxWaitMillis=1000 #连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1
redis.maxTotal=600 #最大活动对象数
redis.testOnBorrow=true #获取连接的时候检查有效性, 默认false
redis.testOnReturn=true #调用return Object方法时,是否进行有效性检查
测试效果:
<!-- 导入redis包 --> <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-redis --> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.8.4.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
2.redis.properties 配置文件
redis.host=127.0.0.1 #IP地址
redis.port=6379 #端口号
redis.password= #密码为空
redis.maxIdle=300 #最大空闲连接数
redis.maxWaitMillis=1000 #连接时的最大等待毫秒数,小于零:阻塞不确定的时间,默认-1
redis.maxTotal=600 #最大活动对象数
redis.testOnBorrow=true #获取连接的时候检查有效性, 默认false
redis.testOnReturn=true #调用return Object方法时,是否进行有效性检查
3.配置application-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd "> <!--扫描redis配置文件 --> <context:property-placeholder location="classpath:redis.properties" /> <!--设置连接池 --> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="${redis.maxIdle}" /> <property name="maxTotal" value="${redis.maxTotal}" /> <property name="maxWaitMillis" value="${redis.maxWaitMillis}" /> <property name="testOnBorrow" value="${redis.testOnBorrow}" /> <property name="testOnReturn" value="${redis.testOnReturn}" /> </bean> <!--设置链接属性 --> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:hostName="${redis.host}" p:port="${redis.port}" p:password="${redis.password}" p:pool-config-ref="poolConfig" p:timeout="100000" /> <!-- Jedis模板配置 --> <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <!-- 如果不配置Serializer,那么存储的时候智能使用String,如果用User类型存储,那么会提示错误User can't cast to String!!! --> <property name="keySerializer"> <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" /> </property> <property name="valueSerializer"> <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" /> </property> </bean> </beans>
4.单元测试redis
import java.util.logging.Logger; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration({"classpath:applicationContext-redis.xml"}) public class TestRedis { @Autowired private RedisTemplate redisTemplate; private static final Logger log = Logger.getLogger(TestRedis.class.getName()); @Test public void test(){ redisTemplate.opsForValue().set("username", "熊二"); log.info("value:"+redisTemplate.opsForValue().get("username")); } }
测试效果:
相关文章推荐
- SSM框架快速集成redis
- ssm框架下redis的集成
- 基于JavaEE的SSM框架集成Redis
- SSM框架集成mysql读写分离,动态加载数据源
- 带你逐步深入了解SSM框架——淘淘商城项目之redis缓存
- ssm集成框架freemarker
- ssm(spring+springMVC+Mybatis)框架集成Mongodb
- java SSM 框架 微信自定义菜单 快递接口 SpringMVC mybatis redis shiro ehcache websocket
- SSM框架集成(二)springmvc的集成
- java SSM 框架 多数据源 代码生成器 websocket即时通讯 shiro redis
- Redis分布式框架搭建与集成
- 关于SSM(Spring+SpringMVC+Mybatis)三大框架集成验证码插件Kaptcha简单方法
- SSM框架集成整合(Spring+SpringMVC+mybatis)
- 一分钟 SSM框架集成 swagger
- SSM框架中集成TKMybatis+PageHelper实现高效查询和分页
- Redis分布式框架搭建与集成
- SSM集成Redis做缓存
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
- SSM框架搭建(Spring+SpringMVC+MyBatis)与easyui集成并实现增删改查实现
- 【SSM框架】Spring + Springmvc + Mybatis 基本框架搭建集成教程