手机版
你好,游客 登录 注册
背景:
阅读新闻

MyBatis3下的通用SQL的select语句执行类

[日期:2012-02-25] 来源:Linux社区  作者:kojhliang [字体:大 中 小]
SqlMapper.xml的映射文件如下:
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"   
  3.     "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  4. <mapper namespace="com.dao.SqlMapper">  
  5.   
  6. <!-- 方法一:支持單表或者多表的通用查詢方法  By 梁敏鸿 2012年2月24日 -->  
  7.   <select id="selectBySql" resultType="map" parameterType="com.common.SelectBuilder">  
  8.    <!-- 1.SQL语句的基本部分 -->  
  9.    <if test="distinct">  
  10.         <if test="!select.empty">  
  11.          SELECT DISTINCT  
  12.          </if>  
  13.          <foreach collection="select" item="select_content" open=" " separator="," close=" " >   
  14.         ${select_content}      
  15.       </foreach>  
  16.    </if>   
  17.     <if test="!distinct">  
  18.         <if test="!select.empty">  
  19.          SELECT   
  20.          </if>  
  21.         <foreach collection="select" item="select_content" open=" " separator="," close=" " >   
  22.         ${select_content}      
  23.       </foreach>  
  24.    </if>   
  25.      <if test="!from.empty">  
  26.       FROM  
  27.       <foreach collection="from" item="from_content" open=" " separator="," close=" " >   
  28.         ${from_content}      
  29.       </foreach>  
  30.      </if>  
  31.    <!-- 2.join 查询部分-->  
  32.        <if test="!join.empty">  
  33.       JOIN  
  34.       <foreach collection="join" item="join_content" open=" " separator="\n JOIN" close=" " >   
  35.         ${join_content}      
  36.       </foreach>  
  37.      </if>  
  38.      <if test="!innerJoin.empty">  
  39.       INNER JOIN  
  40.       <foreach collection="innerJoin" item="innerJoin_content" open=" " separator="\n INNER JOIN" close=" " >   
  41.         ${innerJoin_content}      
  42.       </foreach>  
  43.      </if>  
  44.       <if test="!outerJoin.empty">  
  45.       OUTER JOIN  
  46.       <foreach collection="outerJoin" item="outerJoin_content" open=" " separator="\n OUTER JOIN" close=" " >   
  47.         ${outerJoin_content}      
  48.       </foreach>  
  49.      </if>  
  50.       <if test="!leftOuterJoin.empty">  
  51.      LEFT OUTER JOIN  
  52.       <foreach collection="leftOuterJoin" item="leftOuterJoin_content" open=" " separator="\n  LEFT OUTER JOIN" close=" " >   
  53.         ${leftOuterJoin_content}      
  54.       </foreach>  
  55.      </if>  
  56.      <if test="!rightOuterJoin.empty">  
  57.      RIGHT OUTER JOIN  
  58.       <foreach collection="rightOuterJoin" item="rightOuterJoin_content" open=" " separator="\n  RIGHT OUTER JOIN" close=" " >   
  59.         ${rightOuterJoin_content}      
  60.       </foreach>  
  61.      </if>  
  62.    <!-- 3.SQL语句WHERE部分  -->  
  63.      <if test="!where.empty">  
  64.       where 1=1  
  65.       <foreach collection="where" item="where_Criterion" open=" " separator=" " close=" " >   
  66.                <choose >  
  67.                 <when test="where_Criterion.noValue" >  
  68.                    ${where_Criterion.condition}  
  69.                 </when>  
  70.                 <when test="where_Criterion.singleValue" >  
  71.                    ${where_Criterion.condition} #{where_Criterion.value}  
  72.                 </when>  
  73.                 <when test="where_Criterion.betweenValue" >  
  74.                    ${where_Criterion.condition} #{where_Criterion.value} and #{where_Criterion.secondValue}  
  75.                 </when>  
  76.                 <when test="where_Criterion.listValue" >  
  77.                    ${where_Criterion.condition}  
  78.                   <foreach collection="where_Criterion.value" item="listItem" open="(" close=")" separator="," >  
  79.                     #{listItem}  
  80.                   </foreach>  
  81.                 </when>  
  82.               </choose>   
  83.            <!-- 加上close关闭符号,为了处理or的括号包含的情况 -->   
  84.                 <if test="where_Criterion.close!=null and where_Criterion.close!=''">  
  85.                  ${where_Criterion.close}  
  86.                 </if>              
  87.       </foreach>  
  88.      </if>  
  89.     <!-- 4.SQL语句GROUP BY部分 -->  
  90.       <if test="!groupBy.empty">  
  91.           GROUP BY  
  92.       <foreach collection="groupBy" item="groupBy_content" open=" " separator="," close=" " >   
  93.         ${groupBy_content}      
  94.       </foreach>  
  95.       </if>  
  96.       <!-- 5.SQL语句HAVING部分 -->  
  97.       <if test="!having.empty">  
  98.           HAVING 1=1  
  99.        <foreach collection="having" item="having_Criterion" open=" " separator=" " close=" " >   
  100.                <choose >  
  101.                 <when test="having_Criterion.noValue" >  
  102.                    ${having_Criterion.condition}  
  103.                 </when>  
  104.                 <when test="having_Criterion.singleValue" >  
  105.                    ${having_Criterion.condition} #{having_Criterion.value}  
  106.                 </when>  
  107.                 <when test="having_Criterion.betweenValue" >  
  108.                    ${having_Criterion.condition} #{having_Criterion.value} and #{having_Criterion.secondValue}  
  109.                 </when>  
  110.                 <when test="having_Criterion.listValue" >  
  111.                    ${having_Criterion.condition}  
  112.                   <foreach collection="having_Criterion.value" item="listItem" open="(" close=")" separator="," >  
  113.                     #{listItem}  
  114.                   </foreach>  
  115.                 </when>  
  116.               </choose>   
  117.               <!-- 加上close关闭符号,为了处理or的括号包含的情况 -->   
  118.                 <if test="having_Criterion.close!=null and having_Criterion.close!=''">  
  119.                  ${having_Criterion.close}  
  120.                 </if>              
  121.       </foreach>  
  122.       </if>  
  123.       <!-- 6.SQL语句ORDER BY部分  -->  
  124.       <if test="!orderBy.empty">  
  125.           ORDER BY  
  126.            <foreach collection="orderBy" item="orderBy_content" open=" " separator="," close=" " >   
  127.             ${orderBy_content}      
  128.             </foreach>  
  129.       </if>  
  130. </select>     
  131. <!-- 方法二:支持單表或者多表的通用统计方法  By lmh 2012年2月24日 -->  
  132.   <select id="countBySql" resultType="int" parameterType="com.common.SelectBuilder">  
  133.          <if test="distinct">  
  134.         <if test="!select.empty">  
  135.          SELECT DISTINCT  
  136.          </if>  
  137.         COUNT(*)  
  138.    </if>   
  139.     <if test="!distinct">  
  140.         <if test="!select.empty">  
  141.          SELECT   
  142.          </if>  
  143.         COUNT(*)  
  144.    </if>   
  145.      <if test="!from.empty">  
  146.       FROM  
  147.       <foreach collection="from" item="from_content" open=" " separator="," close=" " >   
  148.         ${from_content}      
  149.       </foreach>  
  150.      </if>  
  151.        
  152.        <if test="!join.empty">  
  153.       JOIN  
  154.       <foreach collection="join" item="join_content" open=" " separator="\n JOIN" close=" " >   
  155.         ${join_content}      
  156.       </foreach>  
  157.      </if>  
  158.      <if test="!innerJoin.empty">  
  159.       INNER JOIN  
  160.       <foreach collection="innerJoin" item="innerJoin_content" open=" " separator="\n INNER JOIN" close=" " >   
  161.         ${innerJoin_content}      
  162.       </foreach>  
  163.      </if>  
  164.       <if test="!outerJoin.empty">  
  165.       OUTER JOIN  
  166.       <foreach collection="outerJoin" item="outerJoin_content" open=" " separator="\n OUTER JOIN" close=" " >   
  167.         ${outerJoin_content}      
  168.       </foreach>  
  169.      </if>  
  170.       <if test="!leftOuterJoin.empty">  
  171.      LEFT OUTER JOIN  
  172.       <foreach collection="leftOuterJoin" item="leftOuterJoin_content" open=" " separator="\n  LEFT OUTER JOIN" close=" " >   
  173.         ${leftOuterJoin_content}      
  174.       </foreach>  
  175.      </if>  
  176.      <if test="!rightOuterJoin.empty">  
  177.      RIGHT OUTER JOIN  
  178.       <foreach collection="rightOuterJoin" item="rightOuterJoin_content" open=" " separator="\n  RIGHT OUTER JOIN" close=" " >   
  179.         ${rightOuterJoin_content}      
  180.       </foreach>  
  181.      </if>  
  182.   
  183.      <if test="!where.empty">  
  184.       where 1=1  
  185.       <foreach collection="where" item="where_Criterion" open=" " separator=" " close=" " >   
  186.                <choose >  
  187.                 <when test="where_Criterion.noValue" >  
  188.                   ${where_Criterion.condition}  
  189.                 </when>  
  190.                 <when test="where_Criterion.singleValue" >  
  191.                    ${where_Criterion.condition} #{where_Criterion.value}  
  192.                 </when>  
  193.                 <when test="where_Criterion.betweenValue" >  
  194.                   ${where_Criterion.condition} #{where_Criterion.value} and #{where_Criterion.secondValue}  
  195.                 </when>  
  196.                 <when test="where_Criterion.listValue" >  
  197.                    ${where_Criterion.condition}  
  198.                   <foreach collection="where_Criterion.value" item="listItem" open="(" close=")" separator="," >  
  199.                     #{listItem}  
  200.                   </foreach>  
  201.                 </when>  
  202.               </choose>   
  203.               
  204.                 <if test="where_Criterion.close!=null and where_Criterion.close!=''">  
  205.                  ${where_Criterion.close}  
  206.                 </if>              
  207.       </foreach>  
  208.      </if>  
  209.   
  210.       <if test="!groupBy.empty">  
  211.           GROUP BY  
  212.       <foreach collection="groupBy" item="groupBy_content" open=" " separator="," close=" " >   
  213.         ${groupBy_content}      
  214.       </foreach>  
  215.       </if>  
  216.   
  217.       <if test="!having.empty">  
  218.           HAVING 1=1  
  219.        <foreach collection="having" item="having_Criterion" open=" " separator=" " close=" " >   
  220.                <choose >  
  221.                 <when test="having_Criterion.noValue" >  
  222.                    ${having_Criterion.condition}  
  223.                 </when>  
  224.                 <when test="having_Criterion.singleValue" >  
  225.                    ${having_Criterion.condition} #{having_Criterion.value}  
  226.                 </when>  
  227.                 <when test="having_Criterion.betweenValue" >  
  228.                    ${having_Criterion.condition} #{having_Criterion.value} and #{having_Criterion.secondValue}  
  229.                 </when>  
  230.                 <when test="having_Criterion.listValue" >  
  231.                    ${having_Criterion.condition}  
  232.                   <foreach collection="having_Criterion.value" item="listItem" open="(" close=")" separator="," >  
  233.                     #{listItem}  
  234.                   </foreach>  
  235.                 </when>  
  236.               </choose>   
  237.   
  238.                 <if test="having_Criterion.close!=null and having_Criterion.close!=''">  
  239.                  ${having_Criterion.close}  
  240.                 </if>              
  241.       </foreach>  
  242.       </if>  
  243.   
  244.       <if test="!orderBy.empty">  
  245.           ORDER BY  
  246.            <foreach collection="orderBy" item="orderBy_content" open=" " separator="," close=" " >   
  247.             ${orderBy_content}      
  248.             </foreach>  
  249.       </if>  
  250. </select>     
  251. </mapper>  
望大家指出设计的不足和程序存在的漏洞和错误。
linux
相关资讯       SQL  MyBatis  MyBatis3 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/5/24 14:04:43
牛叉