在SpringMVC项目基础上整合redis

SpingMVC与redis整合汇总

最近公司要在现有项目基础上整合redis,以用来对客户数据进行高效处理。
话不多少,直接上代码说明:

  • 1、配置文件
    (1)pom.xml
        <dependency>
            <groupId>org.springframework.datagroupId>
            <artifactId>spring-data-redisartifactId>
            <version>1.4.2.RELEASEversion>
        dependency>
        <dependency>
            <groupId>redis.clientsgroupId>
            <artifactId>jedisartifactId>
            <version>2.5.2version>
        dependency>
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-pool2artifactId>
            <version>2.4.2version>
        dependency>
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.3.2version>
        dependency>

(2)redis.properties

#redis
redis.host=127.0.0.1
redis.port=6379
redis.pass=123456
redis.timeout=-1

redis.maxIdle=20
redis.maxTotal=500
redis.minIdle=8
redis.maxWaitMillis=-1
redis.testOnBorrow=true
redis.testOnReturn=true
redis.testWhileIdle=true

redis.timeBetweenEvictionRunsMillis=30000
redis.numTestsPerEvictionRun=10
redis.minEvictableIdleTimeMillis=60000

(3)redis-config.xml


<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="minIdle" value="${redis.minIdle}" />
        <property name="maxTotal" value="${redis.maxTotal}" />

        <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
        <property name="testOnReturn" value="${redis.testOnReturn}" />

        <property name="testWhileIdle" value="${redis.testWhileIdle}" />
        <property name="timeBetweenEvictionRunsMillis" value="${redis.timeBetweenEvictionRunsMillis}" />
        <property name="numTestsPerEvictionRun" value="${redis.numTestsPerEvictionRun}" />
        <property name="minEvictableIdleTimeMillis" value="${redis.minEvictableIdleTimeMillis}" />
    bean>
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
         p:host-name="${redis.host}" 
          p:port="${redis.port}" 
          p:password="${redis.pass}" 
          p:timeout="${redis.timeout}" 
          p:pool-config-ref="poolConfig"/>

    
    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
         <property name="connectionFactory" ref="connectionFactory" />
         
         <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> 

在applicationContext.xml中引入

<import resource="redis-config.xml"/>
  • 2、java代码
    (1)RedisController.java
@Controller
@RequestMapping("/testData/")
public class RedisController {
     
    @Resource
    private RedisCacheUtil redisCache;
    //设置redis
    @RequestMapping("setRedis")
    public String setRedis(HttpServletResponse response, HttpServletRequest request) {
        redisCache.hset("student","age", "88");
        try {
            this.write(response, "保存成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void write(HttpServletResponse response, Object o) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println(o.toString());
        out.flush();
        out.close();
    }
}

(2)工具类
RedisCacheUtil.java

@Component("redisCache")
public class RedisCacheUtil {
     
    @Resource
    private StringRedisTemplate redisTemplate;

    /**
     * 方法说明:向hash中添加值 . 
*/
public void hset(String key,String field,String value){ if(Public.isEmpty(key)){ return; } redisTemplate.opsForHash().put(key, field, value); } /** * 方法说明:从redis 中取值.
*/
public String hget(String key,String field){ if(Public.isEmpty(key)){ return null; } return (String) redisTemplate.opsForHash().get(key, field); } /** * 方法说明:检验是否存在key .
*/
public boolean hexists(String key,String field){ if(Public.isEmpty(key)){ return false; } return redisTemplate.opsForHash().hasKey(key, field); } /** * 方法说明: 查询key对应多少条数据.
*/
public long hsize(String key){ if(Public.isEmpty(key)){ return 0L; } return redisTemplate.opsForHash().size(key); } /** * 方法说明:删除操作 .
*/
public void hdel(String key,String field){ if(Public.isEmpty(key)){ return; } redisTemplate.opsForHash().delete(key, field); } }
  • 3、jsp代码
<body>
     Hello World!
    <a href="${pageContext.request.contextPath}/testData/list.do">保存数据a>
  body>

说明:此代码放到当前系统能够正常访问的页面上。此时可以正常保存数据。

你可能感兴趣的:(redis,Redis)