Jedis应用

Jedis

配置类

package com.example.demo.configure;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@Slf4j
public class JedisConfig extends CachingConfigurerSupport {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Bean
    public JedisPool redisPoolFactory(){
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        JedisPool jedisPool = new JedisPool(jedisPoolConfig, host,port);
        log.info("JedisPool注入成功!");
        log.info("redis地址:" + host + ":" + port);
        return  jedisPool;
    }
}

 schema.sql

drop table users if exists;
create table users (
    id bigint auto_increment,
    name varchar(255),
    age int,
    create_time timestamp,
    primary key (id)
);
insert into users (name, create_time,age) values ('Lili', now(),29);
insert into users (name, create_time,age) values ('Fiona', now(),30);
insert into users (name, create_time,age) values ('xyz', now(),30);
insert into users (name, create_time,age) values ('zbc', now(),16);
insert into users (name, create_time,age) values ('Nana', now(),18);
View Code

实体类Users.java

package com.example.demo.model;

import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "users")
@Data
@Builder
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private int age;
    @Column(updatable = false)
    @CreationTimestamp
    private Date createTime;
}
View Code

数据操作接口UsersRepository.java

package com.example.demo.repository;

import com.example.demo.model.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

import java.util.List;

@RepositoryRestResource(collectionResourceRel = "user", path = "user")
public interface UsersRepository extends JpaRepository<Users,Integer> {
    List<Users> findByName(@Param("name") String name);
    List<Users> findByNameContaining(@Param("name") String name);
}
View Code

控制器

package com.example.demo.controller;

import com.example.demo.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@Slf4j
@RestController
public class DemoController {

    @Autowired
    UsersRepository usersRepository;

    @Autowired
    private JedisPool jedisPool;


    @RequestMapping("/redis")
    public String redis() {
        try (Jedis jedis = jedisPool.getResource()) {
            usersRepository.findAll().forEach(c -> {
                jedis.hset("users",
                        c.getName(),
                        Integer.toString(c.getAge()));
            });
            Map<String, String> users = jedis.hgetAll("users");
            log.info("Users: {}", users);
            String name = jedis.hget("users", "Fiona");
            log.info("Fiona - {}", name);
        }
        return "success";
    }

}

启动项目

GET http://127.0.0.1:8080/redis

查看redis里保存的值

>redis-cli -h 192.168.99.100 -p 6379
192.168.99.100:6379> keys *
1) "users"
2) "key1"
192.168.99.100:6379> type users
hash
192.168.99.100:6379> hget users Fiona
"30"
posted @ 2020-04-08 18:18  慕尘  阅读(323)  评论(0编辑  收藏  举报