SpringMvc整合redis

1、在pom.xml中增加以下节点

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.7.4.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>

2、配置redis相关参数

    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> 
        <!-- 最大空闲连接数 -->
        <property name="maxIdle" value="100" /> 
        <!-- 最大连接数 -->
        <property name="maxTotal" value="500" /> 
        <!-- 连接耗尽时是否阻塞, false报异常,ture阻塞直到超时, 默认true -->
        <property name="blockWhenExhausted" value="true" />
        <!-- 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间,  默认-1 --> 
        <property name="maxWaitMillis" value="300" /> 
        <!-- 在获取连接的时候检查有效性, 默认false -->
        <property name="testOnBorrow" value="true" />  
    </bean> 

    <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> 
	<!-- 主机 -->
        <property name="hostName" value="192.168.93.128" /> 
        <!-- 端口,默认端口6379,配置文件中修改 -->
        <property name="port" value="6379"/> 
        <!-- 密码,默认无密码,在redis配置中增加 -->
        <property name="password" value="admin" /> 
        <!-- 引用jedisPoolConfig配置 -->
        <property name="poolConfig" ref="jedisPoolConfig" /> 
      	<!-- 使用连接池 -->
        <property name="usePool" value="true"/> 
    </bean> 
	
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">   
        <property name="connectionFactory"   ref="jedisConnectionFactory" />   
       	<!-- 字符串序列化 -->
        <property name="keySerializer">   
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />   
        </property>   
        <!-- 对象序列化 -->   
        <property name="valueSerializer">   
            <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />   
        </property>   
        <!-- 字符串序列化 -->
        <property name="hashKeySerializer">     
           <bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/>     
        </property>  
         <!-- 对象序列化 -->    
        <property name="hashValueSerializer">   
           <bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>     
        </property> 
     </bean> 

3、在spring配置文件中导入redis配置文件

<import resource="./spring-redis.xml"/> 

4、在web.xml文件加载spring配置文件

        <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
		</param-value>
	</context-param>
	<servlet>
		<servlet-name>dispatcherServlet</servlet-name>
		<servlet-class>com.ctjy.wxmis.http.servlet.CustomerDispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>
				classpath:mvc.xml,classpath:dubbo.xml,classpath:mq.xml
			</param-value>
		</init-param>
		<init-param>
			<param-name>forward</param-name>
			<param-value>true</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>


5、java代码中调用redis模版(下面代码只演示key-value类型,根据需要调整)

@Controller
@RequestMapping("/redis")
public class RedisController {
	@Autowired
	private  RedisTemplate redisTemplate;
	
	@ResponseBody
	@RequestMapping("/redis")
	public String redis() {
		ValueOperations<String, Object> value = redisTemplate.opsForValue();
		try {
			value.set("myredis", "myredis");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		System.out.println(value.get("myredis"));
		return "test redis";
	}

}


  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值