Mybatis(http://blog.mybatis.org/) is one fantastic ORM tool for Java, I like it very much because of its lightweight and smart way to do ORM. There is also one mybatis generatorIn official release, generator helps generating standard java code as you project's DAO layer.
This project is plugin suite for mybatis generator. What is mybatis generator, please see http://www.mybatis.org/generator/. The generator is handy to use, but there are still some not handy parts:
- Subquery I want to execute sql like this, select c1, c2, ..., cn from table where c1 in (select cc1 as c1 from table 1 where cc2 = 1), there is no generated code source for this. Using offical generator, you will write code like this:
YourOtherDomainExample e=new YourOtherDomainExample(); e.createCriteria().andCc2Equals(1); List yourOtherDomainList = YourOtherDomainMapper.selectByExample(e);
//then constructe one list of id list idList=new ArrayList(yourOtherDomainList.size()); for(YourOtherDomain d:yourOtherDomainList) idList.add(d.getCc1());
//use IdInList query YourDomainExample e1=new YourDomainExample(); e1.createCriteria().andC1In(idList); YourDomainMapper.selectByExample(e1);
<plugin type="net.sourceforge.jweb.mybatis.generator.plugins.SubqueryCriteriaPlugin"/> DO it
add this plugin to your generatorConfig.xml, then you will getYourDomainExample{ ... protected abstract static class GeneratedCriteria { ... Criteria has andGenericSubquery(String subquery){ ... } ... } } Usage: YourDomainExample e=new YourDomainExample(); e.createCriteria().andGenericSubquery("c1 in (select cc1 as c1 from table 1 where cc2 = 10)"); mapper.selectByExample(e);
-
SelectOneByExample, default generatored code does not have select single one Object by example,
<plugin type="net.sourceforge.jweb.mybatis.generator.plugins.SelectOneByExamplePlugin"/> DO it
In your DAO/Mapper code, you will see:/** * This method was generated by MyBatis Generator. * This method corresponds to the database table domain * * @mbggenerated */ Domain selectOneByExample(DomainExample example);
-
Advanced Clause Example, default mybatis generator sql statement style is: "select columns from table where (a and b and c ...) OR (d and e and f ... ) OR (... and ...)", in some case, this is really inconvenient, for example, you want you sql as follow:
select columns from table where (a or b) and c, to suit mybatis generator, you must use its equivalent form -
select columns from table where (a and c) or (b and c), if this is complex, it is hard to transform it.<plugin type="net.sourceforge.jweb.mybatis.generator.plugins.AdvancedWhereClausePlugin"/> DO it
You will seeYourDomainExample{ ... protected abstract static class GeneratedCriteria { ... public Criteria andDomainClumnxxxx(.....) { ...... return (Criteria) this; } public Criteria orDomainClumnxxxx(.....) { ...... return (Criteria) this; } ... } }
-
Some annotations plugin
net.sourceforge.jweb.mybatis.generator.plugins.OptionsAnnotationPlugin net.sourceforge.jweb.mybatis.generator.plugins.CacheAnnotationPlugin net.sourceforge.jweb.mybatis.generator.plugins.ModelBuilderPlugin
<repository> <id>Sonatype OSS Snapshot Repository</id> <url>http://oss.sonatype.org/content/repositories/snapshots</url> </repository> <repository> <id>Sonatype OSS Release Repository</id> <url>https://oss.sonatype.org/service/local/staging/deploy/maven2</url> </repository> <dependency> <groupId>com.github.alexmao86</groupId> <artifactId>jweb-maven-plugin</artifactId> <version>1.0</version> </dependency>
remove warnings enhance PagePlugin to add method parameter type argument with domain type