当前位置:Gxlcms > 数据库问题 > Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数 (转)

时间:2021-07-01 10:21:17 帮助过:34人阅读

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

一、编写Mybatis Generator Dialect插件

 

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

 

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

 

Xml代码  技术分享
  1. >  
  2. <generatorConfiguration >  
  3. <classPathEntry location="E:\work\eclipseWorkspace\myEclipse\CTSPMTS\WebRoot\WEB-INF\lib\ojdbc14.jar" />  
  4.     <context id="oracle" >  
  5. <plugin type="org.mybatis.generator.plugins.CaseInsensitiveLikePlugin"></plugin>  
  6. <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>  
  7. <!-- Pagination -->  
  8. <plugin type="com.tgwoo.core.dao.plugin.OraclePaginationPlugin"></plugin>  
  9.         <commentGenerator>    
  10. <property name="suppressDate" value="true" />    
  11. <property name="suppressAllComments" value="true" />  
  12. </commentGenerator>    
  13.         <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@192.168.0.2:1521:ctspmt" userId="ctspmt" password="ctspmt123" />  
  14. <javaModelGenerator targetPackage="com.tgwoo.ctspmt.model" targetProject="CTSPMTS/src" />  
  15. <sqlMapGenerator targetPackage="com.tgwoo.ctspmt.data" targetProject="CTSPMTS/src" />  
  16. <javaClientGenerator targetPackage="com.tgwoo.ctspmt.data" targetProject="CTSPMTS/src" type="XMLMAPPER" /><!-- 
  17.        
  18.         <table schema="ctspmt" tableName="mt_e_interface_log"/> 
  19.         --><!--  
  20. <table schema="ctspmt" tableName="mt_e_msg" />  
  21. <table schema="ctspmt" tableName="mt_e_msg_log" />  
  22. <table schema="ctspmt" tableName="mt_e_msg_receiver" />  
  23. <table schema="ctspmt" tableName="st_e_org" />  
  24. <table schema="ctspmt" tableName="st_e_role" />  
  25. <table schema="ctspmt" tableName="st_e_user" />  
  26. <table schema="ctspmt" tableName="mt_e_user_msg_conf" />  
  27. <table schema="ctspmt" tableName="mt_j_user_device" />  
  28. <table schema="ctspmt" tableName="st_j_user_role" />  
  29. <table schema="ctspmt" tableName="ST_E_UNIQUE_KEY" />       
  30. ><table schema="ctspmt" tableName="mt_v_msg_item" />  
  31. </context>  
  32. </generatorConfiguration>  

 

 

三、示例

 

Java代码  技术分享
  1. 2011 Tgwoo Inc.  
  2. //www.tgwoo.com/  
  3. package com.tgwoo.ctspmt.test;  
  4. import com.tgwoo.core.config.SpringBeanProxy;  
  5. import com.tgwoo.core.dao.pojo.Page;  
  6. import com.tgwoo.ctspmt.data.MtVMsgItemMapper;  
  7. import com.tgwoo.ctspmt.model.MtVMsgItemExample;  
  8. /** 
  9.  * @author pan.wei 
  10.  * @date 2011-11-25 下午01:26:17 
  11.  */  
  12. public class Test {  
  13. /** 
  14.      * @param args 
  15.      */  
  16. public static void main(String[] args) {  
  17. //get spring mapper instance   
  18.                 MtVMsgItemMapper.class);  
  19. new MtVMsgItemExample();  
  20. new Page(0, 10);  
  21.         ex.createCriteria().andMsgCodeEqualTo("222");  
  22. // set count,up to you  
  23.         int row = mapper.selectByExample(ex).size();  
  24. "============row:" + row + "================");  
  25.   
  26. }  

 

四、分页类

 

Java代码  技术分享
  1. package com.tgwoo.core.dao.pojo;  
  2. /** 
  3.  * @author pan.wei 
  4.  * @date 2011-12-1 上午11:36:12 
  5.  */  
  6. public class Page {  
  7. // 分页查询开始记录位置  
  8. private int begin;  
  9. // 分页查看下结束位置  
  10. private int end;  
  11. // 每页显示记录数  
  12. private int length;  
  13. // 查询结果总记录数  
  14. private int count;  
  15. // 当前页码  
  16. private int current;  
  17. // 总共页数  
  18. private int total;  
  19.     public Page() {  
  20.   
  21. /** 
  22.      * 构造函数 
  23.      *  
  24.      * @param begin 
  25.      * @param length 
  26.      */  

人气教程排行