Redis使用入门

本文详细介绍如何在Spring框架中集成Redis数据库,包括下载安装、配置连接池、定义序列化方式及使用SessionCallback进行数据操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

现在GitHub上下载:https://github.com/MicrosoftArchive/redis/releases/tag/win-3.0.504

下载完后解压:

在这个目录下新建立一个startup.cmd文件,写入:

redis-server redis.windows.conf

 双击这个文件,再打开redis-cli.exe,这样就可以连接到他的服务器了。

 

在spring中使用redis,需要jedis.jar,spring-data-redis.jar。

配置applicationContext.xml

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<!--最大空闲数 -->
		<property name="maxIdle" value="50" />
		<!--最大连接数 -->
		<property name="maxTotal" value="100" />
		<!--最大等待时间 -->
		<property name="maxWaitMillis" value="20000" />
	</bean>

	<bean id="connectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="localhost" />
		<property name="port" value="6379" />
		<!--<property name="password" value="paasword"/> -->
		<property name="poolConfig" ref="poolConfig" />
	</bean>

	<bean id="jdkSerializationRedisSerializer"
		class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
		
	<bean id="stringRedisSerializer"
		class="org.springframework.data.redis.serializer.StringRedisSerializer" />
		
	<bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="keySerializer" ref="stringRedisSerializer" />
		<property name="valueSerializer" ref="jdkSerializationRedisSerializer" />
	</bean>
</beans>

 



import java.io.Serializable;

public class Role implements Serializable {
	
	private static final long serialVersionUID = 6977402643848374753L;

	private long id;
	private String roleName;
	private String note;

	public long getId() {
		return id;
	}

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

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

}

必须要序列化!

使用:

package com.ssm.chapter17.main;

import Pojo.Role;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;


import javax.swing.*;

public class Chapter17Main {
    public static void main(String args[])
    {
       ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
        Role role = new Role();
        role.setId(1);
        role.setRoleName("role_name_1");
        role.setNote("role_note_1");
        SessionCallback callback=new SessionCallback<Role>() {
            @Override
            public  Role execute(RedisOperations ops) throws DataAccessException {
                ops.boundValueOps("role_1").set(role);
                return (Role)ops.boundValueOps("role_1").get();
            }
        };
        Role saveRole = (Role) redisTemplate.execute(callback);
        System.out.println(saveRole.getId());

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值