ssm+reids+maven连接池的简单应用

1.首先开启reids服务


2.设置密码

3.导入架包(这里我用的是maven)

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

4.配置spring与redis整合(spring-jedis.xml)

<?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: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/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 引入jedis的properties配置文件 -->
    <!--如果你有多个数据源需要通过<context:property-placeholder管理,且不愿意放在一个配置文件里,那么一定要加上ignore-unresolvable=“true"-->
    <!--  <context:property-placeholder location="classpath:redis.properties"/>-->
    <!--Jedis连接池的相关配置-->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!--新版是maxTotal,旧版是maxActive-->
        <property name="maxTotal" value="200" />
        <property name="maxIdle" value="50" />
       <property name="maxWaitMillis" value="20000" />
        <property name="testOnBorrow" value="true"/>
        <property name="testOnReturn" value="true"/>
    </bean>

    <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
        <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
        <constructor-arg name="host" value="127.0.0.1" />
        <constructor-arg name="port" value="6379" type="int" />
        <constructor-arg name="timeout" value="30000" type="int" />
        <constructor-arg name="password" value="123456789" /><!--密码-->
        <constructor-arg name="database" value="0" type="int" />
    </bean>
</beans>

(注意:我在web.xml会进行扫描,如:(详细配置请参照我的ssm整合博文))

<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:spring/spring-*.xml
		</param-value>
	</context-param>

5.写一个控制器类测试

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Controller
@RequestMapping("/Jedis")
public class JedisController {
	
	    @Autowired
	    private JedisPool jedisPool;//注入JedisPool

	    @RequestMapping(value = "/set",method = RequestMethod.GET)
	    @ResponseBody
	    public String set(){
	        //获取ShardedJedis对象
	        Jedis jedis = jedisPool.getResource();
	        //存入键值对
	        jedis.set("zzf","hello jedis ok");
	        //回收ShardedJedis实例
	        jedis.close();

	        return "set";
	    }

	    @RequestMapping(value = "/get",method = RequestMethod.GET)
	    @ResponseBody
	    public String get(){
	        Jedis jedis = jedisPool.getResource();
	        //根据键值获得数据
	        String result = jedis.get("zzf");
	        jedis.close();

	        return result;
	    }

}

6.运行项目


OK,可以看到reids与ssm整合成功了。




猜你喜欢

转载自blog.csdn.net/qq_37598011/article/details/80572699
今日推荐