记录一次mapper.xml 字符串判断的小坑

Integer queryDaNumByParam(@Param("timeType") String timeType, 
@Param("dazt") Integer dazt, @Param("tableName") String tableName);

timeType 参数是字符串 和数字比较时 必须使用双引号 下面是错误的

<select id="queryDaNumByParam" resultType="java.lang.Integer">
    SELECT
    count( id ) AS num
    FROM
    ${tableName}
    WHERE DAZT = #{dazt}
    <if test="timeType == '1'">
      AND TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ) &gt;= trunc( SYSDATE, 'day' ) + 1
      AND TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ) &lt;= trunc( SYSDATE, 'day' ) + 8
    </if>
    <if test="timeType == '2'">
      AND TO_CHAR( TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ), 'YYYY-MM' ) = TO_CHAR( SYSDATE, 'YYYY-MM' )
    </if>
    <if test="timeType == '3'">
      AND TO_CHAR( TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ), 'YYYY' ) = TO_CHAR( SYSDATE, 'YYYY' )
    </if>
  </select>

应该为下面这种

	<select id="queryDaNumByParam" resultType="java.lang.Integer">
		SELECT
		count( id ) AS num
		FROM
		${tableName}
		WHERE DAZT = #{dazt}
		<if test='timeType == "1" '>
			AND TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ) &gt;= trunc( SYSDATE, 'day' ) + 1
			AND TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ) &lt;= trunc( SYSDATE, 'day' ) + 8
		</if>
		<if test='timeType == "2" '>
			AND TO_CHAR( TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ), 'YYYY-MM' ) = TO_CHAR( SYSDATE, 'YYYY-MM' )
		</if>
		<if test='timeType == "3" '>
			AND TO_CHAR( TO_DATE( CJSJ, 'yyyy-mm-dd hh24:mi:ss' ), 'YYYY' ) = TO_CHAR( SYSDATE, 'YYYY' )
		</if>
	</select>
上一篇:linux内核中的GPIO系统之(4):pinctrl驱动的理解和总结


下一篇:Oracle 时间与绝对秒数转换