In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how SpringBoot integrates SpringDataRedis. 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.
SpringBoot integrates SpringDataRedis
1. Create a project to add dependencies
creates the SpringBoot project and adds the following dependencies:
Org.springframework.boot
Spring-boot-starter-web
Org.springframework.boot
Spring-boot-starter-data-redis
Org.springframework.boot
Spring-boot-starter-test
Test
Redis.clients
Jedis
2.9.0
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
two。 Set up the application.properties file
Spring.redis.jedis.pool.max-idle=10
Spring.redis.jedis.pool.min-idle=5
Spring.redis.pool.max-total=20
Spring.redis.hostName=192.168.88.120
Spring.redis.port=6379
one
two
three
four
five
3. Add a configuration class for Redis
adds Redis's java configuration class to set relevant information.
/ * *
* @ program: springboot-redis-demo
* @ description: configuration class of Redis
* @ author: Bobo Roast Duck
* @ create: 2019-05-20 23:40
, /
@ Configuration
Public class RedisConfig {
/ * *
* 1. Create a JedisPoolConfig object. Complete some link pool configuration in this object
* @ ConfigurationProperties: creates an entity with the same prefix.
, /
@ Bean
@ ConfigurationProperties (prefix= "spring.redis.pool")
Public JedisPoolConfig jedisPoolConfig () {
JedisPoolConfig config = new JedisPoolConfig ()
/ * / maximum free time
Config.setMaxIdle (10)
/ / minimum free time
Config.setMinIdle (5)
/ / maximum number of links
Config.setMaxTotal (20);
System.out.println ("default:" + config.getMaxIdle ())
System.out.println ("default:" + config.getMinIdle ())
System.out.println ("default:" + config.getMaxTotal ())
Return config
}
/ * *
* 2. Create JedisConnectionFactory: configure redis link information
, /
@ Bean
@ ConfigurationProperties (prefix= "spring.redis")
Public JedisConnectionFactory jedisConnectionFactory (JedisPoolConfig config) {
System.out.println ("configuration completed:" + config.getMaxIdle ())
System.out.println ("configuration completed:" + config.getMinIdle ())
System.out.println ("configuration completed:" + config.getMaxTotal ())
JedisConnectionFactory factory = new JedisConnectionFactory ()
/ / the configuration object of the associated link pool
Factory.setPoolConfig (config)
/ / configure the information of the link Redis
/ / Host address
/ * factory.setHostName ("192.168.70.128")
/ / Port
Factory.setPort (6379);
Return factory
}
/ * *
* 3. Create RedisTemplate: the method used to perform Redis operations
, /
@ Bean
Public RedisTemplate redisTemplate (JedisConnectionFactory factory) {
RedisTemplate template = new RedisTemplate ()
/ / Association
Template.setConnectionFactory (factory)
/ / set the serializer for key
Template.setKeySerializer (new StringRedisSerializer ())
/ / set the serializer for value
Template.setValueSerializer (new StringRedisSerializer ())
Return template
}
}
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
thirty-six
thirty-seven
thirty-eight
thirty-nine
forty
forty-one
forty-two
forty-three
forty-four
forty-five
forty-six
forty-seven
forty-eight
forty-nine
fifty
fifty-one
fifty-two
fifty-three
fifty-four
fifty-five
fifty-six
fifty-seven
fifty-eight
fifty-nine
sixty
sixty-one
sixty-two
sixty-three
sixty-four
sixty-five
sixty-six
sixty-seven
4. Add pojo
/ * *
* @ program: springboot-redis-demo
* @ description: Users
* @ author: Bobo Roast Duck
* @ create: 2019-05-20 23:47
, /
Public class Users implements Serializable {
Private Integer id
Private String name
Private Integer age
Public Integer getId () {
Return id
}
Public void setId (Integer 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
}
@ Override
Public String toString () {
Return "Users [id=" + id + ", name=" + name + ", age=" + age + "]"
}
}
one
two
three
four
five
six
seven
eight
nine
ten
eleven
twelve
thirteen
fourteen
fifteen
sixteen
seventeen
eighteen
nineteen
twenty
twenty-one
twenty-two
twenty-three
twenty-four
twenty-five
twenty-six
twenty-seven
twenty-eight
twenty-nine
thirty
thirty-one
thirty-two
thirty-three
thirty-four
thirty-five
5. Unit testing
@ RunWith (SpringRunner.class)
@ SpringBootTest (classes = SpringbootRedisDemoApplication.class)
Public class SpringbootRedisDemoApplicationTests {
@ Autowired
Private RedisTemplate redisTemplate
/ * *
* add a string
, /
@ Test
Public void testSet () {
This.redisTemplate.opsForValue () .set ("key", "bobokaoya...")
}
/ * *
* get a string
, /
@ Test
Public void testGet () {
String value = (String) this.redisTemplate.opsForValue () .get ("key")
System.out.println (value)
}
/ * *
* add Users object
, /
@ Test
Public void testSetUesrs () {
Users users = new Users ()
Users.setAge (20)
Users.setName ("Zhang Sanfeng")
Users.setId (1)
/ / reset the serializer
This.redisTemplate.setValueSerializer (new JdkSerializationRedisSerializer ())
This.redisTemplate.opsForValue () .set (users, users)
}
/ * *
* fetch Users object
, /
@ Test
Public void testGetUsers () {
/ / reset the serializer
This.redisTemplate.setValueSerializer (new JdkSerializationRedisSerializer ())
Users users = (Users) this.redisTemplate.opsForValue () .get ("users")
System.out.println (users)
}
/ * *
* storing Users objects based on JSON format
, /
@ Test
Public void testSetUsersUseJSON () {
Users users = new Users ()
Users.setAge (20)
Users.setName ("Li Sifeng")
Users.setId (1)
This.redisTemplate.setValueSerializer (new Jackson2JsonRedisSerializer (Users.class))
This.redisTemplate.opsForValue () .set (users_json, users)
}
/ * *
* fetching Users objects based on JSON format
, /
@ Test
Public void testGetUseJSON () {
This.redisTemplate.setValueSerializer (new Jackson2JsonRedisSerializer (Users.class))
Users users = (Users) this.redisTemplate.opsForValue () .get ("users_json")
System.out.println (users)
}
}
These are all the contents of the article "how SpringBoot integrates SpringDataRedis". 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.