Mybatis Generator实现分页功能

众所周知,Mybatis本身没有提供基于数据库方言的分页功能,而是基于JDBC的游标分页,很容易出现性能问题。网上有很多分页的解决方案,不外乎是基于Mybatis本机的插件机制,通过拦截Sql做分页。但是在像Oracle这样的数据库上,拦截器生成的Sql语句没有变量绑定,而且每次语句的都要去拦截,感觉有点浪费性能。

Mybatis Generator是Mybatis的代码生成工具,可以生成大部分的查询语句。

本文提供的分页解决方案是新增Mybatis Generator插件,在用Mybatis Generator生成Mybatis代码时,直接生成基于数据库方言的Sql语句,解决Oralce等数据库的变量绑定,且无需使用Mybatis拦截器去拦截语句判断分页。

一、编写Mybatis Generator Dialect插件

/**

  1.  * Copyright (C) 2011 Tgwoo Inc.  
  2.  * http://www.tgwoo.com/  
  3.  */  
  4. package com.tgwoo.core.dao.plugin;  
  5.   
  6. import java.util.List;  
  7.   
  8. import org.mybatis.generator.api.CommentGenerator;  
  9. import org.mybatis.generator.api.IntrospectedTable;  
  10. import org.mybatis.generator.api.PluginAdapter;  
  11. import org.mybatis.generator.api.dom.java.Field;  
  12. import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;  
  13. import org.mybatis.generator.api.dom.java.JavaVisibility;  
  14. import org.mybatis.generator.api.dom.java.Method;  
  15. import org.mybatis.generator.api.dom.java.Parameter;  
  16. import org.mybatis.generator.api.dom.java.TopLevelClass;  
  17. import org.mybatis.generator.api.dom.xml.Attribute;  
  18. import org.mybatis.generator.api.dom.xml.Document;  
  19. import org.mybatis.generator.api.dom.xml.TextElement;  
  20. import org.mybatis.generator.api.dom.xml.XmlElement;  
  21.   
  22. /** 
  23.  * @author pan.wei 
  24.  * @date 2011-11-30 下午08:36:11 
  25.  */  
  26. public class OraclePaginationPlugin extends PluginAdapter {  
  27.   
  28.     @Override  
  29.     public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,  
  30.             IntrospectedTable introspectedTable) {  
  31.         // add field, getter, setter for limit clause  
  32.         addPage(topLevelClass, introspectedTable, "page");  
  33.         return super.modelExampleClassGenerated(topLevelClass,  
  34.                 introspectedTable);  
  35.     }  
  36.   
  37.     @Override  
  38.     public boolean sqlMapDocumentGenerated(Document document,  
  39.             IntrospectedTable introspectedTable) {  
  40.         XmlElement parentElement = document.getRootElement();  
  41.   
  42.         // 产生分页语句前半部分  
  43.         XmlElement paginationPrefixElement = new XmlElement("sql");  
  44.         paginationPrefixElement.addAttribute(new Attribute("id",  
  45.                 "OracleDialectPrefix"));  
  46.         XmlElement pageStart = new XmlElement("if");  
  47.         pageStart.addAttribute(new Attribute("test""page != null"));  
  48.         pageStart.addElement(new TextElement(  
  49.                 "select * from ( select row_.*, rownum rownum_ from ( "));  
  50.         paginationPrefixElement.addElement(pageStart);  
  51.         parentElement.addElement(paginationPrefixElement);  
  52.   
  53.         // 产生分页语句后半部分  
  54.         XmlElement paginationSuffixElement = new XmlElement("sql");  
  55.         paginationSuffixElement.addAttribute(new Attribute("id",  
  56.                 "OracleDialectSuffix"));  
  57.         XmlElement pageEnd = new XmlElement("if");  
  58.         pageEnd.addAttribute(new Attribute("test""page != null"));  
  59.         pageEnd.addElement(new TextElement(  
  60.                 "<![CDATA[ ) row_ ) where rownum_ > #{page.begin} and rownum_ <= #{page.end} ]]>"));  
  61.         paginationSuffixElement.addElement(pageEnd);  
  62.         parentElement.addElement(paginationSuffixElement);  
  63.   
  64.         return super.sqlMapDocumentGenerated(document, introspectedTable);  
  65.     }  
  66.   
  67.     @Override  
  68.     public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(  
  69.             XmlElement element, IntrospectedTable introspectedTable) {  
  70.   
  71.         XmlElement pageStart = new XmlElement("include"); //$NON-NLS-1$     
  72.         pageStart.addAttribute(new Attribute("refid""OracleDialectPrefix"));  
  73.         element.getElements().add(0, pageStart);  
  74.   
  75.         XmlElement isNotNullElement = new XmlElement("include"); //$NON-NLS-1$     
  76.         isNotNullElement.addAttribute(new Attribute("refid",  
  77.                 "OracleDialectSuffix"));  
  78.         element.getElements().add(isNotNullElement);  
  79.   
  80.         return super.sqlMapUpdateByExampleWithoutBLOBsElementGenerated(element,  
  81.                 introspectedTable);  
  82.     }  
  83.   
  84.     /** 
  85.      * @param topLevelClass 
  86.      * @param introspectedTable 
  87.      * @param name 
  88.      */  
  89.     private void addPage(TopLevelClass topLevelClass,  
  90.             IntrospectedTable introspectedTable, String name) {  
  91.         topLevelClass.addImportedType(new FullyQualifiedJavaType(  
  92.                 "com.tgwoo.core.dao.pojo.Page"));  
  93.         CommentGenerator commentGenerator = context.getCommentGenerator();  
  94.         Field field = new Field();  
  95.         field.setVisibility(JavaVisibility.PROTECTED);  
  96.         field.setType(new FullyQualifiedJavaType("com.tgwoo.core.dao.pojo.Page"));  
  97.         field.setName(name);  
  98.         commentGenerator.addFieldComment(field, introspectedTable);  
  99.         topLevelClass.addField(field);  
  100.         char c = name.charAt(0);  
  101.         String camel = Character.toUpperCase(c) + name.substring(1);  
  102.         Method method = new Method();  
  103.         method.setVisibility(JavaVisibility.PUBLIC);  
  104.         method.setName("set" + camel);  
  105.         method.addParameter(new Parameter(new FullyQualifiedJavaType(  
  106.                 "com.tgwoo.core.dao.pojo.Page"), name));  
  107.         method.addBodyLine("this." + name + "=" + name + ";");  
  108.         commentGenerator.addGeneralMethodComment(method, introspectedTable);  
  109.         topLevelClass.addMethod(method);  
  110.         method = new Method();  
  111.         method.setVisibility(JavaVisibility.PUBLIC);  
  112.         method.setReturnType(new FullyQualifiedJavaType(  
  113.                 "com.tgwoo.core.dao.pojo.Page"));  
  114.         method.setName("get" + camel);  
  115.         method.addBodyLine("return " + name + ";");  
  116.         commentGenerator.addGeneralMethodComment(method, introspectedTable);  
  117.         topLevelClass.addMethod(method);  
  118.     }  
  119.   
  120.     /** 
  121.      * This plugin is always valid - no properties are required 
  122.      */  
  123.     public boolean validate(List<String> warnings) {  
  124.         return true;  
  125.     }  
  126. }  

二、增加插件到Mybatis Generator配置文件中

<?xml version="1.0" encoding="UTF-8" ?>

  1. <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >  
  2. <generatorConfiguration >  
  3.     <classPathEntry location="E:\work\eclipseWorkspace\myEclipse\CTSPMTS\WebRoot\WEB-INF\lib\ojdbc14.jar" />  
  4.       
  5.     <context id="oracle" >  
  6.         <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"></plugin>  
  7.         <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>  
  8.         <!-- Pagination -->  
  9.         <plugin type="com.tgwoo.core.dao.plugin.OraclePaginationPlugin"></plugin>  
  10.           
  11.         <commentGenerator>    
  12.            <property name="suppressDate" value="true" />    
  13.             <property name="suppressAllComments" value="true" />  
  14.         </commentGenerator>    
  15.           
  16.         <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.0.2:1521:ctspmt" userId="ctspmt" password="ctspmt123" />  
  17.         <javaModelGenerator targetPackage="com.tgwoo.ctspmt.model" targetProject="CTSPMTS/src" />  
  18.         <sqlMapGenerator targetPackage="com.tgwoo.ctspmt.data" targetProject="CTSPMTS/src" />  
  19.         <javaClientGenerator targetPackage="com.tgwoo.ctspmt.data" targetProject="CTSPMTS/src" type="XMLMAPPER" /><!-- 
  20.        
  21.         <table schema="ctspmt" tableName="mt_e_interface_log"/> 
  22.         --><!--  
  23.         <
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值