sql 语句报错集

1、关于MyBatis一些小错误,元素内容必须由格式正确的字符数据或标记组成.(oracle)

今天在Mapper.xml文件写查询语句报了个奇怪的错误

Caused by: org.apache.ibatis.builder.BuilderException: Error creating document instance.  Cause: org.xml.sax.SAXParseException; lineNumber: 30; columnNumber: 45; 元素内容必须由格式正确的字符数据或标记组成


原因 : Mapper.xml 文件里  < (小于)号 ,   >(大于)号 ,会被认为是括号,需要额外注意,

解决办法 : 将 < 号换成  &lt;     > 号 换成&gt; 


<if test="param.day =='1'">
AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) &lt;=  SEARCH_TIME
</if>


接下来就不报错了,但是我发现判断 <if test="param.day =='1'">失败,没有进入到if里

原因 :在xml文件直接判断 ' != ' 是可以的 ,判断 ' == ' 会发生冲突

解决办法  :   <if test="param.day =='1'.toString()"> 在判断的参数后加上toString()方法;

2、mybatis 判断参数中是否有筛选条件以及排序查询代码(此处踩了好久的坑)

2.1(这种方法只能查询1-10条内的数据,大于10条查询时list为[]),所以接下来的才是争取sql语句

 <select id="selectByExample" parameterType="com.po.UploadFileExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Feb 25 22:47:18 CST 2019.
    -->
    select
    <if test="distinct">
      distinct
    </if>
    <include refid="Base_Column_List" />
    from UPLOAD_FILE
     <choose>
    	<when test="_parameter != null">
    		 <include refid="Example_Where_Clause" />
    		 <choose>
    		 	<when test="_parameter.oredCriteria.get(0).valid">
    		 		<if test="start &gt; -1">
			      		and rownum &gt;= #{start} and rownum &lt; #{limit}
			 		</if>
    		 	</when>
    		 	<otherwise>
    		 		 <if test="start &gt; -1">
			      		where rownum &gt;= #{start} and rownum &lt; #{limit}
			 		</if>
    		 	</otherwise>
    		 </choose>
    	</when>
    	<otherwise>
    		 <if test="start &gt; -1">
      			where rownum &gt;= #{start} and rownum &lt; #{limit}
 			</if>
    	</otherwise>
    </choose>
    <if test="orderByClause != null">
      order by ${orderByClause}
    </if>
  </select>

2.2正确排序分页查询

--sql
select from parent1.* from(
    select parent2.*,rownum rn from(
        select table.* from table where() oder by columname desc
    )parent2
)parent1 where (rn>start / rn<=limit / rn betwwen start and limit 
 
 <!--xml sql 内容-->
<select id="selectByExample" parameterType="com.po.DialogExample" resultMap="BaseResultMap">
    <!--
      WARNING - @mbg.generated
      This element is automatically generated by MyBatis Generator, do not modify.
      This element was generated on Mon Feb 25 20:06:55 CST 2019.
    -->
    <choose>
    	<when  test="start &gt; -1 or limit &gt; -1">
    	select parent1.* from(
    		select parent2.*, rownum rn from(
	    		select 
	    		 <if test="distinct">
			      distinct
			    </if>
	    		<include refid="Base_Column_List" />
	    		 from DIALOG  
	   			<if test="_parameter != null">
	   				<include refid="Example_Where_Clause" />
	   			</if>
	   			<if test="orderByClause != null">
			      order by ${orderByClause}
			    </if>
	    		) parent2
    	)parent1 where 
    	<choose>
    		<when test="start &gt; -1 and limit &gt; -1">
    			rn &gt; #{start}  and rn &lt;= #{limit}
    		</when>
    		<otherwise>
    			<choose>
    				<when test="start &gt; -1">
    					rn &gt; #{start}
    				</when>
    				<otherwise>
    					rn &lt;= #{limit}
    				</otherwise>
    			</choose>
    		</otherwise>
    	</choose>
    	</when>
    	<otherwise>
    		select 
    		 <if test="distinct">
		      distinct
		    </if>
    		<include refid="Base_Column_List" />
    		 from DIALOG uf  
   			<if test="_parameter != null">
   				<include refid="Example_Where_Clause" />
   			</if>
   			<if test="orderByClause != null">
		      order by ${orderByClause}
		    </if>
    	</otherwise>
    </choose>
  </select>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值