- N +

Spring DataJPA Redis教程(基础版)

Spring DataJPA Redis教程(基础版)原标题:Spring DataJPA Redis教程(基础版)

导读:

本教程主要详细讲解Spring Data Redis,它向Redis提供Spring Data平台的抽象.Redis由基于key/value库的数据结构存数,以持久保存数据,并...

文章目录 [+]

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

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

基础环境


技术版本
java1.8+
SpringBoot2.x.x
DataJPA2.x.x
Jedis2.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的支持

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0"

  3.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4.         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


  5.    <parent>

  6.        <artifactId>spring-learn-integration-datajpa</artifactId>

  7.        <groupId>com.edurt.sli</groupId>

  8.        <version>1.0.0</version>

  9.    </parent>


  10.    <modelVersion>4.0.0</modelVersion>


  11.    <artifactId>spring-learn-integration-datajpa-redis</artifactId>


  12.    <name>Spring DataJPA Redis教程(基础版)</name>


  13.    <dependencies>

  14.        <dependency>

  15.            <groupId>org.springframework.boot</groupId>

  16.            <artifactId>spring-boot-starter-web</artifactId>

  17.            <version>${dependency.springboot2.common.version}</version>

  18.        </dependency>

  19.        <dependency>

  20.            <groupId>org.springframework.boot</groupId>

  21.            <artifactId>spring-boot-starter-data-redis</artifactId>

  22.            <version>${dependency.springboot2.common.version}</version>

  23.        </dependency>

  24.        <dependency>

  25.            <groupId>org.projectlombok</groupId>

  26.            <artifactId>lombok</artifactId>

  27.            <version>${dependency.lombok.version}</version>

  28.        </dependency>

  29.        <dependency>

  30.            <groupId>redis.clients</groupId>

  31.            <artifactId>jedis</artifactId>

  32.            <version>${dependency.jedis.version}</version>

  33.        </dependency>

  34.        <dependency>

  35.            <groupId>io.lettuce</groupId>

  36.            <artifactId>lettuce-core</artifactId>

  37.            <version>${dependency.lettuce.version}</version>

  38.        </dependency>

  39.    </dependencies>


  40.    <build>

  41.        <plugins>

  42.            <plugin>

  43.                <groupId>org.springframework.boot</groupId>

  44.                <artifactId>spring-boot-maven-plugin</artifactId>

  45.                <version>${dependency.springboot2.common.version}</version>

  46.                <configuration>

  47.                    <fork>true</fork>

  48.                </configuration>

  49.            </plugin>

  50.            <plugin>

  51.                <groupId>org.apache.maven.plugins</groupId>

  52.                <artifactId>maven-compiler-plugin</artifactId>

  53.                <version>${plugin.maven.compiler.version}</version>

  54.                <configuration>

  55.                    <source>${system.java.version}</source>

  56.                    <target>${system.java.version}</target>

  57.                </configuration>

  58.            </plugin>

  59.        </plugins>

  60.    </build>


  61. </project>

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

  • 一个简单的应用类

  1. /**

  2. * Licensed to the Apache Software Foundation (ASF) under one

  3. * or more contributor license agreements.  See the NOTICE file

  4. * distributed with this work for additional information

  5. * regarding copyright ownership.  The ASF licenses this file

  6. * to you under the Apache License, Version 2.0 (the

  7. * "License"); you may not use this file except in compliance

  8. * with the License.  You may obtain a copy of the License at

  9. * <p>

  10. * http://www.apache.org/licenses/LICENSE-2.0

  11. * <p>

  12. * Unless required by applicable law or agreed to in writing, software

  13. * distributed under the License is distributed on an "AS IS" BASIS,

  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  15. * See the License for the specific language governing permissions and

  16. * limitations under the License.

  17. */

  18. package com.edurt.sli.slidr;


  19. import org.springframework.boot.SpringApplication;

  20. import org.springframework.boot.autoconfigure.SpringBootApplication;


  21. /**

  22. * <p> SpringBootDataJPARedisIntegration </p>

  23. * <p> Description : SpringBootDataJPARedisIntegration </p>

  24. * <p> Author : qianmoQ </p>

  25. * <p> Version : 1.0 </p>

  26. * <p> Create Time : 2019-06-14 00:44 </p>

  27. * <p> Author Email: <a >qianmoQ</a> </p>

  28. */

  29. @SpringBootApplication

  30. public class SpringBootDataJPARedisIntegration {


  31.    public static void main(String[] args) {

  32.        SpringApplication.run(SpringBootDataJPARedisIntegration.class, args);

  33.    }


  34. }

配置支持Redis


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

spring.redis.host=localhostspring.redis.port=6379spring.redis.database=0spring.redis.jedis.pool.max-active=8spring.redis.jedis.pool.max-wait=1msspring.redis.lettuce.pool.max-active=8spring.redis.jedis.pool.min-idle=0

操作Redis数据


  • 在 /src/main/java/com/edurt/sli/slidr目录下创建model目录,并在该目录下新建RedisModel文件

  1. /**

  2. * Licensed to the Apache Software Foundation (ASF) under one

  3. * or more contributor license agreements.  See the NOTICE file

  4. * distributed with this work for additional information

  5. * regarding copyright ownership.  The ASF licenses this file

  6. * to you under the Apache License, Version 2.0 (the

  7. * "License"); you may not use this file except in compliance

  8. * with the License.  You may obtain a copy of the License at

  9. * <p>

  10. * http://www.apache.org/licenses/LICENSE-2.0

  11. * <p>

  12. * Unless required by applicable law or agreed to in writing, software

  13. * distributed under the License is distributed on an "AS IS" BASIS,

  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  15. * See the License for the specific language governing permissions and

  16. * limitations under the License.

  17. */

  18. package com.edurt.sli.slidr.model;


  19. import lombok.AllArgsConstructor;

  20. import lombok.Data;

  21. import lombok.NoArgsConstructor;

  22. import lombok.ToString;

  23. import org.springframework.data.annotation.Id;

  24. import org.springframework.data.redis.core.RedisHash;


  25. /**

  26. * <p> RedisModel </p>

  27. * <p> Description : RedisModel </p>

  28. * <p> Author : qianmoQ </p>

  29. * <p> Version : 1.0 </p>

  30. * <p> Create Time : 2019-06-14 01:06 </p>

  31. * <p> Author Email: <a >qianmoQ</a> </p>

  32. */

  33. @RedisHash(value = "Redis")

  34. @Data

  35. @ToString

  36. @AllArgsConstructor

  37. @NoArgsConstructor

  38. public class RedisModel {


  39.    @Id

  40.    private String id;

  41.    private String name;


  42. }

@RedisHash为每个数据库创建域类的空子类。

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

  • 在 /src/main/java/com/edurt/sli/slidr目录下创建repository目录,并在该目录下新建RedisSupport文件

  1. /**

  2. * Licensed to the Apache Software Foundation (ASF) under one

  3. * or more contributor license agreements.  See the NOTICE file

  4. * distributed with this work for additional information

  5. * regarding copyright ownership.  The ASF licenses this file

  6. * to you under the Apache License, Version 2.0 (the

  7. * "License"); you may not use this file except in compliance

  8. * with the License.  You may obtain a copy of the License at

  9. * <p>

  10. * http://www.apache.org/licenses/LICENSE-2.0

  11. * <p>

  12. * Unless required by applicable law or agreed to in writing, software

  13. * distributed under the License is distributed on an "AS IS" BASIS,

  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  15. * See the License for the specific language governing permissions and

  16. * limitations under the License.

  17. */

  18. package com.edurt.sli.slidr.repository;


  19. import com.edurt.sli.slidr.model.RedisModel;

  20. import org.springframework.data.repository.CrudRepository;

  21. import org.springframework.stereotype.Repository;


  22. /**

  23. * <p> RedisSupport </p>

  24. * <p> Description : RedisSupport </p>

  25. * <p> Author : qianmoQ </p>

  26. * <p> Version : 1.0 </p>

  27. * <p> Create Time : 2019-06-14 00:59 </p>

  28. * <p> Author Email: <a >qianmoQ</a> </p>

  29. */

  30. @Repository

  31. public interface RedisSupport extends CrudRepository<RedisModel, String> {

  32. }

CrudRepository中提供了一些基础的增删改查的功能.

  • 测试增删改查的功能

/src/main/java/com/edurt/sli/slidr目录下创建controller目录,并在该目录下新建RedisController文件

  1. /**

  2. * Licensed to the Apache Software Foundation (ASF) under one

  3. * or more contributor license agreements.  See the NOTICE file

  4. * distributed with this work for additional information

  5. * regarding copyright ownership.  The ASF licenses this file

  6. * to you under the Apache License, Version 2.0 (the

  7. * "License"); you may not use this file except in compliance

  8. * with the License.  You may obtain a copy of the License at

  9. * <p>

  10. * http://www.apache.org/licenses/LICENSE-2.0

  11. * <p>

  12. * Unless required by applicable law or agreed to in writing, software

  13. * distributed under the License is distributed on an "AS IS" BASIS,

  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

  15. * See the License for the specific language governing permissions and

  16. * limitations under the License.

  17. */

  18. package com.edurt.sli.slidr.controller;


  19. import com.edurt.sli.slidr.model.RedisModel;

  20. import com.edurt.sli.slidr.repository.RedisSupport;

  21. import org.springframework.beans.factory.annotation.Autowired;

  22. import org.springframework.web.bind.annotation.*;


  23. /**

  24. * <p> RedisController </p>

  25. * <p> Description : RedisController </p>

  26. * <p> Author : qianmoQ </p>

  27. * <p> Version : 1.0 </p>

  28. * <p> Create Time : 2019-06-14 01:05 </p>

  29. * <p> Author Email: <a >qianmoQ</a> </p>

  30. */

  31. @RestController

  32. @RequestMapping(value = "redis")

  33. public class RedisController {


  34.    @Autowired

  35.    private RedisSupport support;


  36.    @GetMapping

  37.    public Object get() {

  38.        return this.support.findAll();

  39.    }


  40.    @PostMapping

  41.    public Object post(@RequestBody RedisModel mode) {

  42.        return this.support.save(mode);

  43.    }


  44.    @PutMapping

  45.    public Object put(@RequestBody RedisModel mode) {

  46.        return this.support.save(mode);

  47.    }


  48.    @DeleteMapping

  49.    public Object delete(@RequestParam String id) {

  50.        this.support.deleteById(id);

  51.        return "SUCCESS";

  52.    }


  53. }

添加数据

[email protected] ~> curl -X POST http://localhost:8080/redis -H 'Content-Type:application/json' -d '{"id": "1", "name": "Hello Redis"}'{"id":"1","name":"Hello Redis"}⏎

修改数据

[email protected] ~> 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"}⏎

获取数据

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

删除数据

[email protected] ~> 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


本文标题:Spring DataJPA Redis教程(基础版)
本文链接:http://www.kyjszj.com/htzq/367.html
作者授权:除特别说明外,本文由 开源技术之家 原创编译并授权 开源技术之家 刊载发布。
版权声明:本文不使用任何协议授权,您可以任何形式自由转载或使用。

有好的文章希望我们帮助分享和推广,猛戳这里我要投稿

返回列表
上一篇:
下一篇:

发表评论中国互联网举报中心

快捷回复:

    评论列表 (暂无评论,共252人参与)参与讨论

    还没有评论,来说两句吧...