Mybatis-generator plug-in extension to generate paging capabilities that support multiple databases

Source: Internet
Author: User
Tags generator

Background: In the process of using mybatis, considering the integration framework in the late use of the process, it may be necessary to be compatible with a variety of databases, under this precondition, the completion of the general crud function of the writing, this paper first consider the different database for paging function unified operation For example, the paging of MySQL database is the use of the Limit keyword, and the paging of Oracle database is the use of the ROWNUM keyword;

Demo git address section: http://git.oschina.net/alexgaoyh/MutiModule-parent/commit/8e83bb3e5eca7e711a88927905343e499cac359e

The main attribute used in this example is Databaseidprovider , which is used by this property to generate SQL paging statements that support multiple databases:

The Mutidatasourcepaginationplugin class file is the extended Mybatis-generator plug-in, which is used to complete the paging Logic code section under different database support during the process of generating the code file .

In this example, you only need to care about the generated Mutidatabasemapper.xml:

<select id= "Selectbyexample" resultmap= "Baseresultmap" parametertype= " Com.alexgaoyh.MutiModule.persist.mutiDatabase.MutiDatabaseExample "><include refid=" Oracledialectprefix "/ >select<if test= "distinct" >distinct</if><include refid= "base_column_list"/>from mutidatabase Mutidatabase<if test= "_parameter! = null" ><include refid= "example_where_clause"/></if><if test = "Orderbyclause! = null" >order by ${orderbyclause}</if><include refid= "Oracledialectsuffix"/>< Include refid= "Mysqldialect"/></select><sql id= "Oracledialectprefix" ><if test= "page! = null and _ DatabaseId = = ' Oracle ' ">select * FROM (select row_.*, RowNum rownum_ from (</if></sql><sql id=" Oracledi Alectsuffix "><if test=" page! = null and _databaseid = = ' Oracle ' "> <! [cdata[) Row_) where Rownum_ > #{page.begin} and Rownum_ <= #{page.end}]]></if></sql><sql id= "M Ysqldialect "><if test="Page! = null and _databaseid = = ' MySQL '" >limit #{page.begin}, #{page.length}</if></sql> 




thus, by Databaseidprovider, the introduction of different SQL statements is completed;

Some links:

Http://stackoverflow.com/questions/29139092/mybatis-multi-datasource-configuration-issue

Https://mybatis.github.io/spring/factorybean.html

Mybatis-generator plug-in extension to generate paging capabilities that support multiple databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.