Dear Readers,
Below is a very simple example of using mybatis annotations. Personally I support annotations based approach over XML, just for a simple reason that everything is inline and debugging and maintenance found to be easier than with other approaches.
Most annotations are self explanatory. MyBatis is really good in this aspect. We can guess what the annotations functionality is by just having a quick glance.
Well as usual it is up to your requirements and team preferences to choose one over the other.
Below is a simple interface to deal with Address data and it handles CRUD operations.
01 | package com.examples.dao; |
03 | import org.apache.ibatis.annotations.CacheNamespace; |
04 | import org.apache.ibatis.annotations.Delete; |
05 | import org.apache.ibatis.annotations.Insert; |
06 | import org.apache.ibatis.annotations.Options; |
07 | import org.apache.ibatis.annotations.Select; |
08 | import org.apache.ibatis.annotations.Update; |
10 | import com.examples.vo.AddressVO; |
12 | @CacheNamespace (implementation=org.mybatis.caches.ehcache.EhcacheCache. class ) |
13 | public interface AddressDAO{ |
15 | String GET_ADDRESS_BY_ID = "SELECT * FROM vw_address WHERE id = #{addressId}" ; |
16 | String INSERT_ADDRESS = "INSERT into address (building,street,location,town,postCode,countyId,countryId,notes,createdOn,createdBy,active) VALUES (#{building},#{street},#{location},#{town},#{postCode},#{countyId},#{countryId},#{notes},sysdate(),#{createdBy},1)" ; |
17 | String UPDATE_ADDRESS = "UPDATE address set building=#{building},countyId=#{countyId}, street=#{street},location=#{location},town=#{town},postCode=#{postCode},notes=#{notes},modifiedOn=sysdate(),modifiedBy=#{modifiedBy},countryId=#{countryId} where id= #{id}" ; |
18 | String DELETE_ADDRESS = "DELETE from address WHERE id = #{addressId}" ; |
20 | @Select (GET_ADDRESS_BY_ID) |
21 | @Options (useCache= true ) |
22 | public AddressVO doSelectAddress( long addressId) throws Exception; |
24 | @Insert (INSERT_ADDRESS) |
25 | @Options (useGeneratedKeys = true , keyProperty = "id" , flushCache= true ) |
26 | public int doCreateAddress(AddressVO address) throws Exception; |
28 | @Update (UPDATE_ADDRESS) |
29 | @Options (flushCache= true ) |
30 | public int doUpdateAddress(AddressVO address) throws Exception; |
32 | @Delete (DELETE_ADDRESS) |
33 | @Options (flushCache= true ) |
34 | public int doDeleteAddress( long addressId) throws Exception; |
I am using EHCACHE for cache implementations (Have a look at http://www.terracotta.org/ehcache/ to find out more about EHCACHE). Therefore I’ve set the Cache Namespace by using the annotation and indicated that implementation class should be EhcacheCache.class.
1 | @CacheNamespace (implementation=org.mybatis.caches.ehcache.EhcacheCache. class ) |
In the subsequent lines I declared set of static variables holding SQL queries.
Rest all code is straight forward except the flushCache attribute within @Options annotation
1 | @Options (flushCache= true ) |
flushCache=true : Indicates that Cache must be flushed after the operation is successfully carried out. In the above example cache is flushed right after @Insert, @Update & @Delete operations.
flushCache=false : Is the opposite as you might have expected by now. By default this option is set to false. You do not need to set this to false explicitly.
All other annotations and attributes are self explanatory I trust. If you find it hard to figure out then please do not hesitate to drop me an email. I will respond to your queries as soon as I can.
Did you like this? Share it: