mybatis结合generator进行分页插件PluginAdapter开发
mybatis结合generator生成的代码没有分页的功能,可以尝试自己继承分页插件PluginAdapter,进行开发,实现自己的分页插件这样generator生产的代码 带分页功能了。继承PluginAdapter类,实现相关方法。MyBatis MySQL自动生成带分页插件。
·
mybatis结合generator生成的代码没有分页的功能,可以尝试自己继承分页插件PluginAdapter,进行开发,实现自己的分页插件这样generator生产的代码 带分页功能了。
MyBatis MySQL自动生成带分页插件
继承PluginAdapter类,实现相关方法
/**
* MyBatis MySQL自动生成带分页插件
*
* @author
*/
public class MysqlPaginationPlugin extends PluginAdapter {
@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
addLimit(topLevelClass, introspectedTable, "limitStart");
addLimit(topLevelClass, introspectedTable, "limitSize");
return super.modelExampleClassGenerated(topLevelClass, introspectedTable);
}
/**
* 为selectByExample添加limitStart和limitSize
*/
@Override
public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
XmlElement isNotNullElement = new XmlElement("if");
isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitSize >= 0"));
isNotNullElement.addElement(new TextElement("limit #{limitStart} , #{limitSize}"));
element.addElement(isNotNullElement);
return super.sqlMapSelectByExampleWithoutBLOBsElementGenerated(element, introspectedTable);
}
/**
* 为selectByExampleWithBLOBs添加limitStart和limitSize
*/
@Override
public boolean sqlMapSelectByExampleWithBLOBsElementGenerated(XmlElement element,
IntrospectedTable introspectedTable) {
XmlElement isNotNullElement = new XmlElement("if");
isNotNullElement.addAttribute(new Attribute("test", "limitStart != null and limitSize >= 0"));
isNotNullElement.addElement(new TextElement("limit #{limitStart} , #{limitSize}"));
element.addElement(isNotNullElement);
return super.sqlMapSelectByExampleWithBLOBsElementGenerated(element, introspectedTable);
}
private void addLimit(TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String name) {
CommentGenerator commentGenerator = context.getCommentGenerator();
/**
* 创建类成员变量 如protected Integer limitStart;
*/
Field field = new Field();
field.setVisibility(JavaVisibility.PROTECTED);
field.setType(PrimitiveTypeWrapper.getIntegerInstance());
field.setName(name);
commentGenerator.addFieldComment(field, introspectedTable);
topLevelClass.addField(field);
/**
* 首字母大写
*/
char c = name.charAt(0);
String camel = Character.toUpperCase(c) + name.substring(1);
/**
* 添加Setter方法
*/
Method method = new Method();
method.setVisibility(JavaVisibility.PUBLIC);
method.setName("set" + camel);
method.addParameter(new Parameter(PrimitiveTypeWrapper.getIntegerInstance(), name));
StringBuilder sb = new StringBuilder();
sb.append("this.");
sb.append(name);
sb.append(" = ");
sb.append(name);
sb.append(";");
/**
* 如 this.limitStart = limitStart;
*/
method.addBodyLine(sb.toString());
commentGenerator.addGeneralMethodComment(method, introspectedTable);
topLevelClass.addMethod(method);
/**
* 添加Getter Method 直接调用AbstractJavaGenerator的getGetter方法
*/
Method getterMethod = AbstractJavaGenerator.getGetter(field);
commentGenerator.addGeneralMethodComment(getterMethod, introspectedTable);
topLevelClass.addMethod(getterMethod);
}
public boolean validate(List<String> warnings) {
return true;
}
public static void generate() {
String config = PaginationMysqlPlugin.class.getClassLoader().getResource("generatorConfig.xml").getFile();
String[] arg = { "-configfile", config, "-overwrite" };
ShellRunner.main(arg);
}
public static void main(String[] args) {
generate();
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)