开发者

ibatis / mybatis caching within a restful webservice

开发者 https://www.devze.com 2023-04-08 13:50 出处:网络
I am using mybatis withi开发者_如何学JAVAn a Jax-RS (Jersey) restful web app. So automatically, I dont have a session or state management.

I am using mybatis withi开发者_如何学JAVAn a Jax-RS (Jersey) restful web app. So automatically, I dont have a session or state management.

Question is how can I use the caching features of mybatis ?


Caching in MyBatis is very straight forward. Per the documentation (pg 42 of the user manual http://mybatis.googlecode.com/svn/trunk/doc/en/MyBatis-3-User-Guide.pdf)

By default, there is no caching enabled, except for local session caching, which improves performance and is required to resolve circular dependencies. To enable a second level of caching, you simply need to add one line to your SQL Mapping file: MyBatis 3 - User Guide 6 June 2011 43

<cache/>

Literally that’s it.

The common pitfalls I had while doing this:

On the mapper you add the cache element to; if you have dependent entities, make sure to explicitly flush the cache when required. Even though flushing is already done for you on insert, update, delete for the elements in the mappings you have set the cache element, sometimes you have to flush a cache due to updates/deletes/etc defined in different xml mappings.

Basically, when you're thinking about your caching, you should ask yourself, "When this entity is changed, do I want it to flush a cache for an entity in a different mapping?" If the answer is yes, use cache-ref element as opposed to just cache.

Ex from page 45 of the doc:

<cache-ref namespace=”com.someone.application.data.SomeMapper”/>
0

精彩评论

暂无评论...
验证码 换一张
取 消