redis笔记 二

1.redis一个实例中的数据库

1.1.数据库的数量

redis下,数据库是由一个整数索引标识,而不是由一个数据库名称。默认情况下,一个客户端连接到数据库0。

redis配置文件中下面的参数来控制数据库总数(默认情况下):

database 16

这说明此实例下有16个db,db0~db15.

1.2.可以通过下面的命令来切换到不同的数据库下:

select 2

每个数据库都有属于自己的空间,不必担心之间的key冲突。

不同的数据库下,相同的key取到各自的值。

1.3.

flushdb命令清除数据,只会清除当前的数据库下的数据,不会影响到其他数据库。

flushall命令会清除这个实例的数据。在执行这个命令前要格外小心。

2.redis与spring整合

pom.xml

<dependencies>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.3.7.RELEASE</version>
        </dependency>
        
        <!-- datasource -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <!-- 这个组下还有其他几种数据源 -->
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.7.3</version>
        </dependency> </dependencies>

 

 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" > -->

<web-app>
	<display-name>Ar chetype Created Web Application</display-name>
	<description></description>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:redisAPConf/spring-context.xml
		</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
</web-app>

spring-context.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-4.2.xsd  
		http://www.springframework.org/schema/context 
		http://www.springframework.org/schema/context/spring-context-4.2.xsd">

	<!-- 切面注入 -->
	<!-- <aop:aspectj-autoproxy proxy-target-class="true" /> -->
	<!-- ①:对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
	<context:component-scan base-package="com.test.redis.**" />

	<!-- spring的默认实现,不配置也一样 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />

	<!-- ②:启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
	<bean
		class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>

 spring-dataBase.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:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">

	<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="10" />
		<property name="minIdle" value="3" />
		<property name="maxTotal" value="100" />
		<property name="testOnBorrow" value="true" />
	</bean>

	<bean id="connectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
		p:host-name="192.168.118.6" p:port="6379" p:password="123456"
		p:pool-config-ref="poolConfig" />

	<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
		<property name="connectionFactory" ref="connectionFactory" />
	</bean>

</beans>

主方法

public static void main(String[] args) {
		// TODO Auto-generated method stub
		String configs[] = new String[] { "classpath:redisAPConf/spring-context.xml",
				"classpath:redisAPConf/spring-dataBase.xml" };
		ApplicationContext context = new ClassPathXmlApplicationContext(configs);
		StringRedisTemplate srt = (StringRedisTemplate) context.getBean("redisTemplate");
BoundHashOperations hashOperations =srt.boundHashOps("login");
         hashOperations.put("dale", "123456546"); //这部分没写完,【StringRedisTemplate】对象对RedisConnection进行了封装,它提供了连接管理,序列化等功能,它对Redis的交互进行了更高层次的抽象。另外还提供了Redis操作命令的操作视图,这极大的方便和简化了Redis的操作。 }

 

posted @ 2017-03-22 17:27  Dale_na  阅读(130)  评论(0编辑  收藏  举报