Spring Boot 2 + Redis例子

Redis是一個key-value數據庫,支持存儲的value類型包括string(字符串)、list(鏈表)、set(集合)、zset(sorted set --有序集合)和hash(哈希類型)。在Java中,使用較爲普遍的客戶端有Redisson、Jedis。Spring Data Redis模塊默認使用Jedis。java

開發環境:IntelliJ IDEA 2019.2.2
Spring Boot版本:2.1.8web

1、項目構建redis

一、新建一個名稱爲demo的Spring Boot項目。
二、pom.xml spring

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

三、application.yml數據庫

配置Redis的相關屬性。瀏覽器

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

2、實體類 服務器

一、User.javaapp

映射Redis的數據:
(1) 類使用@RedisHash註解表示User對象會以hash類型保存;
(2) 字段id使用@Id註解表示自動生成guid;
(3) 字段使用@Indexed表示創建索引,在方法名查詢時須要設置此註解;spring-boot

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;
    }
}

3、數據訪問層測試

一、數據訪問層 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);
}

4、業務層

一、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);
    }
}

5、控制器

一、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}]

其它控制器方法略。

相關文章
相關標籤/搜索