MyBatis3下的通用SQL的select语句执行类
| [日期:2012-02-25] |
来源:Linux社区
作者:kojhliang |
[字体:大 中 小]
|
SqlMapper.xml的映射文件如下:
- <?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="com.dao.SqlMapper">
-
- <!-- 方法一:支持單表或者多表的通用查詢方法 By 梁敏鸿 2012年2月24日 -->
- <select id="selectBySql" resultType="map" parameterType="com.common.SelectBuilder">
- <!-- 1.SQL语句的基本部分 -->
- <if test="distinct">
- <if test="!select.empty">
- SELECT DISTINCT
- </if>
- <foreach collection="select" item="select_content" open=" " separator="," close=" " >
- ${select_content}
- </foreach>
- </if>
- <if test="!distinct">
- <if test="!select.empty">
- SELECT
- </if>
- <foreach collection="select" item="select_content" open=" " separator="," close=" " >
- ${select_content}
- </foreach>
- </if>
- <if test="!from.empty">
- FROM
- <foreach collection="from" item="from_content" open=" " separator="," close=" " >
- ${from_content}
- </foreach>
- </if>
- <!-- 2.join 查询部分-->
- <if test="!join.empty">
- JOIN
- <foreach collection="join" item="join_content" open=" " separator="\n JOIN" close=" " >
- ${join_content}
- </foreach>
- </if>
- <if test="!innerJoin.empty">
- INNER JOIN
- <foreach collection="innerJoin" item="innerJoin_content" open=" " separator="\n INNER JOIN" close=" " >
- ${innerJoin_content}
- </foreach>
- </if>
- <if test="!outerJoin.empty">
- OUTER JOIN
- <foreach collection="outerJoin" item="outerJoin_content" open=" " separator="\n OUTER JOIN" close=" " >
- ${outerJoin_content}
- </foreach>
- </if>
- <if test="!leftOuterJoin.empty">
- LEFT OUTER JOIN
- <foreach collection="leftOuterJoin" item="leftOuterJoin_content" open=" " separator="\n LEFT OUTER JOIN" close=" " >
- ${leftOuterJoin_content}
- </foreach>
- </if>
- <if test="!rightOuterJoin.empty">
- RIGHT OUTER JOIN
- <foreach collection="rightOuterJoin" item="rightOuterJoin_content" open=" " separator="\n RIGHT OUTER JOIN" close=" " >
- ${rightOuterJoin_content}
- </foreach>
- </if>
- <!-- 3.SQL语句WHERE部分 -->
- <if test="!where.empty">
- where 1=1
- <foreach collection="where" item="where_Criterion" open=" " separator=" " close=" " >
- <choose >
- <when test="where_Criterion.noValue" >
- ${where_Criterion.condition}
- </when>
- <when test="where_Criterion.singleValue" >
- ${where_Criterion.condition} #{where_Criterion.value}
- </when>
- <when test="where_Criterion.betweenValue" >
- ${where_Criterion.condition} #{where_Criterion.value} and #{where_Criterion.secondValue}
- </when>
- <when test="where_Criterion.listValue" >
- ${where_Criterion.condition}
- <foreach collection="where_Criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
- <!-- 加上close关闭符号,为了处理or的括号包含的情况 -->
- <if test="where_Criterion.close!=null and where_Criterion.close!=''">
- ${where_Criterion.close}
- </if>
- </foreach>
- </if>
- <!-- 4.SQL语句GROUP BY部分 -->
- <if test="!groupBy.empty">
- GROUP BY
- <foreach collection="groupBy" item="groupBy_content" open=" " separator="," close=" " >
- ${groupBy_content}
- </foreach>
- </if>
- <!-- 5.SQL语句HAVING部分 -->
- <if test="!having.empty">
- HAVING 1=1
- <foreach collection="having" item="having_Criterion" open=" " separator=" " close=" " >
- <choose >
- <when test="having_Criterion.noValue" >
- ${having_Criterion.condition}
- </when>
- <when test="having_Criterion.singleValue" >
- ${having_Criterion.condition} #{having_Criterion.value}
- </when>
- <when test="having_Criterion.betweenValue" >
- ${having_Criterion.condition} #{having_Criterion.value} and #{having_Criterion.secondValue}
- </when>
- <when test="having_Criterion.listValue" >
- ${having_Criterion.condition}
- <foreach collection="having_Criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
- <!-- 加上close关闭符号,为了处理or的括号包含的情况 -->
- <if test="having_Criterion.close!=null and having_Criterion.close!=''">
- ${having_Criterion.close}
- </if>
- </foreach>
- </if>
- <!-- 6.SQL语句ORDER BY部分 -->
- <if test="!orderBy.empty">
- ORDER BY
- <foreach collection="orderBy" item="orderBy_content" open=" " separator="," close=" " >
- ${orderBy_content}
- </foreach>
- </if>
- </select>
- <!-- 方法二:支持單表或者多表的通用统计方法 By lmh 2012年2月24日 -->
- <select id="countBySql" resultType="int" parameterType="com.common.SelectBuilder">
- <if test="distinct">
- <if test="!select.empty">
- SELECT DISTINCT
- </if>
- COUNT(*)
- </if>
- <if test="!distinct">
- <if test="!select.empty">
- SELECT
- </if>
- COUNT(*)
- </if>
- <if test="!from.empty">
- FROM
- <foreach collection="from" item="from_content" open=" " separator="," close=" " >
- ${from_content}
- </foreach>
- </if>
-
- <if test="!join.empty">
- JOIN
- <foreach collection="join" item="join_content" open=" " separator="\n JOIN" close=" " >
- ${join_content}
- </foreach>
- </if>
- <if test="!innerJoin.empty">
- INNER JOIN
- <foreach collection="innerJoin" item="innerJoin_content" open=" " separator="\n INNER JOIN" close=" " >
- ${innerJoin_content}
- </foreach>
- </if>
- <if test="!outerJoin.empty">
- OUTER JOIN
- <foreach collection="outerJoin" item="outerJoin_content" open=" " separator="\n OUTER JOIN" close=" " >
- ${outerJoin_content}
- </foreach>
- </if>
- <if test="!leftOuterJoin.empty">
- LEFT OUTER JOIN
- <foreach collection="leftOuterJoin" item="leftOuterJoin_content" open=" " separator="\n LEFT OUTER JOIN" close=" " >
- ${leftOuterJoin_content}
- </foreach>
- </if>
- <if test="!rightOuterJoin.empty">
- RIGHT OUTER JOIN
- <foreach collection="rightOuterJoin" item="rightOuterJoin_content" open=" " separator="\n RIGHT OUTER JOIN" close=" " >
- ${rightOuterJoin_content}
- </foreach>
- </if>
-
- <if test="!where.empty">
- where 1=1
- <foreach collection="where" item="where_Criterion" open=" " separator=" " close=" " >
- <choose >
- <when test="where_Criterion.noValue" >
- ${where_Criterion.condition}
- </when>
- <when test="where_Criterion.singleValue" >
- ${where_Criterion.condition} #{where_Criterion.value}
- </when>
- <when test="where_Criterion.betweenValue" >
- ${where_Criterion.condition} #{where_Criterion.value} and #{where_Criterion.secondValue}
- </when>
- <when test="where_Criterion.listValue" >
- ${where_Criterion.condition}
- <foreach collection="where_Criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
-
- <if test="where_Criterion.close!=null and where_Criterion.close!=''">
- ${where_Criterion.close}
- </if>
- </foreach>
- </if>
-
- <if test="!groupBy.empty">
- GROUP BY
- <foreach collection="groupBy" item="groupBy_content" open=" " separator="," close=" " >
- ${groupBy_content}
- </foreach>
- </if>
-
- <if test="!having.empty">
- HAVING 1=1
- <foreach collection="having" item="having_Criterion" open=" " separator=" " close=" " >
- <choose >
- <when test="having_Criterion.noValue" >
- ${having_Criterion.condition}
- </when>
- <when test="having_Criterion.singleValue" >
- ${having_Criterion.condition} #{having_Criterion.value}
- </when>
- <when test="having_Criterion.betweenValue" >
- ${having_Criterion.condition} #{having_Criterion.value} and #{having_Criterion.secondValue}
- </when>
- <when test="having_Criterion.listValue" >
- ${having_Criterion.condition}
- <foreach collection="having_Criterion.value" item="listItem" open="(" close=")" separator="," >
- #{listItem}
- </foreach>
- </when>
- </choose>
-
- <if test="having_Criterion.close!=null and having_Criterion.close!=''">
- ${having_Criterion.close}
- </if>
- </foreach>
- </if>
-
- <if test="!orderBy.empty">
- ORDER BY
- <foreach collection="orderBy" item="orderBy_content" open=" " separator="," close=" " >
- ${orderBy_content}
- </foreach>
- </if>
- </select>
- </mapper>
