Browse Source

新增动态生成insert,update 功能

周浩 9 years ago
parent
commit
ff003ce0e5

+ 180 - 96
hsweb-web-dao-impl-mybatis/src/main/resources/org/hsweb/web/dao/impl/mybatis/mapper/oracle/basic/BasicMapper.xml

@@ -4,130 +4,214 @@
         "http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">
 <mapper namespace="BasicMapper">
     <!--通用查询条件-->
-    <sql id="selectCondition">
-            <!--动态生成查询条件-->
-            <foreach item="item" index="index" collection="$fields">
-                <if test="#this['term.'+item]!=null">
-                    <choose>
-                        <when test="$fieldsType[item] == 'date' ">
-                            AND ${$tableName}.${item}=to_date(#{term.${item}},'YYYY-MM-DD HH24:MI:SS')
-                        </when>
-                        <otherwise>AND ${$tableName}.${item}=#{term.${item}}</otherwise>
-                    </choose>
-                </if>
-                <if test="#this['term.'+item+'$NOT']!=null">
-                    <choose>
-                        <when test="$fieldsType[item] == 'date' ">
-                            AND ${$tableName}.${item}!=to_date(#{term.${item+'$NOT'}},'YYYY-MM-DD HH24:MI:SS')
-                        </when>
-                        <otherwise>AND ${$tableName}.${item}!=#{term.${item+'$NOT'}}</otherwise>
-                    </choose>
-                </if>
-                <if test="#this['term.'+item+'$LIKE']!=null">
-                    AND INSTR(${$tableName}.${item},#{term.${item+'$LIKE'}})>0
-                </if>
-                <if test="#this['term.'+item+'$NOTLIKE']!=null">
-                    AND INSTR(${$tableName}.${item},#{term.${item+'$NOTLIKE'}})&lt;=0
-                </if>
-                <if test="#this['term.'+item+'$START']!=null">
-                    AND ${$tableName}.${item} LIKE #{term.${item+'$START'}}||'%'
-                </if>
-                <if test="#this['term.'+item+'$END']!=null">
-                    AND ${$tableName}.${item} LIKE '%'||#{term.${item+'$END'}}
-                </if>
-                <if test="#this['term.'+item+'$IN']!=null">
-                    AND ${$tableName}.${item} IN
-                    <foreach item="it" index="i" collection="#this['term.'+item+'$IN']" open="(" separator="," close=")">
-                        #{it}
-                    </foreach>
-                </if>
-                <if test="#this['term.'+item+'$NOTIN']!=null">
-                    AND ${$tableName}.${item} NOT IN
-                    <foreach item="it" index="i" collection="#this['term.'+item+'$NOTIN']" open="(" separator="," close=")">
-                        #{it}
-                    </foreach>
-                </if>
-                <if test="#this['term.'+item+'$ISNULL']!=null">
-                    AND ${$tableName}.${item} IS NULL
-                </if>
-                <if test="#this['term.'+item+'$NOTNULL']!=null">
-                    AND ${$tableName}.${item} IS NOT NULL
-                </if>
-                <if test="#this['term.'+item+'$GT']!=null">
-                    <choose>
-                        <when test="$fieldsType[item] == 'date' ">
-                            AND ${$tableName}.${item} &gt;=to_date(#{term.${item+'$GT'}},'YYYY-MM-DD HH24:MI:SS')
-                        </when>
-                        <when test="$fieldsType[item] == 'number' ">
-                            AND ${$tableName}.${item} &gt;=#{term.${item+'$GT'}}
-                        </when>
-                        <otherwise></otherwise>
-                    </choose>
-                </if>
-                <if test="#this['term.'+item+'$LT']!=null">
-                    <choose>
-                        <when test="$fieldsType[item] == 'date' ">
-                            AND ${$tableName}.${item} &lt;=to_date(#{term.${item+'$LT'}},'YYYY-MM-DD HH24:MI:SS')
-                        </when>
-                        <when test="$fieldsType[item] == 'number' ">
-                            AND ${$tableName}.${item} &lt;=#{term.${item+'$LT'}}
-                        </when>
-                        <otherwise></otherwise>
-                    </choose>
-                </if>
-            </foreach>
+    <sql id="buildWhere">
+        <!--动态生成查询条件-->
+        <foreach item="item" index="index" collection="$fields">
+            <if test="#this['term.'+item]!=null">
+                <choose>
+                    <when test="$fieldsInfo[item]['javaType'] == 'date' ">
+                        AND ${$tableName}.${item}=to_date(#{term.${item}},'YYYY-MM-DD HH24:MI:SS')
+                    </when>
+                    <otherwise>AND ${$tableName}.${item}=#{term.${item}}</otherwise>
+                </choose>
+            </if>
+            <if test="#this['term.'+item+'$NOT']!=null">
+                <choose>
+                    <when test="$fieldsInfo[item]['javaType'] == 'date' ">
+                        AND ${$tableName}.${item}!=to_date(#{term.${item+'$NOT'}},'YYYY-MM-DD HH24:MI:SS')
+                    </when>
+                    <otherwise>AND ${$tableName}.${item}!=#{term.${item+'$NOT'}}</otherwise>
+                </choose>
+            </if>
+            <if test="#this['term.'+item+'$LIKE']!=null">
+                AND INSTR(${$tableName}.${item},#{term.${item+'$LIKE'}})>0
+            </if>
+            <if test="#this['term.'+item+'$NOTLIKE']!=null">
+                AND INSTR(${$tableName}.${item},#{term.${item+'$NOTLIKE'}})&lt;=0
+            </if>
+            <if test="#this['term.'+item+'$START']!=null">
+                AND ${$tableName}.${item} LIKE #{term.${item+'$START'}}||'%'
+            </if>
+            <if test="#this['term.'+item+'$END']!=null">
+                AND ${$tableName}.${item} LIKE '%'||#{term.${item+'$END'}}
+            </if>
+            <if test="#this['term.'+item+'$IN']!=null">
+                AND ${$tableName}.${item} IN
+                <foreach item="it" index="i" collection="#this['term.'+item+'$IN']" open="(" separator="," close=")">
+                    #{it}
+                </foreach>
+            </if>
+            <if test="#this['term.'+item+'$NOTIN']!=null">
+                AND ${$tableName}.${item} NOT IN
+                <foreach item="it" index="i" collection="#this['term.'+item+'$NOTIN']" open="(" separator="," close=")">
+                    #{it}
+                </foreach>
+            </if>
+            <if test="#this['term.'+item+'$ISNULL']!=null">
+                AND ${$tableName}.${item} IS NULL
+            </if>
+            <if test="#this['term.'+item+'$NOTNULL']!=null">
+                AND ${$tableName}.${item} IS NOT NULL
+            </if>
+            <if test="#this['term.'+item+'$GT']!=null">
+                <choose>
+                    <when test="$fieldsInfo[item]['javaType'] == 'date' ">
+                        AND ${$tableName}.${item} &gt;=to_date(#{term.${item+'$GT'}},'YYYY-MM-DD HH24:MI:SS')
+                    </when>
+                    <when test="$fieldsInfo[item]['javaType'] == 'number' ">
+                        AND ${$tableName}.${item} &gt;=#{term.${item+'$GT'}}
+                    </when>
+                    <otherwise></otherwise>
+                </choose>
+            </if>
+            <if test="#this['term.'+item+'$LT']!=null">
+                <choose>
+                    <when test="$fieldsInfo[item]['javaType'] == 'date' ">
+                        AND ${$tableName}.${item} &lt;=to_date(#{term.${item+'$LT'}},'YYYY-MM-DD HH24:MI:SS')
+                    </when>
+                    <when test="$fieldsInfo[item]['javaType'] == 'number' ">
+                        AND ${$tableName}.${item} &lt;=#{term.${item+'$LT'}}
+                    </when>
+                    <otherwise></otherwise>
+                </choose>
+            </if>
+        </foreach>
         <!--动态生成查询条件结束-->
     </sql>
     <!--生成查询字段-->
-    <sql id="selectField">
+    <sql id="buildSelectField">
+        <choose>
+            <!--指定查询的字段-->
+            <when test="includes!=null and includes.size()>0">
+                <foreach item="item" index="index" collection="includes" open="" separator="," close="">
+                    <if test="item in $fields">
+                        ${$tableName}.${item} as "${item}"
+                    </if>
+                </foreach>
+            </when>
+            <!--指定不查询的字段-->
+            <when test="(includes==null or includes.size()==0) and excludes!=null and excludes.size()>0">
+                <foreach item="item" index="index" collection="$fields" open=" " separator="," close="">
+                    <if test="!(item in excludes)">
+                        ${$tableName}.${item} as "${item}"
+                    </if>
+                </foreach>
+            </when>
+            <otherwise>*</otherwise>
+        </choose>
+    </sql>
+
+    <!--生成修改字段-->
+    <sql id="buildUpdateField">
+        <set>
             <choose>
-                <!--指定查询的字段-->
+                <!--指定要修改的字段-->
                 <when test="includes!=null and includes.size()>0">
                     <foreach item="item" index="index" collection="includes" open="" separator="," close="">
                         <if test="item in $fields">
-                            ${$tableName}.${item} as "${item}"
+                            ${item}=#{data.${item},jdbcType=${$fieldsInfo[item]['jdbcType']}}
                         </if>
                     </foreach>
                 </when>
-                <!--指定不查询的字段-->
+                <!--指定不修改的字段-->
                 <when test="(includes==null or includes.size()==0) and excludes!=null and excludes.size()>0">
                     <foreach item="item" index="index" collection="$fields" open=" " separator="," close="">
                         <if test="!(item in excludes)">
-                            ${$tableName}.${item} as "${item}"
+                            ${item}= #{data.${item},jdbcType=${$fieldsInfo[item]['jdbcType']}}
                         </if>
                     </foreach>
                 </when>
-                <otherwise>*</otherwise>
+                <!--修改所有-->
+                <otherwise>
+                    <foreach item="item" index="index" collection="$fields" open=" " separator="," close="">
+                        <if test="data[item] != null">
+                            ${item}=#{data.${item},jdbcType=${$fieldsInfo[item]['jdbcType']}}
+                        </if>
+                    </foreach>
+                </otherwise>
             </choose>
+        </set>
     </sql>
 
-    <select id="select" parameterType="org.hsweb.web.bean.common.QueryParam" resultType="map">
-        <include refid="selectSql"></include>
-    </select>
-
-    <select id="selectSql" parameterType="org.hsweb.web.bean.common.QueryParam" resultType="map">
-        ${$sql}
-    </select>
+    <!--生成插入字段-->
+    <sql id="buildInsertField">
+        <choose>
+            <when test="includes!=null and includes.size()>0">
+                <foreach item="item" index="index" collection="includes" open="(" separator="," close=")">
+                    <if test="item in $fields">${item}</if>
+                </foreach>
+            </when>
+            <when test="(includes==null or includes.size()==0) and excludes!=null and excludes.size()>0">
+                <foreach item="item" index="index" collection="$fields" open="(" separator="," close=")">
+                    <if test="!(item in excludes)">${item}</if>
+                </foreach>
+            </when>
+            <otherwise>
+                <foreach item="item" index="index" collection="$fields" open="(" separator="," close=")">${item}</foreach>
+            </otherwise>
+        </choose>
+    </sql>
 
-    <select id="total" parameterType="org.hsweb.web.bean.common.QueryParam" resultType="int">
-        <include refid="totalSql"></include>
-    </select>
+    <!--生成插入值-->
+    <sql id="buildInsertValues">
+        <choose>
+            <when test="includes!=null and includes.size()>0">
+                <foreach item="item" index="index" collection="includes" open="(" separator="," close=")">
+                    <if test="item in $fields">
+                        #{data.${item},jdbcType=${$fieldsInfo[item]['jdbcType']}}
+                    </if>
+                </foreach>
+            </when>
+            <when test="(includes==null or includes.size()==0) and excludes!=null and excludes.size()>0">
+                <foreach item="item" index="index" collection="$fields" open="(" separator="," close=")">
+                    <if test="!(item in excludes)">
+                        #{data.${item},jdbcType=${$fieldsInfo[item]['jdbcType']}}
+                    </if>
+                </foreach>
+            </when>
+            <otherwise>
+                <foreach item="item" index="index" collection="$fields" open="(" separator="," close=")">
+                    #{data.${item},jdbcType=${$fieldsInfo[item]['jdbcType']}}
+                </foreach>
+            </otherwise>
+        </choose>
+    </sql>
 
     <!--生成查询sql-->
-    <sql id="selectSql">
-        SELECT
-        <include refid="BasicMapper.selectField"/>
-        FROM ${$tableName}
+    <sql id="buildSelectSql">
+        select
+        <include refid="BasicMapper.buildSelectField"/>
+        from ${$tableName}
+        <where>
+            <include refid="BasicMapper.buildWhere"/>
+        </where>
+    </sql>
+
+    <!--生成InsertSql-->
+    <sql id="buildInsertSql">
+        insert into ${$tableName}
+        <include refid="BasicMapper.buildInsertField"/>
+        values
+        <include refid="BasicMapper.buildInsertValues"/>
+    </sql>
+
+    <!--生成UpdateSql-->
+    <sql id="buildUpdateSql">
+        update ${$tableName}
+        <include refid="BasicMapper.buildUpdateField"/>
         <where>
-            <include refid="BasicMapper.selectCondition"/>
+            <include refid="BasicMapper.buildWhere"/>
+            <if test="term.size()==0">
+                u_id=#{data.u_id}
+            </if>
         </where>
     </sql>
 
     <!--生成查询数量sql-->
-    <sql id="totalSql">
-        SELECT count(0) AS total FROM ${$tableName}
+    <sql id="buildTotalSql">
+        select count(0) as "total" from ${$tableName}
         <where>
-            <include refid="BasicMapper.selectCondition"/>
+            <include refid="BasicMapper.buildWhere"/>
         </where>
     </sql>
 </mapper>