When using transation on Mybatis, there are 3 ways. Container managed transaction - JEE engine manages transaction User managed transaction - transaction is controlled by user source code Spring managed transaction - transaction is controlled by Spring Transaction policy I usually use Spring managed or container managed transaction. But sometimes, I need to control transaction … Continue reading A tip on using user transaction on Mybatis
Category: Mybatis
Handling big data on Mybatis
Sometimes, we need to query big data from database. In that case, OutOfMemory error or frequent full GC can happen. Suppose that there are several million records and we need to query all data to process some logic. Example sql map Example dao When this method is called, OutOfMemory Exception can happen. The following heap … Continue reading Handling big data on Mybatis
Parameterizing SQL on Mybatis
When parameterizing SQL on Mybatis, there are 2 ways. Using #{PARAM} Using ${PARAM} When #{} is declared, it is transformed into PreparedStatement's parameter. However, ${} is String substitution inside Mybatis. The following example shows the difference. For the above example, the first sql parameterizes table name with #{} and the second sql with ${} When … Continue reading Parameterizing SQL on Mybatis
Implementing dynamic SQL on Mybatis
Mybatis supports dynamic sql by using if, choose or when tag inside mapper xml. (More on dynamic sql tags) Sometimes, however, such tags are not enough for some requirements. Some times ago, I had to build select statements based on random table name. In that case, all parts of select statement were really dynamic. But … Continue reading Implementing dynamic SQL on Mybatis
Some points to consider when using Mybatis cache
When using Mybatis cache, there are some points to consider. Local session cache lifecycle Local session cache is enabled with default option Cache boundary is for all the queries within a SqlSession An item is cached when querying a record The item is reused when querying with the same parameter Cache is flushed when insert/update/delete … Continue reading Some points to consider when using Mybatis cache
Using Ehcache instead of Mybatis internal cache
Ehcache is a very robust in-memory cache. In production, Ehcache could be better solution for Mybatis cache (More on Mybatis internal cache) How to use Ehcache as Mybatis cache (2nd level cache) You can set Mybatis 2nd level cache (Global cache) as 3rd party. (You can not change local session cache type) Import required library … Continue reading Using Ehcache instead of Mybatis internal cache
Integrating Mybatis with Spring
Spring framework does not support Mybatis internally. Instead, mybatis-spring project supports integration of Mybatis with Spring. This article is based on Spring 4.3.16 and Mybatis 3.4.6. (As of writing this article, Spring 5.1.x still don't support Mybatis internally) You can download the sample project here. Test table for the sample Test Mybatis mapper xml Required … Continue reading Integrating Mybatis with Spring
Mybatis cache mechanism
Mybatis supports internal cache. There are 2 kinds of cache. The first is session cache. (First level cache) The second is global cache. (Second level cache) Session cache (First level cache) Session cache is managed within a SqlSession. (SqlSession is the wrapper of a database connection) It can not be accessed from another SqlSession. And … Continue reading Mybatis cache mechanism