Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Introduction to the usage of Spring Data Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "introduction to the usage of Spring Data Redis". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Spring Data Redis introduction

Spring Data Redis is officially launched by Spring, which can be regarded as a sub-framework of Spring framework integrating Redis operations, encapsulating many commands of Redis, and it is very convenient to use Spring to operate Redis databases. Spring provides similar integration to many tools, such as Spring Data MongDB, Spring Data JPA and so on. Spring Data Redis is just one of them.

Environment building

To use SDR, we first need to build a Spring+SpringMVC environment. Since this is not the focus of this article, I will skip this step directly. After the Spring+SpringMVC environment has been successfully built, we need to integrate SDR first by adding the following dependencies:

Redis.clientsjedis2.9.0org.springframework.dataspring-data-redisRELEASEorg.apache.commonscommons-pool2RELEASE

Then create the redis.properties file under the resources directory as the configuration file for redis, as follows:

Redis.host=192.168.248.128redis.port=6379redis.maxIdle=300redis.maxTotal=600redis.maxWait=1000redis.testOnBorrow=true

In the configuration file of spring, add the following bean:

Well, after configuring redisTemplate in Spring, we can then inject redisTemplate into the Dao layer and use it.

Next, let's first create the entity class User, and note that User must be serializable:

Public class User implements Serializable {private String username;private String password;private String id;//get/set ellipsis}

Then add and obtain the data in the Dao layer, as follows:

@ Repositorypublic class HelloDao {@ AutowiredRedisTemplate redisTemplate;public void set (String key, String value) {ValueOperations ops = redisTemplate.opsForValue (); ops.set (key, value);} public String get (String key) {ValueOperations ops = redisTemplate.opsForValue (); return ops.get (key). ToString ();} public void setuser (User user) {ValueOperations ops = redisTemplate.opsForValue (); ops.set (user.getId (), user);} public User getuser (String id) {ValueOperations ops = redisTemplate.opsForValue (); User user = ops.get (id) System.out.println (user); return user;}}

The introduction to Redistemplate in the official documentation of SDR allows you to call methods such as ValueOperations and ListOperations through Redistemplate, which are high-level wrappers of Redis commands, respectively. But ValueOperations and so on, these commands are eventually translated into RedisCallback to be executed. In other words, stronger functionality can be achieved by using RedisCallback.

Finally, I would like to show you my Service and Controller, as follows:

@ Servicepublic class HelloService {@ AutowiredHelloDao helloDao;public void set (String key, String value) {helloDao.set (key,value);} public String get (String key) {return helloDao.get (key);} public void setuser (User user) {helloDao.setuser (user);} public String getuser (String id) {String s = helloDao.getuser (id). ToString (); return;} Controller:@Controllerpublic class HelloController {@ AutowiredHelloService helloService @ RequestMapping ("/ set") @ ResponseBodypublic void set (String key, String value) {helloService.set (key, value);} @ RequestMapping ("/ get") @ ResponseBodypublic String get (String key) {return helloService.get (key);} @ RequestMapping ("/ setuser") @ ResponseBodypublic void setUser () {User user = new User (); user.setId ("1"); user.setUsername ("Shenzhen"); user.setPassword ("sang"); helloService.setuser (user) } @ RequestMapping (value = "/ getuser", produces = "text/html;charset=UTF-8") @ ResponseBodypublic String getUser () {return helloService.getuser ("1");}} this is the end of the introduction to the usage of Spring Data Redis. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report