Spring Boot整合Redis

项目结构

Spring Boot整合Redis

 

 

 一、添加Redis依赖

     <!--redis依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

二、编写controller包下面的类

Spring Boot整合RedisSpring Boot整合Redis
package com.uos.blogs.controller;

import org.springframework.data.redis.core.index.Indexed;


public class Address {
    @Indexed
    private String city;
    @Indexed
    private String country;

    public Address(String city, String country) {
        this.city = city;
        this.country = country;
    }

    @Override
    public String toString() {
        return "Address{" +
                "city='" + city + '\'' +
                ", country='" + country + '\'' +
                '}';
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }
}
Address
Spring Boot整合RedisSpring Boot整合Redis
package com.uos.blogs.controller;

import org.springframework.data.redis.core.index.Indexed;


public class Family {
    @Indexed
    private String type;
    @Indexed
    private String username;

    @Override
    public String toString() {
        return "Family{" +
                "type='" + type + '\'' +
                ", username='" + username + '\'' +
                '}';
    }

    public Family(String type, String username) {
        this.type = type;
        this.username = username;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
Family
Spring Boot整合RedisSpring Boot整合Redis
package com.uos.blogs.controller;

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

import java.util.List;


@RedisHash("persons")
public class Person {
    @Id
    private String id;
    @Indexed
    private String firstname;
    @Indexed
    private String lastname;
    private Address address;
    private List<Family> familyList;

    public Person(String firstname, String lastname) {
        this.firstname = firstname;
        this.lastname = lastname;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", firstname='" + firstname + '\'' +
                ", lastname='" + lastname + '\'' +
                ", address='" + address + '\'' +
                ", familyList=" + familyList +
                '}';
    }

    public String getId() {
        return id;
    }

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

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public List<Family> getFamilyList() {
        return familyList;
    }

    public void setFamilyList(List<Family> familyList) {
        this.familyList = familyList;
    }
}
Person
Spring Boot整合RedisSpring Boot整合Redis
package com.uos.blogs.controller;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface PersonRepository extends CrudRepository<Person,String> {
    List<Person> findByLastname(String lastname);
    Page<Person> findPersonByLastname(String lastname, Pageable page);

    List<Person> findByFirstnameAndLastname(String firstname,String lastname);
    List<Person> findByAddress_city(String city);
    List<Person> findByFamilyList_username(String username);

}
PersonRepository

三、application.yml

spring:
  redis:
    host: 192.168.152.120
    port: 6379
    password: 1

四、编写测试类

package com.uos.blogs;

import com.uos.blogs.controller.Address;
import com.uos.blogs.controller.Family;
import com.uos.blogs.controller.Person;
import com.uos.blogs.controller.PersonRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

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

@SpringBootTest
class RedisApplicationTests {

    @Autowired
    private PersonRepository repository;

    @Test
    public void savePerson(){
        Person person = new Person("张","有才");
        Person person2 = new Person("James","Harden");
        //创建并添加住址信息
        Address address = new Address("北京","China");
        person.setAddress(address);
        //创建并添加家庭成员
        List<Family> list = new ArrayList<>();
        Family dad = new Family("父亲","张良");
        Family mom = new Family("母亲","李香君");
        list.add(dad);
        list.add(mom);
        person.setFamilyList(list);
        //向redis数据库中添加信息
        Person save = repository.save(person);
        Person save2 = repository.save(person2);
        System.out.println(save);
        System.out.println(save2);
    }
    @Test
    public void setPerson(){
        List<Person> list = repository.findByAddress_city("北京");
        System.out.println(list);
    }
    @Test
    public void updatePerson(){
        Person person = repository.findByFirstnameAndLastname("张","有才").get(0);
        person.setLastname("小明");
        Person update = repository.save(person);
        System.out.println(update);
    }
    @Test
    public void deletePerson(){
        Person person = repository.findByFirstnameAndLastname("张","有才").get(0);
        repository.delete(person);
    }



    @Test
    void contextLoads() {
    }

}