1、mybaits中使用小于号
【错误分析】
如过在mybatis中使用<>&等字符,配置文件中会提示如下错误
The content of elements must consist of well-formed character data or markup.
【解决方法】
方法一 如果是在动态语句中,则需要转换
1 2 |
<if test="startDateTime!=null"> and mm.ttime > to_date(#{startDateTime},'yyyy-mm-dd hh24:mi:ss')</if> <if test="endDateTime!=null"> and mm.ttime <= to_date(#{endDateTime},'yyyy-mm-dd hh24:mi:ss')</if> |
符号:
1 2 3 4 5 6 |
< < > > <> <> & & ' ' " " |
方法二 (推荐), 此时特殊符号不进行解析
1 |
<![CDATA[ and mm.ttime > to_date(#{startDateTime},'yyyy-mm-dd hh24:mi:ss') ]]> |
【参考资料】
2、mybatis 可以返回map类型对象
1 |
<select id="queryChannelEmpowersByPage" resultType="map" parameterType="com.pgw.dto.empower.EmpowerParamDTO"> |
注意:resultMap 和 resultType 区别
3、mybaits 查询sql中in的写法
1 2 3 4 5 6 |
<if test="nodeCodes != null and nodeCodes.size>0"> and a.account in(SELECT account from t_empower where code in <foreach item="item" index="index" collection="nodeCodes" open="(" separator="," close="))"> #{item} </foreach> </if> |
4、mybatis 查询like 正规写法
正确方法一:
1 2 3 |
<if test="targetName !=null and targetName != ''"> and a.target_name like CONCAT('%',#{targetName},'%') </if> |
正确方法二:
like “%”#{name}”%”
错误方法
like ‘%”#{name}”%’
【参考资料】