spring整合redis詳解

1、需要的jar包:

spring-data-redis-X.X.X.jar
jedis-X.X.X.jar
spring所需jar包

2、applicationContext.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:jee="http://www.springframework.org/schema/jee" 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.xsd  
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">  

    <context:property-placeholder location="classpath:redis.properties" />  
    <context:component-scan base-package="com.x.redis.dao">
    </context:component-scan>
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxIdle" value="${redis.maxIdle}" />  
        <property name="maxActive" value="${redis.maxActive}" />  
        <property name="maxWait" value="${redis.maxWait}" />  
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
    </bean> <!-- redis的一些其他的配置 --> 

    <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:pool-config-ref="poolConfig"/>  <!--連接redis的配置 -->

    <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> 
        <property name="connectionFactory"   ref="connectionFactory" />  
    </bean><!-- redis模板,對redis操作的封裝,類似hibernate的hibernateTemplate -->          

    <bean id="userDAO" class="com.x.redis.dao.impl.UserDAOImpl" />   
</beans> 

3、redis.properties中的配置

# Redis settings
#redis.host=127.0.0.1
#redis.port=6380
redis.host=127.0.0.1
redis.port=6379
redis.pass=

redis.maxIdle=300
redis.maxActive=500
redis.maxWait=1500
redis.testOnBorrow=true

4、User.java

public class User {

    private String id;
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

5、UserDao.java

public class UserDao {

    @Autowired
    protected RedisTemplate<Serializable, Serializable> redisTemplate;
    //保存
    public void saveUser(final User user) {
        redisTemplate.execute(new RedisCallback<Object>() {

            @Override
            public Object doInRedis(RedisConnection connection) throws DataAccessException {
                connection.set(redisTemplate.getStringSerializer().serialize(user.getId()),
                               redisTemplate.getStringSerializer().serialize(user.getName()));//獲取User中的數據把其存入redis中
                return null;
            }
        });
    }

    //獲取
    public User getUser(final String id) {
        return redisTemplate.execute(new RedisCallback<User>() {
            @Override
            public User doInRedis(RedisConnection connection) throws DataAccessException {
                byte[] key = redisTemplate.getStringSerializer().serialize(id);//將id序列化爲redis中key,
                if (connection.exists(key)) {
                    byte[] value = connection.get(key);//通過key獲取value
                    String name = redisTemplate.getStringSerializer().deserialize(value);
                    User user = new User();
                    user.setName(name);
                    user.setId(id);
                    return user;
                }
                return null;
            }
        });
    }
   還有一些其他的操作如刪除,批量添加,修改等有時間了再......
}
發佈了31 篇原創文章 · 獲贊 11 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章