ssm中加入redis,spring -redis整合,简单,认识redis

1导入spring和redis的jar包,当然推荐用maven。开启服务

2.application.xml中配置

定义线程池

<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="50"></property>
    <property name="maxTotal" value="100"></property>
    <property name="maxWaitMillis" value="20000"></property>
</bean>

定义连接

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

定义序列化

<bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
<bean id= "stringRedisSerializer" class= "org.springframework.data.redis.serializer.StringRedisSerializer" />

定义RedisTemplate

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

整体如下

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
	
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
	<property name="maxIdle" value="50"></property>
	<property name="maxTotal" value="100"></property>
	<property name="maxWaitMillis" value="20000"></property>
</bean>

<bean id="connectionFactory"
	class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
	<property name="hostName" value="localhost"></property>
	<property name="port" value="6379"></property>
	<property name="poolConfig" ref="poolConfig"></property>
</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>
<property name="keySerializer" ref="stringRedisSerializer"></property>
<property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
</bean>

</beans>

 定义对象

package test429Redis;

import java.io.Serializable;

/**
 * @Description
 * @Author 
 * @Date 2018年8月9日 
 */

public class Role implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private long id;
	private String name;
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
	
	
}

 main函数实现

package test429Redis;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * @Description
 * @Author
 * @Date 2018年8月9日
 */

public class RedisTemplateTest {
    
    public static void main(String[] args) {
        //ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        ApplicationContext applicationContext=new  FileSystemXmlApplicationContext("WebContent/WEB-INF/applicationContext.xml");
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);
        Role role = new Role();
        role.setId(4684666);
        role.setName("zzq");
        redisTemplate.opsForValue().set("role1", role);
        Role role1 = (Role) redisTemplate.opsForValue().get("role1");
        
        System.out.println(role1.getName());
    }


}


注意:报错一般就是jar包没有,百度一下就可以解决

坐井观天,井底之蛙,学无止境 记录

猜你喜欢

转载自blog.csdn.net/zzqtty/article/details/81562424