pom.xml文件加入
<!-- config redis data and client jar--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.0.2.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.1.0</version></dependency>
在spring-dao.xml中加在同一个目录下创建
redis-context.xml
<!-- 引入同文件夹下的redis属性配置文件 -->
<import resource="redis-context.xml"/>
redis-context.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" default-lazy-init="false"> <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="100"/><!-- 最大闲置 --> <property name="minIdle" value="10"/><!-- 最小闲置 --> <property name="testOnBorrow" value="true"/><!-- 可以获取 --> </bean> <!-- redis 配置,也可以把配置挪到properties配置文件中,再读取 --> <bean id="jedisPool" class="redis.clients.jedis.JedisPool"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1" value="127.0.0.1" name="host" type="java.lang.String"/> <constructor-arg index="2" value="6379" name="port" type="int"/> <constructor-arg index="3" value="5000" name="timeout" type="int"/> </bean> </beans>
使用方式
@RequestMapping(value = "/xxxx/getdata",method = RequestMethod.POST) @ResponseBody public void GetData(@ModelAttribute("form") User1 form){ // if(form.getUsername()!=null&&form.getPassword()!=null){ Jedis jedis=jedisPool.getResource(); String username=form.getUsername(); String password=form.getPassword(); jedis.set("username",username); jedis.set("password",password); // } return; }