Spring 整合 Redis (零配置) 的简单使用

这只是一个简单的例子。。

pom.xml

        <!--jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!-- srping data -redis -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

SpringConfig类

package com.oukele.redis2.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
@ComponentScan("com.oukele.redis2")
public class SpringConfig {

    @Bean
    RedisConnectionFactory redisFactory(){
        return new JedisConnectionFactory();
    }

    @Bean
    StringRedisTemplate redisTemplate(){
        return new StringRedisTemplate(redisFactory());
    }


}

Car类

package com.oukele.redis2.entity;

public class Car {

    /*
     * 编号
     */
    private int id;
    /*
     * 车名
     */
    private String name;
    /*
     * 车速
     */
    private double speed;

    public Car(int id, String name, double speed) {
        this.id = id;
        this.name = name;
        this.speed = speed;
    }

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public double getSpeed() {
        return speed;
    }

    public void setSpeed(double speed) {
        this.speed = speed;
    }

    @Override
    public String toString() {
        return "Car{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", speed=" + speed +
                '}';
    }
}

 CarDao类

package com.oukele.redis2.dao;

import com.oukele.redis2.entity.Car;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.stereotype.Repository;
import redis.clients.jedis.Jedis;

import java.util.List;

/*
* 数据访问类
* */
@Repository
public class CarDao {

    @Autowired
    private RedisOperations redisOperations;

    @Autowired
    private RedisConnectionFactory redisConnectionFactory;//也可以使用接口

    /*
    * 添加
    * */
    public void add( Car car ){
        redisOperations.opsForValue().set("cars",car.toString());
    }

    /*
    * 获取
    * */
    public void get(){
        System.out.println(redisOperations.opsForValue().get("cars"));
    }

    // ..... 其他操作

}

Main类(测试类)

package com.oukele.redis2;

import com.oukele.redis2.config.SpringConfig;
import com.oukele.redis2.dao.CarDao;
import com.oukele.redis2.entity.Car;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        CarDao carDao = context.getBean(CarDao.class);
        carDao.add(new Car(1,"宝马",34.3));

        carDao.get();
    }
}

测试结果:

 1 Car{id=1, name='宝马', speed=34.3} 

示例源码下载:https://github.com/oukele/Spring-Jedis

posted @ 2018-12-25 19:49  追梦滴小蜗牛  阅读(482)  评论(0编辑  收藏  举报