lettuce应用
lettuce
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-pool2</artifactId> </dependency>
配置
spring.redis.host=192.168.99.100 spring.redis.port=6379 spring.redis.database=0 spring.redis.lettuce.pool.maxActive=5 spring.redis.lettuce.pool.maxIdle=5
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);
数据操作接口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); }
实体类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; }
配置类
package com.example.demo.configure; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration @EnableCaching public class RedisConfig extends CachingConfigurerSupport { @Bean public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory connectionFactory) { // 配置redisTemplate RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer());//key序列化 redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());//value序列化 redisTemplate.afterPropertiesSet(); return redisTemplate; } }
控制器
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.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @Slf4j @RestController public class DemoController { @Autowired UsersRepository usersRepository; @Autowired private RedisTemplate redisTemplate; @RequestMapping("/redis") public String redis() { usersRepository.findAll().forEach(c -> { redisTemplate.opsForHash().put("users", c.getName(), Integer.toString(c.getAge())); }); String age = String.valueOf(redisTemplate.opsForHash().get("users", "Fiona")); log.info("Fiona - {}", age); return "success"; } }