IBatis (MyBatis): Handling Constructors
Join the DZone community and get the full member experience.
Join For Freethis tutorial will walk you through how to setup ibatis ( mybatis ) in a simple java project and will present an example using a class constructor with arguments.
pre-requisites
for this tutorial i am using:
ide: eclipse (you can use your favorite one)
database: mysql
libs/jars: mybatis , mysql conector and junit (for testing)
this is how your project should look like:
sample database
please run the script into your database before getting started with the project implementation. you will find the script (with dummy data) inside the sql folder.
1 – pojo
i represented the beans here with a uml model, but you can download the complete source code in the end of this article.
as you can see, we do not have a default construtor in this class, only a constructor with some arguments. some persistence frameworks requires a default constructor, but you have to be careful with your business logic.
let’s say there are a couple of mandatory atributes in your class. nothing is going to stop you if you do not set these atributes and try to insert, update on the database. and you problably will get an exception for that.
to prevent this kind of situation, you can create a constructor with the mandatory arguments. and ibatis/ mybatis can handle this situation for you, because you do not need to create a default constructor.
2 – blog mapper – xml
while properties will work for most data transfer object (dto) type classes, and likely most of yourdomain model, there may be some cases where you want to use immutable classes. often tables thatcontain reference or lookup data that rarely or never changes is suited to immutable classes.constructor injection allows you to set values on a class upon instantiation, without exposing publicmethods. mybatis also supports private properties and private javabeans properties to achieve this, butsome people prefer constructor injection. the constructor element enables this.in order to inject the results into the constructor, mybatis needs to identify the constructor by the type of its parameters. java has no way to introspect (or reflect) on parameter names. so when creating a constructor element, ensure that the arguments are in order, and that the data types are specified.
if you need, you can still map another attributes, such as association, collection in your resultmap.
following is the blog.xml file:
3 - blog mapper – annotations
we did the configuration in xml, now let’s try to use annotations to do the same thing we did using xml.
this is the code for blogmapper.java:
let’s take a look at the @constructorargs and @arg annotations:
@constructorargs
collects a group of results to be passed to a result object constructor.
attributes: value, which is an array of args.
xml equivalent: constructor
@arg
a single constructor argument that is part of a constructorargs collection.
attributes: id, column, javatype, jdbctype, typehandler.
the id attribute is a boolean value that identifies the property to be used for comparisons, similar to the <idarg> xml element.
xml equivalent: idarg, arg
we do not need to use the @results annotation because the other attributes has the same name, so mybatis knows how to map them.
download
if you want to download the complete sample project, you can get it from my github account: https://github.com/loiane/ibatis-constructor
if you want to download the zip file of the project, just click on download:
there are more articles about ibatis to come. stay tooned!
happy coding!
once again though, some may find this external definition of maps somewhat tedious. thereforethere’s an alternative syntax for those that prefer a more concise mapping style.
Opinions expressed by DZone contributors are their own.
Comments