SpringBoot+redis配置及測試

風泊月發表於2018-06-21

1.建立專案時選擇redis依賴


2.修改配置檔案,使用SpringBoot就避免了之前很多的xml檔案

2.1學過redis的同學都知道這個東西有叢集版也有單機版,無論哪個版本配置起來都很簡單

2.1.1首先找到配置檔案

   

2.1.2然後配置叢集版,直接在配置檔案內編輯即可


2.1.3配置單機版


3.測試 找到測試資料夾,自動注入redis模板


4.分別測試操作String和Hash型別的資料

4.1操作String

@Test
	public void testString(){
		//操作String型別的資料
		ValueOperations<String, String> valueStr = redisTemplate.opsForValue();
		//儲存一條資料
		valueStr.set("goodsProdu","長安");
		//獲取一條資料並輸出
		String goodsName = valueStr.get("goodsProdu");
		System.out.println(goodsName);
		//儲存多條資料
		Map<String,String> map = new HashMap<>();
		map.put("goodsName","福特汽車");
		map.put("goodsPrice","88888");
		map.put("goodsId","88");

		valueStr.multiSet(map);
		//獲取多條資料
		System.out.println("========================================");
		List<String>list = new ArrayList<>();
		list.add("goodsName");
		list.add("goodsPrice");
		list.add("goodsId");
		list.add("goodsProdu");

		List<String> listKeys = valueStr.multiGet(list);
		for (String key : listKeys) {
			System.out.println(key);
		}


	}

效果

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.3.RELEASE)
2018-06-21 09:45:31.328  INFO 8848 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
長安
========================================
福特汽車
88888
88
長安

4.2測試hash資料

@Test
	public void testHash(){
		//建立物件
		HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
		//儲存一條資料
		opsForHash.put("orderInfo","orderId","11");
		//獲取一條資料
		String value = opsForHash.get("orderInfo", "orderId");
		System.out.println(value);

		//儲存多條資料
		Map<String,String> map = new HashMap<>();
		map.put("createTime","2018-06-21");
		map.put("orderSn","888888");
		opsForHash.putAll("orderInfo",map);
		//獲取多條資料
		List<String> listKey = new ArrayList<>();
		listKey.add("createTime");
		listKey.add("orderSn");
		List<String> info = opsForHash.multiGet("orderInfo", listKey);
		for (String s : info) {
			System.out.println(s);

		}

	}

效果

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.3.RELEASE)

2018-06-21 09:48:26.020  INFO 3852 --- [           main] c.b.s.SpringbootRedisApplicationTests    : Starting SpringbootRedisApplicationTests on sixfly with PID 3852 (started by Administrator in D:\work_space\springbootdemo\springboot-redis)
2018-06-21 09:48:26.030  INFO 3852 --- [           main] c.b.s.SpringbootRedisApplicationTests    : No active profile set, falling back to default profiles: default
2018-06-21 09:48:26.174  INFO 3852 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2f953efd: startup date [Thu Jun 21 09:48:26 CST 2018]; root of context hierarchy
2018-06-21 09:48:28.398  INFO 3852 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-06-21 09:48:32.182  INFO 3852 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 
2018-06-21 09:48:35.054  INFO 3852 --- [           main] c.b.s.SpringbootRedisApplicationTests    : Started SpringbootRedisApplicationTests in 11.637 seconds (JVM running for 19.635)
2018-06-21 09:48:36.390  INFO 3852 --- [           main] io.lettuce.core.EpollProvider            : Starting without optional epoll library
2018-06-21 09:48:36.398  INFO 3852 --- [           main] io.lettuce.core.KqueueProvider           : Starting without optional kqueue library
11
2018-06-21
888888

相關文章