Caching with mybatis is quite simple.

Follow the example from this website won’t work because they have some changes in packaging.

to use ehcache/hazelcast with mybatis just download the correct bundle from this link


put the following line into mybatis XML Mapper file.

<mapper namespace=”Contact”>
    <cache type=”org.mybatis.caches.ehcache.EhcacheCache”/>


with the default configuration Ehcache will work in a single node only if you want to use distributed cache function you’ll need to setup Terracotta Cluster.


<mapper namespace=”Contact”>
    <cache type=”org.mybatis.caches.hazelcast.HazelcastCache”/>


with hazelcast you’ll get a full function distributed cluster. However, it depends on your configuration which can be included internally (JAR) or externally (Class path).


From what I notice there is no cache effect when the returned result is only one row (performance degraded), however, with multiple rows there is a significant performance gained.