In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about how to integrate SpringBoot and Redis. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
(1) add Spring Data Redis dependent initiator
When you introduce this dependency to create a project, the following dependencies appear in the project pom.xml file:
(2) write entity classes
Person:
Package com.hardy.springbootdataredis.domain;import org.springframework.data.annotation.Id;import org.springframework.data.redis.core.RedisHash;import org.springframework.data.redis.core.index.Indexed;/** * @ Author: HardyYao * @ Date: 2021-6-15 * / @ RedisHash ("persons") / / specify the storage space for operating entity class objects in the Redis database public class Person {@ Id / / identifies the entity class primary key private String id @ Indexed / / identifies the corresponding attribute to generate a secondary index private String firstname; @ Indexedprivate String lastname;private Address address;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 in the Redis database } public Address getAddress () {return address;} public void setAddress (Address address) {this.address = address @ Overridepublic String toString () {return "Person {" + "id='" + id +''+ ", firstname='" + firstname +''+ ", lastname='" + lastname +''+ ", address=" + address +'}';}}
Address:
Package com.hardy.springbootdataredis.domain;import org.springframework.data.redis.core.index.Indexed;/** * @ Author: HardyYao * @ Date: 2021-6-15 * / public class Address {@ Indexedprivate String city; @ Indexedprivate String 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 } @ Overridepublic String toString () {return "Address {" + "city='" + city +''+ ", country='" + country +''+'}';}}
In the above two entity classes, there are several comments about data manipulation of the Redis database:
RedisHash ("persons"): used to specify the storage space for manipulating entity class objects in the Redis database, which means that data operations on Person entity classes are stored in a storage space called persons in the Redis database.
Id: used to identify the primary key of the entity class. In the Redis database, a string HashKey is generated by default to represent the unique entity object id, but you can also specify the id manually when the data is stored.
Indexed: used to identify that the corresponding attribute generates a secondary index in the Redis database. After using this annotation, a secondary index corresponding to the attribute is generated in the database, and the index name is the attribute name, which can be easily queried.
(3) write Repository interface
SpringBoot provides automatic configuration for some common databases, including Redis. You can simplify the operation of adding, deleting, querying and modifying the data in the database by implementing the Repository interface:
Package com.hardy.springbootdataredis.repository;import com.hardy.springbootdataredis.domain.Person;import org.springframework.data.repository.CrudRepository;import java.util.List;/** * @ Author: HardyYao * @ Date: 2021-6-15 * / public interface PersonRepository extends CrudRepository {List findByAddress_City (String City);}
Note: Repository interface classes written when manipulating Redis databases need to inherit the lowest CrudRepository interface, not JpaRepository (JpaRepository is unique to SpringBoot Integration JPA). Of course, you can also import both SpringBoot integrated JPA dependencies and Redis dependencies in the project pom.xml file, so that you can write an operational Redis database that inherits the interface of JpaRepository.
(4) Redis database connection configuration
Add the connection configuration of the Redis database to the global configuration file application.properties of the project. The sample code is as follows:
# Redis server address spring.redis.host=127.0.0.1# Redis server connection port spring.redis.port=6379# Redis server connection password (default is empty) spring.redis.password= (5) write unit test method package com.hardy.springbootdataredis;import com.hardy.springbootdataredis.domain.Address;import com.hardy.springbootdataredis.domain.Person;import com.hardy.springbootdataredis.repository.PersonRepository;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired Import org.springframework.boot.test.context.SpringBootTest;import java.util.List;@SpringBootTestclass SpringbootdataRedisApplicationTests {@ Autowiredprivate PersonRepository repository; @ Testpublic void savePerson () {Person person = new Person (); person.setFirstname (Zhang); person.setLastname (three); Address address = new Address (); address.setCity (Beijing); address.setCountry (China); person.setAddress (address) / / add data to the Redis database Person save = repository.save (person);} @ Testpublic void selectPerson () {List list = repository.findByAddress_City ("Beijing"); for (Person person: list) {System.out.println (person);}} (6) Integration Test
Open the Redis client visual management tool, and first connect to the local Redis server:
After the connection is successful, you can see that there is no data in the local Redis database:
Run the two test methods written above to view the console print results:
To verify that the save () method does write the data to the local Redis database, open the Redis client visual management tool, refresh the data, and you can see that the data has been written successfully:
From the figure above, we can see that the data added by executing the save () method is successfully stored in the Redis database. In addition, a secondary index, such as address.city, firstname, lastname, and so on, is formed on the left side of the database list, which is generated by adding the @ Indexed annotation to the corresponding attribute when the Person class is created earlier. At the same time, because the secondary index of the corresponding attribute is generated in the Redis database, the specific data information can be queried through the secondary index. For example, repository.findByAddress_City ("Beijing") queries the data information whose index value is Beijing through the address.city index. If the secondary index of the corresponding property is not set, the data result queried through the property index will be empty.
These are all the contents of the article "how to integrate SpringBoot and Redis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.