SSM框架SpringMVC+ Spring + MyBatis(集成redis缓存)

1.pom.xml 

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

		<!-- config redis data and client jar-->
		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-redis</artifactId>
			<version>1.6.2.RELEASE</version>
		</dependency>

引入redis的依赖,这里遇到的问题Could not initialize class org.springframework.data.redis.connection.jedis.JedisConnectionFactory

经查阅是redis的版本问题,用上面的版本即可解决该问题。

2.redis.properties

redis.host=127.0.0.1
redis.port=6379
redis.maxIdle=300
redis.maxWaitMillis=1000
redis.maxTotal=600
redis.testOnBorrow=true
redis.testOnReturn=true

redis的配置,启动本地的redis的服务

3.spring-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xmlns="http://www.springframework.org/schema/beans"
	   xmlns:context="http://www.springframework.org/schema/context"
	   xmlns:aop="http://www.springframework.org/schema/aop"
	   xmlns:tx="http://www.springframework.org/schema/tx"
	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">

	<!-- 连接池基本参数配置,类似数据库连接池 -->
	<!--<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>-->


    <!--引入Redis配置文件-->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:redis.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>

	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis.maxTotal}"/>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"/>
        <property name="testOnReturn" value="${redis.testOnReturn}"/>
		<property name="maxIdle" value="${redis.maxIdle}" />
		<property name="testOnBorrow" value="${redis.testOnBorrow}"/>
	</bean>

	<!-- 连接池配置,类似数据库连接池 -->
	<bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" >
		<property name="hostName" value="${redis.host}"></property>
		<property name="port" value="${redis.port}"></property>
		<!-- <property name="password" value="${redis.pass}"></property> -->
		<property name="poolConfig"  ref="poolConfig"></property>
	</bean>

    <!-- 配置 key 和 value 的序列化器 -->
    <bean id="jdkSerializationRedisSerializer" class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/>
    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    <!-- 配置Redis模板对象 -->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="jedisConnectionFactory"></property>
        <property name="keySerializer" ref="stringRedisSerializer"></property>
        <property name="valueSerializer" ref="jdkSerializationRedisSerializer"></property>
    </bean>

</beans>

配置redis的连接信息。

这里遇到的问题是,项目中有两个properties配置文件,一个数据库的,一个redis的,所以需要采用以下方式配置

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath*:redis.properties</value>
            </list>
        </property>
        <property name="ignoreUnresolvablePlaceholders" value="true" />
    </bean>
<!--数据库配置 -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:jdbc.properties</value>
			</list>
		</property>
		<property name="ignoreUnresolvablePlaceholders" value="true" />
	</bean>

4.BookController中实现redis

@Autowired
	private RedisTemplate redisTemplate;
	/**
	 * 这里就是测试redis
	 */
	@RequestMapping(value = "/redis", method = RequestMethod.GET)
	private String redis(Model model) {
		Book book = (Book) redisTemplate.opsForValue().get("book");
		if (book == null){
			book = bookService.getById(1001);
			logger.info("从数据库获取id为1001的图书{};", book);
			// 将数据设置到缓存中 30秒的有效期
			redisTemplate.opsForValue().set("book",book,30, TimeUnit.SECONDS);
		}else {
			logger.info("从redis缓存中获取id为1001的图书{};", book);
		}
		model.addAttribute("book", book);
		return "detail";
	}

测试结果:

第一次访问 http://localhost:8082/ssm_war/book/redis

从数据库获取id为1001的图书Book [bookId=1001, name=高并发处理, number=10];

第二次访问:从redis缓存中获取id为1001的图书Book [bookId=1001, name=高并发处理, number=10];

自此完成了简单的redis的缓存,注意里面的实体类需要序列化。

使用redis的客户端也可以看到缓存的book的信息。

下载代码 https://github.com/ChenXbFrank/ssm/tree/devlop/  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值