摘要:本教程主要詳細(xì)講解它向提供平臺(tái)的抽象由基于庫的數(shù)據(jù)結(jié)構(gòu)存數(shù),以持久保存數(shù)據(jù),并可用作數(shù)據(jù)庫,緩存,消息代理等。
本教程主要詳細(xì)講解Spring Data Redis,它向Redis提供Spring Data平臺(tái)的抽象.
Redis由基于key/value庫的數(shù)據(jù)結(jié)構(gòu)存數(shù),以持久保存數(shù)據(jù),并可用作數(shù)據(jù)庫,緩存,消息代理等。
基礎(chǔ)環(huán)境技術(shù) | 版本 |
---|---|
Java | 1.8+ |
SpringBoot | 2.x.x |
DataJPA | 2.x.x |
Jedis | 2.9.x |
初始化項(xiàng)目
mvn archetype:generate -DgroupId=com.edurt.sli.slidr -DartifactId=spring-learn-integration-datajpa-redis -DarchetypeArtifactId=maven-archetype-quickstart -Dversion=1.0.0 -DinteractiveMode=false
修改pom.xml增加redis的支持
spring-learn-integration-datajpa com.edurt.sli 1.0.0 4.0.0 spring-learn-integration-datajpa-redis Spring DataJPA Redis教程(基礎(chǔ)版) org.springframework.boot spring-boot-starter-web ${dependency.springboot2.common.version} org.springframework.boot spring-boot-starter-data-redis ${dependency.springboot2.common.version} org.projectlombok lombok ${dependency.lombok.version} redis.clients jedis ${dependency.jedis.version} io.lettuce lettuce-core ${dependency.lettuce.version} org.springframework.boot spring-boot-maven-plugin ${dependency.springboot2.common.version} true org.apache.maven.plugins maven-compiler-plugin ${plugin.maven.compiler.version} ${system.java.version}
spring-boot-starter-data-redis整合Redis需要的依賴包,或者多帶帶使用spring-data-redis和jedis依賴包
一個(gè)簡(jiǎn)單的應(yīng)用類
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at *配置支持Redis* http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.slidr; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** *
SpringBootDataJPARedisIntegration
*Description : SpringBootDataJPARedisIntegration
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-14 00:44
*Author Email: qianmoQ
*/ @SpringBootApplication public class SpringBootDataJPARedisIntegration { public static void main(String[] args) { SpringApplication.run(SpringBootDataJPARedisIntegration.class, args); } }
在resources資源目錄下創(chuàng)建一個(gè)application.properties的配置文件,內(nèi)容如下
spring.redis.host=localhost spring.redis.port=6379 spring.redis.database=0 spring.redis.jedis.pool.max-active=8 spring.redis.jedis.pool.max-wait=1ms spring.redis.lettuce.pool.max-active=8 spring.redis.jedis.pool.min-idle=0操作Redis數(shù)據(jù)
在/src/main/java/com/edurt/sli/slidr目錄下創(chuàng)建model目錄,并在該目錄下新建RedisModel文件
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.slidr.model; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import lombok.ToString; import org.springframework.data.annotation.Id; import org.springframework.data.redis.core.RedisHash; /** *
RedisModel
*Description : RedisModel
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-14 01:06
*Author Email: qianmoQ
*/ @RedisHash(value = "Redis") @Data @ToString @AllArgsConstructor @NoArgsConstructor public class RedisModel { @Id private String id; private String name; }
@RedisHash為每個(gè)數(shù)據(jù)庫創(chuàng)建域類的空子類。
@Id注解的屬性和被命名為id的屬性會(huì)被當(dāng)作標(biāo)識(shí)屬性
在/src/main/java/com/edurt/sli/slidr目錄下創(chuàng)建repository目錄,并在該目錄下新建RedisSupport文件
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.slidr.repository; import com.edurt.sli.slidr.model.RedisModel; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; /** *
RedisSupport
*Description : RedisSupport
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-14 00:59
*Author Email: qianmoQ
*/ @Repository public interface RedisSupport extends CrudRepository{ }
在CrudRepository中提供了一些基礎(chǔ)的增刪改查的功能.
測(cè)試增刪改查的功能
在/src/main/java/com/edurt/sli/slidr目錄下創(chuàng)建controller目錄,并在該目錄下新建RedisController文件
/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 *
* Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.edurt.sli.slidr.controller; import com.edurt.sli.slidr.model.RedisModel; import com.edurt.sli.slidr.repository.RedisSupport; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; /** *
RedisController
*Description : RedisController
*Author : qianmoQ
*Version : 1.0
*Create Time : 2019-06-14 01:05
*Author Email: qianmoQ
*/ @RestController @RequestMapping(value = "redis") public class RedisController { @Autowired private RedisSupport support; @GetMapping public Object get() { return this.support.findAll(); } @PostMapping public Object post(@RequestBody RedisModel mode) { return this.support.save(mode); } @PutMapping public Object put(@RequestBody RedisModel mode) { return this.support.save(mode); } @DeleteMapping public Object delete(@RequestParam String id) { this.support.deleteById(id); return "SUCCESS"; } }
添加數(shù)據(jù)
shicheng@shichengdeMacBook-Pro ~> curl -X POST http://localhost:8080/redis -H "Content-Type:application/json" -d "{"id": "1", "name": "Hello Redis"}" {"id":"1","name":"Hello Redis"}?
修改數(shù)據(jù)
shicheng@shichengdeMacBook-Pro ~> curl -X PUT http://localhost:8080/redis -H "Content-Type:application/json" -d "{"id": "1", "name": "Hello Redis Modfiy"}" {"id":"1","name":"Hello Redis Modfiy"}?
獲取數(shù)據(jù)
shicheng@shichengdeMacBook-Pro ~> curl -X GET http://localhost:8080/redis [{"id":"1","name":"Hello Redis Modfiy"}]?
刪除數(shù)據(jù)
shicheng@shichengdeMacBook-Pro ~> curl -X DELETE "http://localhost:8080/redis?id=1" SUCCESS?打包文件部署
打包數(shù)據(jù)
mvn clean package -Dmaven.test.skip=true -X
運(yùn)行打包后的文件即可
java -jar spring-learn-integration-datajpa/spring-learn-integration-datajpa-redis/target/spring-learn-integration-datajpa-redis-1.0.0.jar源碼地址
GitHub
Gitee
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/77749.html
摘要:菜鳥教程框架中文手冊(cè)入門目標(biāo)使用搭建通過對(duì)數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡(jiǎn)單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計(jì)指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊(cè)入門目標(biāo)使用搭建通過對(duì)數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡(jiǎn)單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計(jì)指南 RESTful API指南 依賴注入 D...
摘要:菜鳥教程框架中文手冊(cè)入門目標(biāo)使用搭建通過對(duì)數(shù)據(jù)增刪查改沒了純粹占行用的拜 后端API入門學(xué)習(xí)指北 了解一下一下概念. RESTful API標(biāo)準(zhǔn)] 所有的API都遵循[RESTful API標(biāo)準(zhǔn)]. 建議大家都簡(jiǎn)單了解一下HTTP協(xié)議和RESTful API相關(guān)資料. 阮一峰:理解RESTful架構(gòu) 阮一峰:RESTful API 設(shè)計(jì)指南 RESTful API指南 依賴注入 D...
摘要:大家好,我是冰河有句話叫做投資啥都不如投資自己的回報(bào)率高。馬上就十一國慶假期了,給小伙伴們分享下,從小白程序員到大廠高級(jí)技術(shù)專家我看過哪些技術(shù)類書籍。 大家好,我是...
閱讀 2869·2021-07-30 15:30
閱讀 563·2019-08-30 15:55
閱讀 1634·2019-08-26 17:04
閱讀 644·2019-08-26 11:36
閱讀 2085·2019-08-26 10:58
閱讀 3565·2019-08-23 14:34
閱讀 1567·2019-08-22 18:48
閱讀 2536·2019-08-21 17:51