mybatis 相关

一、mybatis转义问题

mybatis中SQL在 mapper.xml 中书写时,如果有  < 、>、<=、>=  时会出错,应该使用转义的写法。

  • 写法一  转义
< <= > >= & ' "
&lt; &lt;= &gt; &gt;= &amp; &apos; &quot;

 

例如:

age &gt;= #{age}
  • 写法二

<![CDATA[  sql语句  ]]>

例如:

age <![CDATA[ >= ]]> #{age}

 二、Mybatis中&和#的区别

  •   1 #是将传入的值当做字符串的形式,

  eg: select id,name,age from student where name=#{name}   -- name='cy'

  •    2 $是将传入的数据直接显示生成sql语句,

  eg: select id,name,age from student where name=${name}    -- name=cy

  •    3 使用#可以很大程度上防止sql注入。(语句的拼接)
  •    4 但是如果使用在order by 中就需要使用 $.
  •    5 在大多数情况下还是经常使用#,但在不同情况下必须使用$. 

三、注意点

  1. mybatis中实体字段如果是Integer类型的话,在xml中判断会认为是'';
原文地址:https://www.cnblogs.com/BillyYoung/p/10704012.html