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 文件里 < (小于)号 , >(大于)号 ,会被认为是括号,需要额外注意,
解决办法 : 将 < 号换成 < > 号 换成>
<if test="param.day =='1'">
AND DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= 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 > -1">
and rownum >= #{start} and rownum < #{limit}
</if>
</when>
<otherwise>
<if test="start > -1">
where rownum >= #{start} and rownum < #{limit}
</if>
</otherwise>
</choose>
</when>
<otherwise>
<if test="start > -1">
where rownum >= #{start} and rownum < #{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 > -1 or limit > -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 > -1 and limit > -1">
rn > #{start} and rn <= #{limit}
</when>
<otherwise>
<choose>
<when test="start > -1">
rn > #{start}
</when>
<otherwise>
rn <= #{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>