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