Advanced Database Persistence for Java Examples & Reference Manual |
It may happen your project was already using MyBatis. If this is the case, then your project already has a set of DAOs/VOs Java classes and MyBatis mappers files. It may also happen you want to write your own MyBatis mappers in special cases, where you think very high fine tuning is necessary.
In any case, HotRod can leverage these mappers and combine them in the resulting MyBatis configuration file.
Any DAOs/VOs manually written outside HotRod are fully available by default to all the mappers. That is, the Java classes do not require any special treatment, as long as the are available in the classpath.
The mappers are a little bit different. HotRod can pick existing
mappers—without touching them—if they are placed in the
custom
subdirectory of the
relative-dir
attribute specified in the configuration file for the
<mapper>
tag. Any mapper file found in this directory will be added to the list
of mapper files in the resulting MyBatis configuration file.
The explanation above is correct, though a little bit dry. Let's try an example.
<?xml version="1.0"?> <!DOCTYPE hotrod SYSTEM "hotrod.dtd"> <hotrod> <generators> <mybatis> ... <mappers gen-base-dir="mybatis-mappers" relative-dir="persistence" /> .... </mybatis> </generators> <table name="book" /> </hotrod>
Let's assume the directory structure of the project has a
mybatis-mappers/persistence
directory with a
custom
directory inside. Something like:
+- project/ +- mybatis-mappers/ +- persistence/ +- custom/ +- manga-queries.xml +- fiction-queries.xml
HotRod's generation will produce a mapper for the
BOOK
table. It will also include both existing mapper files
manga-queries.xml
, and
fiction-queries.xml
, since they are in the directory it inspects for existing MyBatis
mapper files. The resulting MyBatis configuration file will look like:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments> ... </environments> <mappers> <mapper resource="persistence/primitives/primitives-book.xml" /> <mapper resource="persistence/custom/manga-queries.xml" /> <mapper resource="persistence/custom/fiction-queries.xml" /> </mappers> </configuration>