Spring集成Redis

1.spring集成redis

引入spring-data-redis.jar包:

	<!-- spring-redis实现 -->
    	<dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.3.4.RELEASE</version>
        </dependency>     
redis.properties 配置文件:
​
#访问地址
host=127.0.0.1
#访问端口
port=6379
#注意,如果没有password,此处不设置值,但这一项要保留
password=
 #数据库下标
 dbIndex=0
#最大空闲数,数据库连接的最大空闲时间。超过空闲时间,数据库连接将被标记为不可用,然后被释放。设为0表示无限制。
maxIdle=300
#连接池的最大数据库连接数。设为0表示无限制
maxActive=600
#最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
maxWait=1000
#在borrow一个jedis实例时,是否提前进行alidate操作;如果为true,则得到的jedis实例均是可用的;
testOnBorrow=true

​

spring-redis配置文件(此处包含了数据库和mybatis的配置):

<?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" 
	xmlns:jdbc="http://www.springframework.org/schema/jdbc"  
	xmlns:jee="http://www.springframework.org/schema/jee" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.1.xsd">
	
	<!-- 配置 spring-mybatis.xml -->
	<!-- 读取配置文件 -->
 	<util:properties id="redis"
		location="classpath:conf/redis.properties"/> 	 
	<util:properties id="jdbc"
		location="classpath:conf/jdbc.properties"/> 

	<!-- 配置数据库连接池 -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close"> 
		<property name="driverClassName"
			value="#{jdbc.driver}"/>
		<property name="url"
			value="#{jdbc.url}"/>
		<property name="username"
			value="#{jdbc.user}"/>
		<property name="password"
			value="#{jdbc.password}"/>
		<property name="maxIdle"
			value="#{jdbc.maxIdle}"/>
		<property name="maxWait"
			value="#{jdbc.maxWait}"/>						
		<property name="maxActive"
			value="#{jdbc.maxActive}"/>
		<property name="defaultAutoCommit"
			value="#{jdbc.defaultAutoCommit}"/>
		<property name="defaultReadOnly"
			value="#{jdbc.defaultReadOnly}"/>
		<property name="testOnBorrow"
			value="#{jdbc.testOnBorrow}"/>			
		<property name="validationQuery"
			value="#{jdbc.validationQuery}"/>	
	</bean>
	
	<!-- 配置MyBatis的 SessionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource"
			 ref="dataSource"/>
		
		<property name="mapperLocations"
			value="classpath:mapper/*.xml&#
  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值