资讯专栏INFORMATION COLUMN

Spring DataJPA Redis教程(基础版)

高璐 / 3529人阅读

摘要:本教程主要详细讲解它向提供平台的抽象由基于库的数据结构存数,以持久保存数据,并可用作数据库,缓存,消息代理等。

本教程主要详细讲解Spring Data Redis,它向Redis提供Spring Data平台的抽象.

Redis由基于key/value库的数据结构存数,以持久保存数据,并可用作数据库,缓存,消息代理等。

基础环境
技术 版本
Java 1.8+
SpringBoot 2.x.x
DataJPA 2.x.x
Jedis 2.9.x
创建项目

初始化项目

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教程(基础版)

    
        
            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}
                    ${system.java.version}
                
            
        
    

spring-boot-starter-data-redis整合Redis需要的依赖包,或者多带带使用spring-data-redisjedis依赖包

一个简单的应用类

/**
 * 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; 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); } }
配置支持Redis

在resources资源目录下创建一个application.properties的配置文件,内容如下

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数据

/src/main/java/com/edurt/sli/slidr目录下创建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为每个数据库创建域类的空子类。

@Id注解的属性和被命名为id的属性会被当作标识属性

/src/main/java/com/edurt/sli/slidr目录下创建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中提供了一些基础的增删改查的功能.

测试增删改查的功能

/src/main/java/com/edurt/sli/slidr目录下创建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"; } }

添加数据

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"}⏎

修改数据

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"}⏎

获取数据

shicheng@shichengdeMacBook-Pro ~> curl -X GET http://localhost:8080/redis
[{"id":"1","name":"Hello Redis Modfiy"}]⏎

删除数据

shicheng@shichengdeMacBook-Pro ~> curl -X DELETE "http://localhost:8080/redis?id=1"
SUCCESS⏎

打包文件部署

打包数据

mvn clean package -Dmaven.test.skip=true -X

运行打包后的文件即可

java -jar spring-learn-integration-datajpa/spring-learn-integration-datajpa-redis/target/spring-learn-integration-datajpa-redis-1.0.0.jar
源码地址

GitHub

Gitee

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/77749.html

相关文章

  • 后端API从入门到放弃指北

    摘要:菜鸟教程框架中文手册入门目标使用搭建通过对数据增删查改没了纯粹占行用的拜 后端API入门学习指北 了解一下一下概念. RESTful API标准] 所有的API都遵循[RESTful API标准]. 建议大家都简单了解一下HTTP协议和RESTful API相关资料. 阮一峰:理解RESTful架构 阮一峰:RESTful API 设计指南 RESTful API指南 依赖注入 D...

    Jeffrrey 评论0 收藏0
  • 后端API从入门到放弃指北

    摘要:菜鸟教程框架中文手册入门目标使用搭建通过对数据增删查改没了纯粹占行用的拜 后端API入门学习指北 了解一下一下概念. RESTful API标准] 所有的API都遵循[RESTful API标准]. 建议大家都简单了解一下HTTP协议和RESTful API相关资料. 阮一峰:理解RESTful架构 阮一峰:RESTful API 设计指南 RESTful API指南 依赖注入 D...

    sf190404 评论0 收藏0
  • 后端API从入门到放弃指北

    摘要:菜鸟教程框架中文手册入门目标使用搭建通过对数据增删查改没了纯粹占行用的拜 后端API入门学习指北 了解一下一下概念. RESTful API标准] 所有的API都遵循[RESTful API标准]. 建议大家都简单了解一下HTTP协议和RESTful API相关资料. 阮一峰:理解RESTful架构 阮一峰:RESTful API 设计指南 RESTful API指南 依赖注入 D...

    Airmusic 评论0 收藏0
  • 从小白程序员一路晋升为大厂高级技术专家我看过哪些书籍?(建议收藏)

    摘要:大家好,我是冰河有句话叫做投资啥都不如投资自己的回报率高。马上就十一国庆假期了,给小伙伴们分享下,从小白程序员到大厂高级技术专家我看过哪些技术类书籍。 大家好,我是...

    sf_wangchong 评论0 收藏0

发表评论

0条评论

高璐

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<