Ibator 1.2.x provides plugin architecture.
I like the plugin system and write some custom ones for my daily use.
The first plugin adds Spring's @Repository annotation to the DAO implementation class.
import java.util.List;
import org.apache.ibatis.ibator.api.IbatorPluginAdapter;
import org.apache.ibatis.ibator.api.IntrospectedTable;
import org.apache.ibatis.ibator.api.dom.java.FullyQualifiedJavaType;
import org.apache.ibatis.ibator.api.dom.java.TopLevelClass;
public class AddRepositoryAnnotationPlugin extends IbatorPluginAdapter
{
public boolean validate(List<string> warnings)
{
return true;
}
@Override
public boolean daoImplementationGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
{
topLevelClass.addAnnotation("@Repository");
topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.stereotype.Repository"));
return true;
}
}
And the second plugin adds the following method to the DAO implementation to allow Spring framework to inject SqlMapClient at runtime.
@Autowired
public void createSqlMapClient(SqlMapClient sqlMapClient)
{
this.setSqlMapClient(sqlMapClient);
}
Here's the plugin implementation.
import java.util.List;
import org.apache.ibatis.ibator.api.IbatorPluginAdapter;
import org.apache.ibatis.ibator.api.IntrospectedTable;
import org.apache.ibatis.ibator.api.dom.java.FullyQualifiedJavaType;
import org.apache.ibatis.ibator.api.dom.java.JavaVisibility;
import org.apache.ibatis.ibator.api.dom.java.Method;
import org.apache.ibatis.ibator.api.dom.java.Parameter;
import org.apache.ibatis.ibator.api.dom.java.TopLevelClass;
public class AddAutowiredSqlMapClientPlugin extends IbatorPluginAdapter
{
public boolean validate(List<String> warnings)
{
return true;
}
@Override
public boolean daoImplementationGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable)
{
Method method = new Method();
method.addAnnotation("@Autowired");
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("createSqlMapClient");
Parameter param = new Parameter(new FullyQualifiedJavaType("com.ibatis.sqlmap.client.SqlMapClient"), "sqlMapClient");
method.addParameter(param);
method.addBodyLine("this.setSqlMapClient(sqlMapClient);");
ibatorContext.getCommentGenerator().addGeneralMethodComment(method, introspectedTable.getFullyQualifiedTable());
topLevelClass.addMethod(method);
topLevelClass.addImportedType(new FullyQualifiedJavaType("com.ibatis.sqlmap.client.SqlMapClient"));
topLevelClass.addImportedType(new FullyQualifiedJavaType("org.springframework.beans.factory.annotation.Autowired"));
return true;
}
}
Hi
ReplyDeleteThanks for posting this, very useful.
Some advice on how to setup the plugin with ibator might also be useful. I had to rebuild the ibator jar from source. Maybe there's an easier way...
Glad it helps :-)
ReplyDeleteUsing ant to run Ibator, you can add your plugin classes to the classpath.