All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mingsoft.base.dao.IBaseDao.xml Maven / Gradle / Ivy

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="net.mingsoft.base.dao.IBaseDao">

    <!-- mysql根据sql动态查询开始 -->
    <select id="queryBySQL" resultType="Map" databaseId="mysql">
        select
        <if test="fields !=null">
            <foreach item="field" collection="fields" open=""
                     separator="," close="">
                ${field}
            </foreach>
        </if>
        <if test="fields ==null">
            *
        </if>
        from ${table}
        <where>
            1=1
            <foreach item="item" index="key" collection="wheres" open="AND"
                     separator="AND" close=""> ${key} = #{item}
            </foreach>
            <include refid="net.mingsoft.base.dao.IBaseDao.sqlWhere"></include>
        </where>
        <if test="orderBy !=null">
            order by ${orderBy}
            <if test="order != null">
                <if test="order =='desc'">
                    desc
                </if>
                <if test="order =='asc'">
                    asc
                </if>
            </if>
            <if test="order==null">desc</if>
        </if>
        <if test="begin != null">
            limit ${begin}
            <if test="end !=null ">
                ,${end}
            </if>
        </if>
    </select>
    <!-- mysql根据sql动态查询结束 -->

    <!-- oracle根据sql动态查询开始 -->
    <select id="queryBySQL" resultType="Map" databaseId="oracle">
        select
        <if test="fields !=null">
            <foreach item="field" collection="fields" open=""
                     separator="," close="">
                ${field}
            </foreach>
        </if>
        <if test="fields ==null">
            *
        </if>
        from ${table}
        <where>
            1=1
            <foreach item="item" index="key" collection="wheres" close="">
                <if test="item != null">
                    AND ${key} = #{item}
                </if>
            </foreach>
            <include refid="net.mingsoft.base.dao.IBaseDao.sqlWhere"></include>
        </where>
        <if test="orderBy !=null">
            order by ${orderBy}
            <if test="order != null">
                <if test="order =='desc'">
                    desc
                </if>
                <if test="order =='asc'">
                    asc
                </if>
            </if>
            <if test="order==null">desc</if>
        </if>
    </select>
    <!-- oracle根据sql动态查询结束 -->

    <!-- sqlserver根据sql动态查询开始 -->
    <select id="queryBySQL" resultType="Map" databaseId="sqlserver">
        select
            <if test="end !=null ">
                TOP ${end}
            </if>
            <if test="fields !=null">
                <foreach item="field" collection="fields" open=""
                         separator="," close="">
                    ${field}
                </foreach>
            </if>
            <if test="fields ==null">
                *
            </if>,ROW_NUMBER ( ) OVER ( ORDER BY RAND( ) ) PAGE_ROW_NUMBER from ${table}
        <where>
            1=1
            <foreach item="item" index="key" collection="wheres" open="AND"
                     separator="AND" close=""> ${key} = #{item}
            </foreach>
            <include refid="net.mingsoft.base.dao.IBaseDao.sqlWhere"></include>
        </where>
        <if test="begin != null">
            and PAGE_ROW_NUMBER > ${begin}
        </if>
        <if test="orderBy !=null">
            order by ${orderBy}
            <if test="order != null">
                <if test="order =='desc'">
                    desc
                </if>
                <if test="order =='asc'">
                    asc
                </if>
            </if>
            <if test="order==null">desc</if>
        </if>
    </select>
    <!-- sqlserver根据sql动态查询结束 -->

    <!-- 根据sql动态查询开始 -->
    <select id="countBySQL" resultType="int">
        select count(*)
        from ${table}
        <where>
            <if test="wheres != null">
                <foreach item="item" index="key" collection="wheres" open=""
                         separator="AND" close=""> ${key} = ${item}
                </foreach>
            </if>
            <if test="wheres == null">
                del = 0
            </if>
            <include refid="net.mingsoft.base.dao.IBaseDao.sqlWhere"></include>
        </where>
    </select>
    <!-- 根据sql动态查询结束 -->

    <!-- mysql或SqlServer根据sql动态更新开始 -->
    <update id="updateBySQL">
        update ${table} set
        <foreach item="field" index="name" collection="fields" open=""
                 separator="," close="">
            ${name}=#{field}
        </foreach>
        <where>
            <foreach item="item" index="key" collection="wheres" open=""
                     separator="AND" close=""> ${key} = ${item}
            </foreach>
        </where>
    </update>
    <!-- mysql或SqlServer根据sql动态更新结束 -->

    <!-- 根据sql动态删除开始 -->
    <update id="deleteBySQL">
        delete from ${table} where
        <foreach item="item" index="key" collection="wheres" open=""
                 separator="AND" close=""> ${key} = #{item}
        </foreach>
    </update>
    <!-- 根据sql动态删除结束 -->

    <!-- 根据sql动态新增开始 -->
    <insert id="insertBySQL">
        insert into ${table}
        <foreach item="field" index="key" collection="fields" open="("
                 separator="," close=")">${key}
        </foreach>
        values
        <foreach item="field" index="key" collection="fields" open="("
                 separator="," close=")">#{field}
        </foreach>
    </insert>
    <!-- 根据sql动态新增结束 -->

    <!-- 根据sql动态创建表开始 -->
    <sql id="createTables">
		 CONSTRAINT fk_${table}_id FOREIGN KEY (basicId) REFERENCES basic (basic_id) ON DELETE CASCADE
	</sql>
    <update id="createTable" statementType="STATEMENT" databaseId="mysql">
        CREATE TABLE ${table} (
        basicId int(11) NOT NULL,
        PRIMARY KEY (basicId),
        <include refid="createTables"></include>
        ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
    </update>
    <!-- 根据sql动态创建表结束 -->
    <!-- 根据sql动态更新表开始 -->
    <sql id="alterTableSql" databaseId="mysql">
        ALTER TABLE ${table} change ${fileds.fieldOldName}
        ${fileds.fieldName} ${fileds.fieldType}
        <if test="fileds.default !=null and fileds.default !=''">default '${fileds.default}'</if>
    </sql>

    <sql id="alterTableSql" databaseId="sqlserver">
        ALTER TABLE ${table} change ${fileds.fieldOldName}
        ${fileds.fieldName} ${fileds.fieldType}
        <if test="fileds.default !=null and fileds.default !=''">default '${fileds.default}'</if>
    </sql>

    <sql id="alterTableSql" databaseId="oracle">
        ALTER TABLE ${table} RENAME TO ${fileds.fieldOldName}
        ${fileds.fieldName} ${fileds.fieldType}
        <if test="fileds.default !=null and fileds.default !=''">default '${fileds.default}'</if>
    </sql>

    <sql id="alterTableAddType">${fileds.fieldType}</sql>
    <update id="alterTable" statementType="STATEMENT">
        <choose>
            <when test="type=='add'">
                ALTER TABLE ${table} add ${fileds.fieldName}
                <include refid="alterTableAddType"></include>
                <if test="fileds.default !=null and fileds.default != ''">default '${fileds.default}'</if>
            </when>
            <when test="type=='modify'">
                <include refid="alterTableSql"></include>
            </when>
            <when test="type=='drop'">
                ALTER TABLE ${table} drop column ${fileds.fieldName}
            </when>
        </choose>

    </update>
    <!-- 根据sql动态更新表结束 alterTable-->

    <!-- 根据sql动态删除表开始 -->
    <update id="dropTable" statementType="STATEMENT">
		DROP TABLE ${table}
	</update>
    <!-- 根据sql动态删除表结束 -->

    <!-- 导入sql语句 -->
    <select id="excuteSql" parameterType="String" statementType="STATEMENT" resultType="java.util.Map">
		${sql}
	</select>
    <!--导入sql语句 -->

    <sql id="sqlWhere" databaseId="mysql">
        <if test="sqlWhereList != null">
        <foreach collection="sqlWhereList" item="item" index="index"
                 open="and( " separator=" " close=" )">

            <if test="item.el == 'eq'">
                <choose>
                    <when test="item.multiple != null and item.multiple == true">
                        FIND_IN_SET(#{item.value}, ${item.field})>0
                    </when>
                    <otherwise>
                        ${item.field} = #{item.value}
                    </otherwise>
                </choose>
            </if>
            <if test="item.el == 'gt'">
                <choose>
                    <when test="item.type=='time'||item.type=='date'">
                        <if test="item.type=='time'">
                            date_format(${item.field},'%T') &gt; date_format(#{item.value},'%T')
                        </if>
                        <if test="item.type=='date'">
                            date_format(${item.field},'%Y-%m-%d %H:%i:%s') &gt; date_format(#{item.value},'%Y-%m-%d %H:%i:%s')
                        </if>
                    </when>
                    <otherwise>
                        ${item.field} &gt; #{item.value}
                    </otherwise>
                </choose>
            </if>
            <if test="item.el == 'gte'">
                ${item.field} &gt;= #{item.value}
            </if>
            <if test="item.el == 'lt'">
                <choose>
                    <when test="item.type=='time'||item.type=='date'">
                        <if test="item.type=='time'">
                            date_format(${item.field},'%T') &lt; date_format(#{item.value},'%T')
                        </if>
                        <if test="item.type=='date'">
                            date_format(${item.field},'%Y-%m-%d %H:%i:%s') &lt; date_format(#{item.value},'%Y-%m-%d %H:%i:%s')
                        </if>
                    </when>
                    <otherwise>
                        ${item.field} &lt; #{item.value}
                    </otherwise>
                </choose>
            </if>
            <if test="item.el == 'lte'">
                ${item.field} &lt;= #{item.value}
            </if>
            <if test="item.el == 'like'">
                ${item.field} like CONCAT(CONCAT('%',#{item.value}),'%')
            </if>
            <if test="item.el == 'likeLeft'">
                ${item.field} like CONCAT(CONCAT(#{item.value}),'%')
            </if>
            <if test="item.el == 'likeRight'">
                ${item.field} like CONCAT('%',#{item.value})
            </if>
            <if test="item.el == 'in'">
                ${item.field} in (${item.value})
            </if>
            <if test="item.el == 'range'">
                <if test="item.type=='time'">
                    date_format(${item.field},'%T') BETWEEN date_format(#{item.value[0]},'%T') AND date_format(#{item.value[1]},'%T')
                </if>
                <if test="item.type=='date'">
                    date_format(${item.field},'%Y-%m-%d %H:%i:%s') BETWEEN date_format(#{item.value[0]},'%Y-%m-%d %H:%i:%s') AND date_format(#{item.value[1]},'%Y-%m-%d %H:%i:%s')
                </if>
            </if>

            <if test="index != (sqlWhereList.size() - 1)">
                <choose>
                    <!--防注入-->
                    <when test="item.action == 'and' or item.action == 'or'">
                        ${item.action}
                    </when>
                    <otherwise>
                        and
                    </otherwise>
                </choose>
            </if>
        </foreach>
        </if>
    </sql>

    <sql id="sqlWhere" databaseId="sqlserver">
        <if test="sqlWhereList != null">
            <foreach collection="sqlWhereList" item="item" index="index"
                     open="and( " separator=" " close=" )">

                <if test="item.el == 'eq'">
                    <choose>
                        <when test="item.multiple != null and item.multiple == true">
                            FIND_IN_SET(#{item.value}, ${item.field})>0
                        </when>
                        <otherwise>
                            ${item.field} = #{item.value}
                        </otherwise>
                    </choose>
                </if>
                <if test="item.el == 'gt'">
                    <choose>
                        <when test="item.type=='time'||item.type=='date'">
                            <if test="item.type=='time'">
                                date_format(${item.field},'%T') &gt; date_format(#{item.value},'%T')
                            </if>
                            <if test="item.type=='date'">
                                date_format(${item.field},'%Y-%m-%d %H:%i:%s') &gt; date_format(#{item.value},'%Y-%m-%d %H:%i:%s')
                            </if>
                        </when>
                        <otherwise>
                            ${item.field} &gt; #{item.value}
                        </otherwise>
                    </choose>
                </if>
                <if test="item.el == 'gte'">
                    ${item.field} &gt;= #{item.value}
                </if>
                <if test="item.el == 'lt'">
                    <choose>
                        <when test="item.type=='time'||item.type=='date'">
                            <if test="item.type=='time'">
                                date_format(${item.field},'%T') &lt; date_format(#{item.value},'%T')
                            </if>
                            <if test="item.type=='date'">
                                date_format(${item.field},'%Y-%m-%d %H:%i:%s') &lt; date_format(#{item.value},'%Y-%m-%d %H:%i:%s')
                            </if>
                        </when>
                        <otherwise>
                            ${item.field} &lt; #{item.value}
                        </otherwise>
                    </choose>
                </if>
                <if test="item.el == 'lte'">
                    ${item.field} &lt;= #{item.value}
                </if>
                <if test="item.el == 'like'">
                    ${item.field} like CONCAT(CONCAT('%',#{item.value}),'%')
                </if>
                <if test="item.el == 'likeLeft'">
                    ${item.field} like CONCAT(CONCAT(#{item.value}),'%')
                </if>
                <if test="item.el == 'likeRight'">
                    ${item.field} like CONCAT('%',#{item.value})
                </if>
                <if test="item.el == 'in'">
                    ${item.field} in (${item.value})
                </if>
                <if test="item.el == 'range'">
                    <if test="item.type=='time'">
                        date_format(${item.field},'%T') BETWEEN date_format(#{item.value[0]},'%T') AND date_format(#{item.value[1]},'%T')
                    </if>
                    <if test="item.type=='date'">
                        date_format(${item.field},'%Y-%m-%d %H:%i:%s') BETWEEN date_format(#{item.value[0]},'%Y-%m-%d %H:%i:%s') AND date_format(#{item.value[1]},'%Y-%m-%d %H:%i:%s')
                    </if>
                </if>

                <if test="index != (sqlWhereList.size() - 1)">
                    <choose>
                        <!--防注入-->
                        <when test="item.action == 'and' or item.action == 'or'">
                            ${item.action}
                        </when>
                        <otherwise>
                            and
                        </otherwise>
                    </choose>
                </if>
            </foreach>
        </if>
    </sql>

    <sql id="sqlWhere" databaseId="oracle">
        <if test="sqlWhereList != null">
            <foreach collection="sqlWhereList" item="item" index="index"
                     open="and( " separator=" " close=" )">
                <if test="item.value != null">
                    <if test="item.el == 'eq'">
                        <choose>
                            <when test="item.multiple != null and item.multiple == true">
                                FIND_IN_SET('${item.value}', ${item.field})>0
                            </when>
                            <otherwise>
                                ${item.field} = '${item.value}'
                            </otherwise>
                        </choose>
                    </if>
                    <if test="item.el == 'gt'">
                        <choose>
                            <when test="item.type=='time'||item.type=='date'">
                                <if test="item.type=='time'">
                                    to_date(${item.field},'%T') &gt; to_date(${item.value},'%T')
                                </if>
                                <if test="item.type=='date'">
                                    ${item.field} &gt; to_date('${item.value}','yyyy-mm-dd hh24:mi:ss')
                                </if>
                            </when>
                            <otherwise>
                                ${item.field} &gt; #{item.value}
                            </otherwise>
                        </choose>
                    </if>
                    <if test="item.el == 'gte'">
                        ${item.field} &gt;= #{item.value}
                    </if>
                    <if test="item.el == 'lt'">
                        <choose>
                            <when test="item.type=='time'||item.type=='date'">
                                <if test="item.type=='time'">
                                    to_date(${item.field},'%T') &lt; to_date(${item.value},'%T')
                                </if>
                                <if test="item.type=='date'">
                                    ${item.field} &lt; to_date('${item.value}','yyyy-mm-dd hh24:mi:ss')
                                </if>
                            </when>
                            <otherwise>
                                ${item.field} &lt; #{item.value}
                            </otherwise>
                        </choose>
                    </if>
                    <if test="item.el == 'lte'">
                        ${item.field} &lt;= #{item.value}
                    </if>
                    <if test="item.el == 'like'">
                        ${item.field} like CONCAT(CONCAT('%',#{item.value}),'%')
                    </if>
                    <if test="item.el == 'likeLeft'">
                        ${item.field} like CONCAT(CONCAT(#{item.value}),'%')
                    </if>
                    <if test="item.el == 'likeRight'">
                        ${item.field} like CONCAT('%',#{item.value})
                    </if>
                    <if test="item.el == 'in'">
                        ${item.field} in (${item.value})
                    </if>
                    <if test="item.el == 'range'">
                        <if test="item.type=='time'">
                            to_date(${item.field},'%T') BETWEEN to_date(#{item.value[0]},'%T') AND to_date(#{item.value[1]},'%T')
                        </if>
                        <if test="item.type=='date'">
                            ${item.field} BETWEEN to_date('${item.value[0]}','yyyy-mm-dd hh24:mi:ss') AND to_date('${item.value[1]}','yyyy-mm-dd hh24:mi:ss')
                        </if>
                    </if>

                    <if test="index != (sqlWhereList.size() - 1)">
                        <choose>
                            <!--防注入-->
                            <when test="item.action == 'and' or item.action == 'or'">
                                ${item.action}
                            </when>
                            <otherwise>
                                and
                            </otherwise>
                        </choose>
                    </if>
                </if>
            </foreach>
        </if>
    </sql>


</mapper>




© 2015 - 2024 Weber Informatics LLC | Privacy Policy