Redis of Java (spring data redis integration)
1. Integrate with spring data redis
POM used in the project xml:
Except for the log part, there is only one spring core and spring data redis
Project file directory structure:
applicationContext. xml:
1. Context: the property placeholder tag is used to import the properties file. To replace variables such as ${redis. Maxidle}.
2. Context: component scan is to be displayed on COM x.redis. The classes under Dao can use the annotation injection method of spring.
3. In fact, we just need to match the jedispoolconfig. The next step is the encapsulation of spring. So you can see directly from the implementation of userdaoimpl.
redis. properties:
UserDAOImpl:
1. Spring uses a template similar to the following code to encapsulate the Dao layer.
2. Redistemplate is just spring's encapsulation of redis.
other:
User:
Test code:
2. No spring data redis integration
Personally, I think this integration has greater flexibility and can complete the task more clearly.
pom. xml:
Directory structure:
data-source. xml
1. Context: Property placeholder and context: component scan have been explained earlier.
2. A shardedjedispool is configured, and there is a jedispool in jdeis. The difference between the two is:
One is in the form of fragmentation, which can connect active and standby redis servers, and the other is single. Detailed follow-up study
3. Since spring data redis encapsulation is not used, you should encapsulate one yourself
Redisdatasource: define three methods
Implement redisdatasource:
1. Inject the configured shardedjedispool. The functions of these three methods are as follows:
The second layer of encapsulation: redisclienttemplate. The example implements value setting and value taking. Finally, the code provides the implementation of all commands.
The code is just another call to jedis's method in the nature of mapping. It uses a broken as the identifier to determine the way to return resources.
The main purpose of this layer is to make the calls on the upper layer no longer care about the acquisition and return of links in the pool.
Test code:
Attached are redisclienttemplate implementations:
There are too many redisclienttemplate codes. Please attach the download address: http://xiazai.jb51.net/201701/yuanma/RedisClientTemplate_jb51.rar
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.