September 28, 2010

MyBatis CRUD Detector Tool

MyBatis CRUD Detector is a java tool (a small Java library) which displays which of your objects have all CRUD methods mapped out in mapper files.

I've put this tool together because I ran into the problem of having tons of POJOs, each of them with various mapped queries in the mybatis mapper files, but no easy way to see if I had implemented all CRUD methods for each class (without manually searching through the xml files)

Here is how it works

Example:

You have the following model classes in package com.application.name:

com.application.name.Address
com.application.name.Contact

and the following mapped statements in MyBatis mapper files (xml files):

getAddress
createAddress
deleteAddress
getContact
createContact

Now, you want to know if you have mapped all 4 CRUD queries in MyBatis for each of your classes. In other words you want to make sure each of your classes has a Select, Insert, Update and Delete mapped query defined in the MyBatis mapper files.

All you have to do with CRUDDetector is this:

//pass in a SqlSessionFactory object and the package name your objects are in
CRUDDetector cd = new CRUDDetector(sf, "com.application.name");
cd.displaySummary();

and you will get an output indicating which CRUD queries you have mapped for each class.

Example output

Address 
[EXISTS]  select query for class Address mapped to selectAddress 
[EXISTS]  insert query for class Address mapped to insertAddress 
[MISSING] update query for class Address mapped to updateAddress 
[EXISTS]  delete query for class Address mapped to deleteAddress 

Contact 
[EXISTS]  select query for class Contact mapped to selectContact 
[EXISTS]  insert query for class Contact mapped to insertContact 
[MISSING] update query for class Contact mapped to updateContact 
[MISSING] delete query for class Contact mapped to deleteContact 

The example above may be trivial, but imagine you have 50 objects in your package and you are working your way through implementing mapped statements for all of them.

Additional Info

In order for CRUDDetector to run, it assumes the following:

If your java class is called UserAccount, it will check for the existence of the following methods in the mybatis mapped xml files:

<select id="selectUserAccount" ... />
<insert id="insertUserAccount" ... />
<update id="updateUserAccount" ... />
<delete id="deleteUserAccount" ... />

You can overwrite the prefix of your mapped queries however. If your queries have a different naming convention like this:

<select id="findUserAccount" ... />
<insert id="createUserAccount" ... />
<update id="editUserAccount" ... />
<delete id="deleteUserAccount" ... />

You can use an additional hashmap parameter to the CRUDDetector to let it know of your own naming conventions.

HashMap queryPrefixes = new HashMap();
queryPrefixes.put(CRUDDetector.SELECT_QUERY_PREFIX,"find");
queryPrefixes.put(CRUDDetector.INSERT_QUERY_PREFIX,"create");
queryPrefixes.put(CRUDDetector.UPDATE_QUERY_PREFIX,"edit");
queryPrefixes.put(CRUDDetector.DELETE_QUERY_PREFIX,"delete");

CRUDDetector cd = new CRUDDetector(sf, "com.application.name",queryPrefixes);
cd.displaySummary();

Example output

Address 
[EXISTS]  select query for class Address mapped to findAddress 
[EXISTS]  insert query for class Address mapped to createAddress 
[MISSING] update query for class Address mapped to editAddress 
[EXISTS]  delete query for class Address mapped to deleteAddress 

Contact 
[EXISTS]  select query for class Contact mapped to findContact 
[EXISTS]  insert query for class Contact mapped to createContact 
[MISSING] update query for class Contact mapped to editContact 
[MISSING] delete query for class Contact mapped to deleteContact 

Finally, the package name that is passed in for CRUDDetector to look into should only contain your MyBatis related POJOs.

Jar Dependencies

CRUDDetector has very few dependencies

Download CRUDDetector

mybatis-crud-detector-1.0.jar

If anyone finds this tool useful I would love to hear from you. Leave a comment below and let me know how to improve it or what functionality it should have.