当前位置: 首页 > news >正文

西安网站建设价格旅游网站开发实现开题报告

西安网站建设价格,旅游网站开发实现开题报告,网页设计模板素材图书馆,郑州网站建设渠道步骤 引入依赖配置数据库参数编写配置类构造RedisTemplate创建测试类测试 1.引入依赖 不写版本号&#xff0c;也是可以的 在pom中引入 <!--redis配置--> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-…

步骤

  1. 引入依赖
  2. 配置数据库参数
  3. 编写配置类
  4. 构造RedisTemplate
  5. 创建测试类测试

1.引入依赖

不写版本号,也是可以的

在pom中引入

<!--redis配置-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

2.配置数据库参数

在SpringBoot的yaml配置文件中引入,如果你用的是properties格式,那么去转换一下就行了,把yaml转换成properties

spring:redis:# Redis数据库索引(默认为0)database: 11# Redis服务器地址host: localhost# Redis服务器连接端口port: 6379# Redis服务器连接密码(默认为空)password: ''# 连接超时时间(毫秒)timeout: 1000

3.编写配置类

package com.kyw.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;@Configuration
public class RedisConfig {@Beanpublic RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory factory) {RedisTemplate<String,Object> template = new RedisTemplate<>();template.setConnectionFactory(factory);// 设置key的序列化方式template.setKeySerializer(RedisSerializer.string());// 设置value的序列化方式template.setValueSerializer(RedisSerializer.json());// 设置hash的key序列化方式template.setHashKeySerializer(RedisSerializer.string());// 设置hash的value序列化方式template.setHashValueSerializer(RedisSerializer.json());// 在设置完参数后生效template.afterPropertiesSet();return template;}}

4.构造RedisTemplate

上面的配置类中就构造好了

5.测试

这里用的是junit测试,是常用的测试工具,没有导入的小伙伴导入一下,网上搜一下很简单。

编写测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
//下面括号里面:classes  = SpringBootDemoApplication.class,如果报错,那你转换成自己的SpringBoot启动类的类名就行
@ContextConfiguration(classes  = SpringBootDemoApplication.class)
public class RedisTest {@Autowiredprivate RedisTemplate redisTemplate;}

测试String

/***String 测试*/
@Test
public void testStrings(){String redisKey = "test:count";//设置一个 key valueredisTemplate.opsForValue().set(redisKey,1);//获取valueSystem.out.println(redisTemplate.opsForValue().get(redisKey));//value自增System.out.println(redisTemplate.opsForValue().increment(redisKey));//value自减System.out.println(redisTemplate.opsForValue().decrement(redisKey));
}

测试哈希

/***哈希 测试*/
@Test
public void testHashes(){String redisKey = "test:user";redisTemplate.opsForHash().put(redisKey,"id",1);redisTemplate.opsForHash().put(redisKey,"username","张三");System.out.println(redisTemplate.opsForHash().get(redisKey,"id"));System.out.println(redisTemplate.opsForHash().get(redisKey,"username"));
}

 

测试list

/***list 类测试*/
@Test
public void testLists(){String redisKey = "test:ids";redisTemplate.opsForList().leftPush(redisKey,101);redisTemplate.opsForList().leftPush(redisKey,102);redisTemplate.opsForList().leftPush(redisKey,103);System.out.println(redisTemplate.opsForList().size(redisKey));System.out.println(redisTemplate.opsForList().index(redisKey,0));System.out.println(redisTemplate.opsForList().range(redisKey,0,2));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));System.out.println(redisTemplate.opsForList().leftPop(redisKey));}

 测试set

/***set 测试*/
@Test
public void testSets(){String redisKey = "test:teachers";redisTemplate.opsForSet().add(redisKey,"鸣人","路飞","韩立","炭治郎","祖国人");System.out.println(redisTemplate.opsForSet().size(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));System.out.println(redisTemplate.opsForSet().pop(redisKey));}

 

 测试zset

@Test
public void testSortSets(){String redisKey = "test:students";redisTemplate.opsForZSet().add(redisKey,"少年鸣人",10);redisTemplate.opsForZSet().add(redisKey,"仙人鸣人",60);redisTemplate.opsForZSet().add(redisKey,"九尾鸣人",70);redisTemplate.opsForZSet().add(redisKey,"九喇嘛联结鸣人",80);redisTemplate.opsForZSet().add(redisKey,"六道鸣人",90);System.out.println(redisTemplate.opsForZSet().zCard(redisKey));System.out.println(redisTemplate.opsForZSet().score(redisKey,"六道鸣人"));System.out.println(redisTemplate.opsForZSet().reverseRank(redisKey,"六道鸣人"));System.out.println(redisTemplate.opsForZSet().range(redisKey,0,4));System.out.println(redisTemplate.opsForZSet().removeRange(redisKey,0,2));}

 测试通用操作


/***测试通用操作*/
@Test
public void testKeys(){redisTemplate.delete("test:user");System.out.println(redisTemplate.hasKey("test:user"));redisTemplate.expire("text:students",10, TimeUnit.SECONDS);
}

测试绑定key到变量的操作

/***测试 绑定一个key 的操作*/
@Test
public void testBondOperations(){String redisKey = "test:count";//用 BoundValueOperations 将一个key绑定到对象上,方便后面的操作//绑定的是 String 那就 BoundValueOperations,是set 那就BoundSetOperations,其余同理BoundValueOperations operations = redisTemplate.boundValueOps(redisKey);System.out.println(operations.get());operations.increment();operations.increment();System.out.println(operations.get());}

如果您觉得文章还不错,麻烦点个赞吧

 

http://www.yayakq.cn/news/848258/

相关文章:

  • 泉州企业建站模板西安计算机培训机构排行榜
  • 广州网站开发广州亦客网络深圳网站推广优化培训
  • 个人网站制作新手教程显示代码wordpress
  • 创建一个网站的技术贴心的合肥网站建设
  • 国外网站引流如何做淄博信息港
  • 网站引导动画怎么做的电子商务网站的建设包含哪些流程
  • 邢台网站建设要多少钱软件开发工作流程
  • 网上花钱做ppt的网站淇县网站建设
  • 潜江 网站建设小程序做网站登录
  • 做网站快速排名怎么做网站的百度权重
  • 禾天姿网站建设网站建设和微信小程序
  • 网站 文件夹结构建网站首选公司
  • 申请个人网站wordpress图片链接插件
  • 简单网站首页怎么做管理信息系统平台
  • 怎么选择大连网站建设如何进入邮箱的网站
  • 如何自己制作微网站企业小型网站要多少钱
  • 百度海外视频网站建设兰州有什么互联网公司
  • 新手学做网站图纸一台云服务器做多个网站
  • 济宁 创意大厦 网站建设自助建站设计工作主要包括
  • 主题教育网站建立帮我写一个网页
  • 济南简单的网站制作电话营销话术
  • html怎么做查询网站吗学生作品网网站
  • 设计网站怎么设计乐清微网站建设
  • 淘宝联盟怎么样做网站社交平台网站建设预算
  • 高并发网站开发语言做网站框架需要什么软件
  • 企业网站首页排版分析做网站是什么公司
  • 什么叫网站权重大连网站开发多少钱
  • 网站开发团队分工北京企业网站制作
  • 网站建设方案项目背景意义做红包网站是犯法的吗
  • 商务网站建设与推广实训意义网页设计与制作课程教学痛点