当前位置 : 主页 > 编程语言 > java >

Spring Boot 2和Redis例子实现过程解析

来源:互联网 收集:自由互联 发布时间:2021-05-13
这篇文章主要介绍了Spring Boot2发布与调用REST服务过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Redis是一个key-va

这篇文章主要介绍了Spring Boot2发布与调用REST服务过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Redis是一个key-value数据库,支持存储的value类型包括string(字符串)、list(链表)、set(集合)、zset(sorted set --有序集合)和hash(哈希类型)。在Java中,使用较为广泛的客户端有Redisson、Jedis。Spring Data Redis模块默认使用Jedis。

开发环境:IntelliJ IDEA 2019.2.2

Spring Boot版本:2.1.8

一、项目构建

1、新建一个名称为demo的Spring Boot项目。

2、pom.xml

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-devtools</artifactId>
    </dependency>

3、application.yml

配置Redis的相关属性。

spring:
 redis:
  port: 6379
  password: 123456
  host: localhost

二、实体类

1、User.java

映射Redis的数据:

(1) 类使用@RedisHash注解表示User对象会以hash类型保存;

(2) 字段id使用@Id注解表示自动生成guid;

(3) 字段使用@Indexed表示建立索引,在方法名查询时需要设置此注解;

package com.example.demo.entity;

import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;


@RedisHash("user")
public class User {
  @Id
  private String id;
  @Indexed
  private String name;
  @Indexed
  private Integer age;

  public String getId() {
    return id;
  }

  public void setId(String id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public Integer getAge() {
    return age;
  }

  public void setAge(Integer age) {
    this.age = age;
  }
}

三、数据访问层

1、数据访问层 UserRepository.java

继承CrudRepository可以不需要任何实现,拥有基本的CRUD操作。也可以手动添加代码实现方法名查询。

package com.example.demo.repository;

import com.example.demo.entity.User;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface UserRepository extends CrudRepository<User,String> {

  //方法名查询:根据字段name查询
  List<User> findByName(String name);

  //方法名查询:根据字段name和age的And查询
  List<User> findByNameAndAge(String name, Integer age);

  //方法名查询:根据字段name和age的Or查询
  List<User> findByNameOrAge(String name, Integer age);
}

四、业务层

1、UserRepository.java

package com.example.demo.service;

import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class UserService {
  @Autowired
  UserRepository userRepository;

  public List<User> getUsers(){
    Iterable<User> users = userRepository.findAll();
    List<User> datas = new ArrayList<User>();
    for(Iterator<User> it = users.iterator(); it.hasNext();){
      User u = it.next();
      datas.add(u);
    }
    return datas;
  }

  public void save(User user) {
    userRepository.save(user);
  }

  public List<User> findByName(String name){
    return userRepository.findByName(name);
  }

  public List<User> findByNameAndAge(String name, Integer age){
    return userRepository.findByNameAndAge(name, age);
  }

  public List<User> findByNameOrAge(String name, Integer age){
    return userRepository.findByNameOrAge(name, age);
  }
}

五、控制器

1、UserController.java

package com.example.demo;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class UserController {
  @Resource
  UserService userService;

  @RequestMapping("/save")
  public String save(){
    for(int i=1;i<=20;i++){
      User user = new User();
      user.setName("a" + i);
      user.setAge(i);
      userService.save(user);
    }
    return "添加成功";
  }

  @RequestMapping("/getUsers")
  public List<User> getUsers(){
    List<User> users = userService.getUsers();
    return users;
  }

  @RequestMapping("/findByName")
  public List<User> findByName(String name){
    return userService.findByName(name);
  }

  @RequestMapping("/findByNameAndAge")
  public List<User> findByNameAndAge(String name, Integer age){
    return userService.findByNameAndAge(name, age);
  }

  @RequestMapping("/findByNameOrAge")
  public List<User> findByNameOrAge(String name, Integer age){
    return userService.findByNameOrAge(name, age);
  }
}

启动服务器,浏览器访问:http://localhost:8080/save 生成20条测试数据,访问http://localhost:8080/getUsers,页面显示如下:

[{"id":"8da6f5ad-b24c-4725-9c69-4d709369eac7","name":"a14","age":14},{"id":"b62b2028-4d41-41be-aeff-e2b56776dd87","name":"a10","age":10},{"id":"c8330a2a-902b-4db9-b262-65d182027eac","name":"a6","age":6},{"id":"cc7b5a91-0ca6-4577-891a-835a1cebef6f","name":"a11","age":11},{"id":"09f8f2e3-2cf8-48f7-a535-1897b7cc0f35","name":"a1","age":1},{"id":"3344413f-8128-42ad-8343-304005c87e5f","name":"a7","age":7},{"id":"db74f020-f3e3-4422-8013-c515b9d8551d","name":"a5","age":5},{"id":"7fc46621-ecaf-47c6-bae3-f2061104d802","name":"a2","age":2},{"id":"dcdcdda0-bab0-450a-b6e6-fdce410b01fd","name":"a8","age":8},{"id":"b456f4fd-158f-43e9-86cf-0340b809ffab","name":"a12","age":12},{"id":"4ffb6019-21b1-4880-8ec3-2703f561d028","name":"a15","age":15},{"id":"7e45d738-7a31-4b24-928e-a9da3583ef6d","name":"a4","age":4},{"id":"7992e502-4049-4d40-bc35-aab72adbe335","name":"a19","age":19},{"id":"3df7b2ac-6bb0-496d-91d2-05ac763e5f68","name":"a13","age":13},{"id":"7c94edba-f91e-42fe-b8b4-7c6a9ba82747","name":"a3","age":3},{"id":"39402099-17ba-4368-ac23-dd5e9fe1d8ae","name":"a17","age":17},{"id":"e49750d6-ce80-4764-87d3-1566d9263360","name":"a18","age":18},{"id":"499e1a9a-b947-4eec-8d37-d1cdf7ac1179","name":"a16","age":16},{"id":"04f0cb4e-c17b-4b4e-b135-e6c222aace21","name":"a9","age":9},{"id":"ac48ce35-5a38-48b8-90b9-6b999c3fd242","name":"a20","age":20}]

其它控制器方法略。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

网友评论