generator mysql插件_mybatis generator插件系列--分页插件

1、首先定义分页插件

MysqlPagePlugin.java

package com.demo.mybatis.plugin;

import org.mybatis.generator.api.CommentGenerator;

import org.mybatis.generator.api.IntrospectedTable;

import org.mybatis.generator.api.PluginAdapter;

import org.mybatis.generator.api.dom.java.*;

import org.mybatis.generator.api.dom.xml.Attribute;

import org.mybatis.generator.api.dom.xml.TextElement;

import org.mybatis.generator.api.dom.xml.XmlElement;

import java.util.List;

/**

*

 
 

* add pagination using mysql limit.

* This class is only used in ibator code generator.

*

*/

/**

* mysql 分页生成插件

*/

public class MysqlPagePlugin extends PluginAdapter {

/**

* 添加 分页 开始行数 和结束行数 属性

*/

@Override

public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,

IntrospectedTable introspectedTable) {

// add field, getter, setter for limit clause

addProperty(topLevelClass, introspectedTable, "limitStart", FullyQualifiedJavaType.getIntInstance());

addProperty(topLevelClass, introspectedTable, "limitEnd", FullyQualifiedJavaType.getIntInstance());

addProperty(topLevelClass, introspectedTable, "groupByClause", FullyQualifiedJavaType.getStringInstance());

return super.modelExampleClassGenerated(topLevelClass, introspectedTable);

}

/**

* 添加 映射 文件配置 limit 的配置

*/

@Override

public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(

XmlElement element, IntrospectedTable introspectedTable) {

//XmlElement isParameterPresenteElemen = (XmlElement) element.getElements();

//设置 if 判断 节点

XmlElement limitElement = new XmlElement("if"); //$NON-NLS-1$

//给 节点添加 条件运算符

limitElement.addAttribute(new Attribute("test", "limitEnd > 0")); //$NON-NLS-1$ //$NON-NLS-2$

//如果条件成立 就进行分页查询

limitElement.addElement(new TextElement(

"limit #{limitStart,jdbcType=INTEGER} , #{limitEnd,jdbcType=INTEGER}"));

//添加节点到 配置文件中

element.addElement(limitElement);

XmlElement groupbyElement = new XmlElement("if"); //$NON-NLS-1$

//给 节点添加 条件运算符

groupbyElement.addAttribute(new Attribute("test", "groupByClause != null")); //$NON-NLS-1$ //$NON-NLS-2$

//如果条件成立 就进行分页查询

groupbyElement.addElement(new TextElement(

"group by ${groupByClause}"));

//添加节点到 配置文件中

element.addElement(groupbyElement);

return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element, introspectedTable);

}

/**

* 给对应的实体 实体添加 属性字段

*/

private void addProperty(TopLevelClass topLevelClass,

IntrospectedTable introspectedTable, String name, FullyQualifiedJavaType fullyQualifiedJavaType) {

CommentGenerator commentGenerator = context.getCommentGenerator();

Field field = new Field();

field.setVisibility(JavaVisibility.PROTECTED);

field.setType(fullyQualifiedJavaType);

field.setName(name);

//field.setInitializationString("-1");

commentGenerator.addFieldComment(field, introspectedTable);

topLevelClass.addField(field);

char c = name.charAt(0);

String camel = Character.toUpperCase(c) + name.substring(1);

Method method = new Method();

method.setVisibility(JavaVisibility.PUBLIC);

method.setName("set" + camel);

method.addParameter(new Parameter(fullyQualifiedJavaType, name));

method.addBodyLine("this." + name + "=" + name + ";");

commentGenerator.addGeneralMethodComment(method, introspectedTable);

topLevelClass.addMethod(method);

method = new Method();

method.setVisibility(JavaVisibility.PUBLIC);

method.setReturnType(fullyQualifiedJavaType);

method.setName("get" + camel);

method.addBodyLine("return " + name + ";");

commentGenerator.addGeneralMethodComment(method, introspectedTable);

topLevelClass.addMethod(method);

}

/**

* This plugin is always valid - no properties are required

*/

public boolean validate(List warnings) {

return true;

}

//public static void generate() {

//String config = MysqlPagePlugin.class.getClassLoader().getResource("generatorConfig-w5Log.xml").getFile();

//String[] arg = { "-configfile", config, "-overwrite" };

//ShellRunner.main(arg);

//}

//public static void main(String[] args) {

//generate();

//}

}

2、然后为mybatisgenerator配置插件

connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF8"

userId="root" password="123456">

targetProject="ilovey.biz/src/main/java"/>

targetProject="ilovey.biz/src/main/resources"/>

targetProject="ilovey.biz/src/main/java" type="XMLMAPPER"/>

3、使用示例

@Repository

public class UsUserInfoDao {

@Autowired

private UsUserInfoMapper usUserMapper;

/**

* 分页查询用户信息

* @param kinCode 用户类型

* @param page 分页参数(page为自定义的对象)

* @return

*/

public List listUserInfo(String kinCode, Page page) {

UsUserInfoExample example = new UsUserInfoExample();

example.createCriteria().andKindCodeEqualTo(kinCode);

//分页

example.setLimitStart(page.limitStart());

example.setLimitEnd(page.limitEnd());

//排序

example.setOrderByClause("create_time desc");

//查询总数

page.setTotalCount(usUserMapper.countByExample(example));

return usUserMapper.selectByExample(example);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值