PAP 快速开发框架:mybatis

    背景: 在使用mybatis的过程中,考虑到整合的框架在后期使用的过程中,有可能是需要兼容到多种数据库的,在这种前提条件下,完成通用CRUD功能的编写,本文前期先考虑到不同数据库针对分页功能的统一操作;例如mysql数据库的分页是limit关键字的使用, oracle数据库的分页是rownum关键字的使用;

    demo git地址部分: http://git.oschina.net/alexgaoyh/MutiModule-parent/commit/8e83bb3e5eca7e711a88927905343e499cac359e

    此例中主要使用的属性为  databaseIdProvider , 通过这个属性的使用,来生成支持多种数据库的sql分页语句:

    MutiDatasourcePaginationPlugin  类文件为扩展的mybatis-generator 插件,用来在生成代码文件的过程中,完成不同数据库支持下的分页逻辑代码部分;

    此例中只需要关心生成的 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="OracleDialectSuffix">
        <if test="page != null and _databaseId == 'oracle'">
      <![CDATA[ ) row_ ) where rownum_ > #{page.begin} and rownum_ <= #{page.end} ]]>
        </if>
    </sql>
    <sql id="MysqlDialect">
        <if test="page != null and _databaseId == 'mysql'">
            limit #{page.begin} , #{page.length}
        </if>
    </sql>

这样,通过 databaseIdProvider  的不同,就完成不同sql语句的引入操作;

    部分链接:

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

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

评论

暂无评论