jdk17+springboot3+redis

编程入门 行业动态 更新时间:2024-10-15 12:36:51

jdk17+springboot3+<a href=https://www.elefans.com/category/jswz/34/1771249.html style=redis"/>

jdk17+springboot3+redis

文章目录

    • springboot3+redis-stack
      • 1.基本准备
      • 2.使用maven搭建springboot项目,完整依赖如下:
      • 3.项目结构
      • 4.代码块

springboot3+redis-stack

1.基本准备

-1. 安装并启动redis-stack,防止与本地使用的redis端口冲突,使用6380端口docker run -d --name redis-stack-server -p 6380:6379 redis/redis-stack-server:latest
-2.启动后如图:![启动成功](.png#pic_center)

更详细介绍参考 [redis官方文档](/) `

2.使用maven搭建springboot项目,完整依赖如下:

<dependencies><dependency><groupId>com.redis.om</groupId><artifactId>redis-om-spring</artifactId><version>0.8.7-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>3.1.3</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><version>3.1.3</version><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional><version>1.18.24</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><version>3.1.3</version><scope>test</scope></dependency>
</dependencies><repositories><repository><id>snapshots-repo</id><url>/</url></repository>
</repositories>

3.项目结构

  • 项目结构图
  • 配置文件application.properties

spring.data.redis.host=127.0.0.1
spring.data.redis.port=6380
spring.data.redis.database=0
server.port=6333

4.代码块

  • controller
package org.micro.redistack.controller;import org.micro.redistack.domain.Person;
import org.micro.redistack.repository.PersonRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;import java.util.List;/*** @author harry* @date 2023/9/15*/
@RestController
@RequestMapping("/api/person/v1")
public class DataQueryController {@AutowiredPersonRepository personRepository;@GetMapping("/all")public List<Person> findAll() {return personRepository.findAll();}@GetMapping("/{id}")public Person findById(@PathVariable("id") String id) {return personRepository.findById(id).get();}@GetMapping("/query")public List<Person> query(@RequestParam("minAge") Integer minAge, @RequestParam("maxAge") Integer maxAge ) {return personRepository.findByAgeBetween(minAge,maxAge);}
}
  • 实体
package org.micro.redistack.domain;import java.util.Set;import lombok.*;
import org.springframework.data.annotation.Id;
import org.springframework.data.geo.Point;import com.redis.om.spring.annotations.Document;
import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;@RequiredArgsConstructor(staticName = "of")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
@Data
@Builder
@NoArgsConstructor
@Document
public class Person {// Id Field, also indexed@Id@Indexedprivate String id;// Indexed for exact text matching@Indexed @NonNullprivate String firstName;@Indexed @NonNullprivate String lastName;//Indexed for numeric matches@Indexed @NonNullprivate Integer age;@Indexed @NonNullprivate Double salary;//Indexed for Full Text matches@Searchable @NonNullprivate String personalStatement;//Indexed for Geo Filtering@Indexed @NonNullprivate Point homeLoc;// Nest indexed object@Indexed @NonNullprivate Address address;@Indexed @NonNullprivate Set<String> skills;
}
package org.micro.redistack.domain;import com.redis.om.spring.annotations.Indexed;
import com.redis.om.spring.annotations.Searchable;import lombok.Data;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;@Data
@RequiredArgsConstructor(staticName = "of")
public class Address {@NonNull@Indexedprivate String houseNumber;@NonNull@Searchable(nostem = true)private String street;@NonNull@Indexedprivate String city;@NonNull@Indexedprivate String state;@NonNull@Indexedprivate String postalCode;@NonNull@Indexedprivate String country;
}
  • 数据访问层
package org.micro.redistack.repository;import com.redis.om.spring.repository.RedisDocumentRepository;
import org.micro.redistack.domain.Person;
import org.springframework.stereotype.Repository;import java.util.List;/*** @author harry* @date 2023/9/14*/
@Repository
public interface PersonRepository extends RedisDocumentRepository<Person,String> {List<Person> findByAgeBetween(int minAge, int maxAge);
}
  • 项目启动类
package org.micro.redistack;import com.redis.om.spring.annotations.EnableRedisDocumentRepositories;
import org.micro.redistack.domain.Address;
import org.micro.redistack.domain.Person;
import org.micro.redistack.repository.PersonRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.geo.Point;import java.util.List;
import java.util.Set;/*** @author harry* @date 2023/9/14*/
@EnableRedisDocumentRepositories(basePackages = {"com.redis.om.*","org.micro.redistack"})
@SpringBootApplication
public class RediStackStarter {public static void main(String[] args) {SpringApplication.run(RediStackStarter.class, args);}//    @Bean
//    CommandLineRunner loadTestData(PersonRepository repo) {
//        return args -> {
//            repo.deleteAll();
//            String thorSays = "The Rabbit Is Correct, And Clearly The Smartest One Among You.";
//            // Serendipity, 248 Seven Mile Beach Rd, Broken Head NSW 2481, Australia
//            Address thorsAddress = Address.of("248", "Seven Mile Beach Rd", "Broken Head", "NSW", "2481", "Australia");
//            Person thor = Person.of("Chris", "Hemsworth", 38, thorSays, new Point(153.616667, -28.716667), thorsAddress, Set.of("hammer", "biceps", "hair", "heart"));
//            repo.save(thor);
//        };
//    }@BeanCommandLineRunner loadTestData(PersonRepository repo) {return args -> {repo.deleteAll();String thorSays = "The Rabbit Is Correct, And Clearly The Smartest One Among You.";String ironmanSays = "Doth mother know you weareth her drapes?";String blackWidowSays = "Hey, fellas. Either one of you know where the Smithsonian is? I’m here to pick up a fossil.";String wandaMaximoffSays = "You Guys Know I Can Move Things With My Mind, Right?";String gamoraSays = "I Am Going To Die Surrounded By The Biggest Idiots In The Galaxy.";String nickFurySays = "Sir, I’m Gonna Have To Ask You To Exit The Donut";// Serendipity, 248 Seven Mile Beach Rd, Broken Head NSW 2481, AustraliaAddress thorsAddress = Address.of("248", "Seven Mile Beach Rd", "Broken Head", "NSW", "2481", "Australia");// 11 Commerce Dr, Riverhead, NY 11901Address ironmansAddress = Address.of("11", "Commerce Dr", "Riverhead", "NY",  "11901", "US");// 605 W 48th St, New York, NY 10019Address blackWidowAddress = Address.of("605", "48th St", "New York", "NY", "10019", "US");// 20 W 34th St, New York, NY 10001Address wandaMaximoffsAddress = Address.of("20", "W 34th St", "New York", "NY", "10001", "US");// 107 S Beverly Glen Blvd, Los Angeles, CA 90024Address gamorasAddress = Address.of("107", "S Beverly Glen Blvd", "Los Angeles", "CA", "90024", "US");// 11461 Sunset Blvd, Los Angeles, CA 90049Address nickFuryAddress = Address.of("11461", "Sunset Blvd", "Los Angeles", "CA", "90049", "US");Person thor = Person.of("Chris", "Hemsworth", 38, 12345.32d,thorSays, new Point(153.616667, -28.716667), thorsAddress, Set.of("hammer", "biceps", "hair", "heart"));Person ironman = Person.of("Robert", "Downey", 56, 1500d,ironmanSays, new Point(40.9190747, -72.5371874), ironmansAddress, Set.of("tech", "money", "one-liners", "intelligence", "resources"));Person blackWidow = Person.of("Scarlett", "Johansson", 37, 8500d,blackWidowSays, new Point(40.7215259, -74.0129994), blackWidowAddress, Set.of("deception", "martial_arts"));Person wandaMaximoff = Person.of("Elizabeth", "Olsen", 32,685d, wandaMaximoffSays, new Point(40.6976701, -74.2598641), wandaMaximoffsAddress, Set.of("magic", "loyalty"));Person gamora = Person.of("Zoe", "Saldana", 43, 566d,gamoraSays, new Point(-118.399968, 34.073087), gamorasAddress, Set.of("skills", "martial_arts"));Person nickFury = Person.of("Samuel L.", "Jackson", 73, 255125d,nickFurySays, new Point(-118.4345534, 34.082615), nickFuryAddress, Set.of("planning", "deception", "resources"));repo.saveAll(List.of(thor, ironman, blackWidow, wandaMaximoff, gamora, nickFury));};}
}

文中示例代码皆来自官方文档,按上面配置后就实现了springboot3与redis-stack的结合。更多redis-stack的使用持续关注后续文章。

更多推荐

jdk17+springboot3+redis

本文发布于:2024-03-06 08:49:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1714888.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:redis

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!