zoukankan      html  css  js  c++  java
  • Maven中Spring-Data-Redis存储对象(redisTemplate) (转)

    Redis是一种nosql数据库,在开发中常用做缓存。Jedis是Redis在java中的redis- client.在此之前,希望已经了解redis的基本使用和Maven的使用。建立Maven Project之后,在POM.xml中添加jedis和spring-data-redis的依赖如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.0.0</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    <!-- spring-redis -->
    <dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>1.0.0.RELEASE</version>
    </dependency>

      Redis连接数据库参数如下:applicationContext-redis.properties

    1
    2
    3
    4
    5
    6
    7
    8
    #redis config
    redis.pool.maxActive=100
    redis.pool.maxIdle=20
    redis.pool.maxWait=1000
    redis.pool.testOnBorrow=true
    redis.hostname=localhost
    redis.port=6379
    redis.password=

      在上下文配置中使用key-value读取方式读取properties中的值:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <!-- Jedis 连接池配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxActive" value="${redis.pool.maxActive}" />
    <property name="maxIdle" value="${redis.pool.maxIdle}" />
    <property name="maxWait" value="${redis.pool.maxWait}" />
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
    </bean>
    <!-- Jedis ConnectionFactory 数据库连接配置-->
    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
    <property name="hostName" value="${redis.hostname}" />
    <property name="port" value="${redis.port}" />
    <property name="password" value="${redis.password}" />
    <property name="poolConfig" ref="jedisPoolConfig" />
    </bean>
    <!—- redisTemplate配置,redisTemplate是对Jedis的对redis操作的扩展,有更多的操作,封装使操作更便捷 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="jedisConnectionFactory" />

      

    上面redisTemplate已经基本配置完成。

    接下来创建User类,必须实现或者间接实现Serializable接口:

    Redis存储对象是使用序列化,spring-data-redis已经将序列化的功能内置,不需要我们去管,我们只需要调用api就可以使用。SerialVersionUID字段对序列化扩展有用,为了以后扩展或者缩减字段时不会造成反序列化出错。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    public class User implements Serializable {
     
    private static final long serialVersionUID = -7898194272883238670L;
     
    public static final String OBJECT_KEY = "USER";
     
    public User() {
    }
     
    public User(String id) {
    }
     
    public User(String id, String name) {
    this.id = id;
    this.name = name;
    }
     
    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;
    }
     
    public String toString() {
    return "User [id=" + id + ", name=" + name + "]";
    }
     
    public String getKey() {
    return getId();
    }
     
    public String getObjectKey() {
    return OBJECT_KEY;
    }
    }

      创建userService类来操作redis增删查改缓存对象。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    public class UserService {
     
    RedisTemplate<String, User> redisTemplate;
     
    public RedisTemplate<String, User> getRedisTemplate() {
    return redisTemplate;
    }
     
    public void setRedisTemplate(RedisTemplate<String, User> redisTemplate) {
    this.redisTemplate = redisTemplate;
    }
     
    public void put(User user) {
    redisTemplate.opsForHash().put(user.getObjectKey(), user.getKey(), user);
    }
     
    public void delete(User key) {
    redisTemplate.opsForHash().delete(key.getObjectKey(), key.getKey());
    }
     
    public User get(User key) {
    return (User) redisTemplate.opsForHash().get(key.getObjectKey(), key.getKey());
    }
    }

      在上下文中配置redisTemplate注入,在使用bean方式来注入时,redisTemplate必须有setter/getter方法:

    1
    2
    3
    4
    5
    <bean id="userService" class="Service.UserService">
    <property name="redisTemplate">
    <ref bean="redisTemplate" />
    </property>
    </bean>

      ======如果使用注解方式自动注入,则可以注释掉上面的bean配置方式======

    在UserService注解@Service(“userService”),也可以在Service里写名字,默认是第一字母小写。

    1
    2
    3
    4
    5
    6
    7
    8
    @Service("userService")
    public class UserService {
     
    @Autowired
    RedisTemplate<String, User> redisTemplate;
    ……
    ……
    }

      在上下文配置文件中,添加自动扫描包的context节点,Base-package的路径要覆盖包含注解的类文件:

    1
    <context:component-scan base-package="*" />

      在main中来简单操作一下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    public class Main {
    public static void main( String[] args )
    {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath*:/conf/applicationContext.xml" );
    UserService userService = (UserService) applicationContext.getBean("userService");
     
    User user1 = new User("user1ID", "User 1");
    User user2 = new User("user2ID", "User 2");
     
    System.out.println("==== getting objects from redis ====");
    System.out.println("User is not in redis yet: " + userService.get(user1));
    System.out.println("User is not in redis yet: " + userService.get(user2));
     
    System.out.println("==== putting objects into redis ====");
    userService.put(user1);
    userService.put(user2);
     
    System.out.println("==== getting objects from redis ====");
    System.out.println("User should be in redis yet: " + userService.get(user1));
    System.out.println("User should be in redis yet: " + userService.get(user2));
     
    System.out.println("==== deleting objects from redis ====");
    userService.delete(user1);
    userService.delete(user2);
     
    System.out.println("==== getting objects from redis ====");
    System.out.println("User is not in redis yet: " + userService.get(user1));
    System.out.println("User is not in redis yet: " + userService.get(user2));
     
    }
    }

      

    http://www.cnblogs.com/jifeng/p/4422435.html

  • 相关阅读:
    汇编14:端口
    汇编13:int指令
    整个软件开发的流程
    css grid 布局
    socket粘包处理
    如何做好需求变更管理?——需求变更流程规范
    软件工程需求分析文档模板
    使用layui的form.on绑定select选中事件, form.on()不执行的原因分析
    ID4收藏
    ASP.NET MVC5中View-Controller间数据的传递
  • 原文地址:https://www.cnblogs.com/softidea/p/5361913.html
Copyright © 2011-2022 走看看