Redis and SSM integration method (mybatis L2 cache)

SSM + redis integration

The SSM framework has been built before, and code copying is no longer done here.

Redis is mainly used for the secondary cache of mybatis. All select in the mybats mapping file will refresh the existing cache. If it does not exist, a new cache will be created, and all insert and update operations will update the cache.

The benefits of redis are also obvious, which can improve the data access performance of the system. This section only shows the integration methods and effects. The articles on redis cluster, load balancing and session sharing will be supplemented later.

Let's start the integration work:

Start redis server in the background first (the redis.conf file needs to be changed for background startup and remote connection to Linux service), and start the command ". / SRC / redis server. / redis. Conf"

I recommend a visual tool "redis Desktop Manager", which is developed under Windows system. It needs to remotely connect to redis under Linux and open the port under Linux (the specific method is to modify the / etc / sysconfig / iptables file and add the external port development command).

After the above operations are completed, the remote connection is successful, as shown in the figure:

There are no cache records yet. Let's enter the code phase. First, in POM Add redis jar package to XML

pom. After the XML is written, you need to add two configuration files: redis.xml properties

The fields are also well understood, and then add the configuration file: spring redis xml

After the configuration file is written, start writing java code:

JedisClusterFactory. java

RedisCache. java

RedisCacheTransfer. java

SerializeUtil. java

After everything is ready, you need to modify the mapping file

To make the mybaits cache effective, you need to turn on the L2 cache as shown in the figure above. The configuration file also needs to be on the web Loading validation in XML

When everything is ready, start the service

After successful startup, click the employee form to trigger the method of querying all employees. The first query statement can see that mybatis has printed the query statement and updated a cache in the redis server

We empty the console and click the query employee button again to execute the query method. We can see that the query statement is not executed, which proves that the second query takes values directly from the cache and does not connect to MySQL for query.

summary

The above is the integration method of redis and SSM (mybatis secondary cache) introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>