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


    
            org.springframework
            spring-context
            4.3.7.RELEASE
        

        
            org.springframework
            spring-jdbc
            4.3.7.RELEASE
        

        
            org.springframework
            spring-orm
            4.3.7.RELEASE
        

        
            org.springframework
            spring-context-support
            4.3.7.RELEASE
        

        
            org.springframework
            spring-webmvc
            4.3.7.RELEASE
        

        
        
        
            org.springframework.data
            
            spring-data-redis
            1.6.0.RELEASE
        

        
            redis.clients
            jedis
            2.7.3
        

 web.xml

<?xml version="1.0" encoding="UTF-8"?>



	Ar chetype Created Web Application
	
	
		contextConfigLocation
		
			classpath:redisAPConf/spring-context.xml
		
	

	
		org.springframework.web.context.ContextLoaderListener
	

spring-context.xml 

<?xml version="1.0" encoding="UTF-8"?>


	
	
	
	

	
	

	
	


 spring-dataBase.xml

<?xml version="1.0" encoding="UTF-8"?>


	
		
		
		
		
	

	

	
		
	


主方法

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的操作。 }