1. 程式人生 > >ssm中加入redis,spring -redis整合,簡單,認識redis

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包沒有,百度一下就可以解決

坐井觀天,井底之蛙,學無止境 記錄