建设部人才交流中心网站,学科网站建设管理,佛山商城网站制作,现在花钱做那个网站好呀1.redis的数据类型,一共有5种.后面结合Jedis和redistemplate,以及单元测试junit一起验证 1)字符串 2)hash 3)列表 4)set(无序集合) 5)zset(有序集合) 2.Jedis的使用 a)引入依赖
!--加入springboot的starter的起步依赖--dependencygroupId…1.redis的数据类型,一共有5种.后面结合Jedis和redistemplate,以及单元测试junit一起验证 1)字符串 2)hash 3)列表 4)set(无序集合) 5)zset(有序集合) 2.Jedis的使用 a)引入依赖
!--加入springboot的starter的起步依赖--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactIdversion2.1.4.RELEASE/version/dependency
!--springboot整合redis--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactIdversion2.1.4.RELEASE/version/dependency
!--jedis依赖--dependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion2.9.1/version/dependency
!--lombok依赖--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/version/dependency
!--配置springboot注解执行器用于yml文件提示--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-configuration-processor/artifactIdversion2.1.4.RELEASE/version/dependency
!--springboot整合junit--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdversion2.1.4.RELEASE/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-test/artifactIdversion2.1.4.RELEASE/version/dependencydependencygroupIdorg.apache.commons/groupIdartifactIdcommons-pool2/artifactIdversion2.6.2/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13/versionscopetest/scope/dependency/dependenciesb)配置yml和启动文件.这里需要注意一下,在使用redis数据库时,是不需要密码验证的.但是在后面配置RedisPoolFactory时,需要配置登录密码.因此,在yml这里需要设置密码,否则在test时会报错.
redis:host: 127.0.0.1password: 123456port: 6379timeout: 6poolMaxTotal: 30poolMaxIdle: 30poolMaxWait: 30SpringBootApplication
public class MySpringBootApplication {public static void main(String[] args){SpringApplication.run(MySpringBootApplication.class,args);}}1)同时设置redis的登录密码
redis设置密码
验证登录: auth 123456
设置密码: config set requirepass 123456)编写配置文件注入参数
Data
Component
ConfigurationProperties(prefix redis)
public class RedisConfig1 {private String host;private int port;private int timeout; //秒private String password;private int poolMaxTotal; //资源池最大连接数private int poolMaxIdle; //资源池允许最大空闲连接数private int poolMaxWait; //超时时间 s
})配置RedisPoolFactory
Service
public class RedisPoolFactory {Autowiredprivate RedisConfig1 redisConfig1;Beanpublic JedisPool JedisPoolFactory(){JedisPoolConfig jedisPoolConfig new JedisPoolConfig();jedisPoolConfig.setMaxTotal(redisConfig1.getPoolMaxTotal());jedisPoolConfig.setMaxIdle(redisConfig1.getPoolMaxIdle());jedisPoolConfig.setMaxWaitMillis(redisConfig1.getPoolMaxWait() * 1000);JedisPool jedisPool new JedisPool(jedisPoolConfig, redisConfig1.getHost(), redisConfig1.getPort(),redisConfig1.getTimeout(), redisConfig1.getPassword());return jedisPool;}})编写测试用例 1)配置junit测试,注入RedisPoolFactory,RedisTemplate
RunWith(SpringRunner.class)
SpringBootTest(classes MySpringBootApplication.class)
public class TestJedis {Autowiredprivate RedisPoolFactory redisPoolFactory;Autowiredprivate RedisTemplate redisTemplate;
}2)测试字符串 Testpublic void test1(){Jedis jedis redisPoolFactory.JedisPoolFactory().getResource();//设置字符类型数据System.out.println(设置字符串类型数据);jedis.set(age,11);jedis.set(work,doctor);System.out.println(jedis.get(age));System.out.println(jedis.get(work));jedis.del(age,doctor);jedis.close();}3)测试hash Testpublic void test1(){Jedis jedis redisPoolFactory.JedisPoolFactory().getResource();//设置hash类型数据System.out.println(设置hash类型数据);jedis.hset(people1,age,11);jedis.hset(people1,work,teacher);jedis.hset(people1,name,李四);System.out.println(jedis.hget(people1, age));System.out.println(jedis.hget(people1,work));System.out.println(jedis.hget(people1,name));jedis.del(people1);jedis.close();} 4)测试列表 Testpublic void test1(){Jedis jedis redisPoolFactory.JedisPoolFactory().getResource();//设置列表类型数据System.out.println(设置列表类型数据);jedis.lpush(person,name,age,work);//左面弹栈ListString person2 jedis.lrange(person, 1, 2);for (String s : person2) {System.out.println(获取列表给定范围元素: s);}System.out.println(左面弹栈);System.out.println(jedis.lpop(person));System.out.println(jedis.lpop(person));System.out.println(jedis.lpop(person));//获取列表长度System.out.println(jedis.llen(person));//右面弹栈System.out.println(右面弹栈);System.out.println(jedis.rpop(person));System.out.println(jedis.rpop(person));System.out.println(jedis.rpop(person));jedis.close();}5)测试set Testpublic void test1(){Jedis jedis redisPoolFactory.JedisPoolFactory().getResource();//设置set类型数据 只可以添加元素jedis.sadd(people2, 22, 33, 44,person);System.out.println(jedis.smembers(people2));jedis.srem(people2,person);System.out.println(jedis.smembers(people2));jedis.close();}6)测试zset
Testpublic void test1(){Jedis jedis redisPoolFactory.JedisPoolFactory().getResource();//设置zset有序集合,排序set 从小到大jedis.zadd(score,92.00,小明);jedis.zadd(score,69.00,小兰);jedis.zadd(score,72.00,小红);System.out.println(jedis.zrange(score,0,2));jedis.close();}7)简单demo,排序学生成绩
Testpublic void test2(){Jedis jedis null;try {JedisPool jedisPool redisPoolFactory.JedisPoolFactory();jedis jedisPool.getResource();//造数据 学生成绩jedis.zadd(score,78,小明);jedis.zadd(score,90,小红);jedis.zadd(score,80,小蓝);jedis.zadd(score,33,小东);jedis.zadd(score,69,小兰);//打印学生排序后的成绩Long scoreLength jedis.zcard(score);SetString zrange jedis.zrange(score, 0, scoreLength);for (String s : zrange) {System.out.println(s);}}finally {if(jedis ! null){jedis.close();}}}2.RedisTemplate的使用 a)编写配置类,这里没有使用yml文件,采用的序列化机制 Jackson2JsonRedisSerializer
Configuration
public class RedisConfig extends JCacheConfigurerSupport {Beanpublic JedisPoolConfig jedisPoolConfig() {JedisPoolConfig jedisPoolConfig new JedisPoolConfig();//控制一个pool可分配多少个jedis实例jedisPoolConfig.setMaxTotal(500);//最大空闲数jedisPoolConfig.setMaxIdle(200);//每次释放连接的最大数目默认是3jedisPoolConfig.setNumTestsPerEvictionRun(1024);//逐出扫描的时间间隔(毫秒) 如果为负数,则不运行逐出线程, 默认-1jedisPoolConfig.setTimeBetweenEvictionRunsMillis(30000);//连接的最小空闲时间 默认1800000毫秒(30分钟)jedisPoolConfig.setMinEvictableIdleTimeMillis(-1);jedisPoolConfig.setSoftMinEvictableIdleTimeMillis(10000);//最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。jedisPoolConfig.setMaxWaitMillis(1500);jedisPoolConfig.setTestOnBorrow(true);jedisPoolConfig.setTestWhileIdle(true);jedisPoolConfig.setTestOnReturn(false);jedisPoolConfig.setJmxEnabled(true);jedisPoolConfig.setBlockWhenExhausted(false);return jedisPoolConfig;}Beanpublic JedisConnectionFactory connectionFactory(JedisPoolConfig jedisPoolConfig) {RedisStandaloneConfiguration redisStandaloneConfiguration new RedisStandaloneConfiguration();redisStandaloneConfiguration.setHostName(127.0.0.1);redisStandaloneConfiguration.setDatabase(0);redisStandaloneConfiguration.setPassword(123456);redisStandaloneConfiguration.setPort(6379);//获得默认的连接池构造器JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jpcb (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder)JedisClientConfiguration.builder();//指定jedisPoolConifig来修改默认的连接池构造器真麻烦滥用设计模式jpcb.poolConfig(jedisPoolConfig);//通过构造器来构造jedis客户端配置JedisClientConfiguration jedisClientConfiguration jpcb.build();//单机配置 客户端配置 jedis连接工厂return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);}Beanpublic RedisTemplateObject, Object redisTemplate(JedisConnectionFactory connectionFactory) {RedisTemplateObject, Object redisTemplate new RedisTemplate();redisTemplate.setConnectionFactory(connectionFactory);//初始化参数和初始化工作redisTemplate.afterPropertiesSet();return redisTemplate;}/*** description: 这里的入参使用了转配方式,由spring注入** params No such property: code for class: Script1* return*/Beanpublic RedisTemplateString,Object redisTemplate1(RedisConnectionFactory redisConnectionFactory){//采用jackson2JsonRedisSerializer序列化机制Jackson2JsonRedisSerializerObject objectJackson2JsonRedisSerializer new Jackson2JsonRedisSerializer(Object.class);ObjectMapper objectMapper new ObjectMapper();//配置采用直接处理字段的方式序列化和反序列化objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);//对象映射器启用默认类型objectMapper.activateDefaultTyping(LaissezFaireSubTypeValidator.instance,ObjectMapper.DefaultTyping.NON_FINAL,JsonTypeInfo.As.PROPERTY);objectJackson2JsonRedisSerializer.setObjectMapper(objectMapper);RedisTemplateString, Object template new RedisTemplate();//配置template参数template.setConnectionFactory(redisConnectionFactory);template.setKeySerializer(objectJackson2JsonRedisSerializer);template.setValueSerializer(objectJackson2JsonRedisSerializer);template.setHashKeySerializer(objectJackson2JsonRedisSerializer);template.setHashValueSerializer(objectJackson2JsonRedisSerializer);template.afterPropertiesSet();return template;}
}1)说明:缺少pom !--lombok依赖--dependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.20/version/dependencydependencygroupIdorg.junit.jupiter/groupIdartifactIdjunit-jupiter/artifactIdversionRELEASE/versionscopecompile/scope/dependency!--引入jackson的依赖--dependencygroupIdcom.fasterxml.jackson.core/groupIdartifactIdjackson-databind/artifactIdversion2.10.0/version/dependencydependencyb)编写测试用例
RunWith(SpringRunner.class)
SpringBootTest(classes MySpringBootApplication.class)
ContextConfiguration(classes {RedisConfig.class})
public class TestRedisTemplate {Autowiredprivate RedisTemplate redisTemplate;/*** description: 学习redistemplate** params No such property: code for class: Script1* return*/Testpublic void test1(){//字符串数据类型redisTemplate.opsForValue().set(sport,football);System.out.println(redisTemplate.opsForValue().get(sport));//hash数据类型redisTemplate.opsForHash().put(people,name,小明);redisTemplate.opsForHash().put(people,age,25);redisTemplate.opsForHash().put(people,salary,null);System.out.println(redisTemplate.opsForHash().get(people,name));System.out.println(redisTemplate.opsForHash().get(people,age));System.out.println(redisTemplate.opsForHash().get(people,salary));//列表//左边堆栈redisTemplate.opsForList().leftPushAll(人生能有几时秋,大江东去浪淘尽,666,醉酒当歌,人生几何);System.out.println(redisTemplate.opsForList().size(人生能有几时秋));//左边弹栈System.out.println(redisTemplate.opsForList().leftPop(人生能有几时秋));System.out.println(redisTemplate.opsForList().leftPop(人生能有几时秋));System.out.println(redisTemplate.opsForList().leftPop(人生能有几时秋));System.out.println(redisTemplate.opsForList().leftPop(人生能有几时秋));System.out.println(redisTemplate.opsForList().leftPop(人生能有几时秋));//set数据类型redisTemplate.opsForSet().add(person,小兰,小明,承瑞,兆雪);redisTemplate.opsForSet().difference(person,星辰大海);redisTemplate.opsForSet().difference(person,万古长灯);redisTemplate.opsForSet().difference(person,666);Set person redisTemplate.opsForSet().members(person);for (Object o : person) {System.out.println(o);}//zset数据类型 这里的数据是doubleredisTemplate.opsForZSet().add(student,小张,89);redisTemplate.opsForZSet().add(student,小兰,99);redisTemplate.opsForZSet().add(student,小红,79);redisTemplate.opsForZSet().add(student,小明,69);Long num redisTemplate.opsForZSet().count(student, 90, 100);System.out.println(num);Long numTotal redisTemplate.opsForZSet().size(student);Set studentRank redisTemplate.opsForZSet().range(student, 0, numTotal - 1);for (Object o : studentRank) {redisTemplate.opsForList().leftPushAll(studentList,o);}long students redisTemplate.opsForList().size(studentList);for (long i 0; i students; i) {System.out.println(String.format(第%s名: redisTemplate.opsForList().leftPop(studentList),i1));}}}4.使用Jedis和RedisTemplate a)RedisTemplate的配置比Jedis的配置类似,但是RedisTemplate使用更方便,它可以自动归还redis连接,同时封装的方法也要比Jedis多