ValueOperations valueOperations = redisTemplate.opsForValue();
BoundValueOperations<String, User> boundValueOps = redisTemplate.boundValueOps("key");
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.3.4.RELEASE</version> </dependency> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.4.2</version> </dependency>
<!--JedisPoolConfig 连接池参数配置--> <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"> <!--最大空闲实例数--> <property name="maxIdle" value="300" /> <!--最大活跃实例数--> <property name="maxTotal" value="600" /> <!--创建实例时最长等待时间--> <property name="maxWaitMillis" value="1000" /> <!--创建实例时是否验证--> <property name="testOnBorrow" value="true" /> </bean> <!--JedisConnectionFactory 跟配置数据库连接池类似,需要配置JedisConnectionFactory来通过服务器或者连接池的方式获取redis服务器的连接--> <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="hostName" value="127.0.0.1"/> <property name="port" value="6379"/> <property name="usePool" value="true"/> <property name="poolConfig" ref="poolConfig"/> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/> <bean id="valueSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/> <!-- redis模板配置 spring-data-redis提供了一个基础的泛型RedisTemplate封装了基础的crud操作--> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="connectionFactory"/> <property name="defaultSerializer" ref="stringRedisSerializer"/> <property name="keySerializer" ref="stringRedisSerializer"/> <property name="valueSerializer" ref="valueSerializer"/> </bean>
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/** 用户ID */
private Long id;
/** 用户名 */
private String name;
/** 用户年龄 */
private Integer age;
}
@Controller
public class UserController extends BaseController {
@Autowired
private IUserService userService;
@Autowired
private RedisTemplate<String,User> redisTemplate;
@ResponseBody
@RequestMapping("/redis")
public Object redis() {
User u1=new User();
u1.setId(1L);
u1.setName("wang");
u1.setAge(22);
redisTemplate.opsForValue().set("user:wang",u1);
User u2=redisTemplate.opsForValue().get("user:wang");
return u2;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有