1.动态查询语句
2.SQL中占位符不能在单引号中,否则,会以?进行查询数据
'%#{param}%'
'%?%'
3.SQL中不能使用加号进行字符串拼接,加号是用来做运算的
'%'+'D'+'%'
4.MyBatis进行拼串,拼串会出现 SQL 注入情况 ,例如:“or 1=1”
'%${param}%'
5.使用内置方法进行拼串
concat('%',#{param},'%')
6.查询条件值本身为%,查询出所有的数据
concat('%',#{param},'%') => '%%%'
'%\%%' 使用转译字符再进行查询。
7.#和\是一个意思,表示转译。使用#代替\
select * from t_user where username like '%#%%' escape '#'
select * from t_user where loginacct like '%@%%' escape '@'
SELECT * FROM t_user WHERE loginacct LIKE concat('%','@%','%') ESCAPE '@'
8.常见的SQL文,在Oracle中,使用两个竖线用来表示字符串拼接,MySQL中没有这样的语法。
select * from t_user where username like '%'|| #{param} ||'%'
<select id="queryCount" resultType="int"> select count(*) from t_user <where> <if test="queryText!=null"> loginacct like '%#{queryText}%' </if> </where> </select> <select id="pageQuery" resultType="User"> select * from t_user <where> <if test="queryText!=null"> loginacct like '%#{queryText}%' </if> </where> limit #{start}, #{size} </select> |
- 有3个参数,但是只是指定了2个.
- Select * from t_user where loginacct like '%#{loginacct}%' limit?,?
org.springframework.dao.TransientDataAccessResourceException: ### Error querying database. Cause: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). ### The error may exist in URL [jar:file:/F:/atcrowdfunding/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/atcrowdfunding-main/WEB-INF/lib/atcrowdfunding-user-0.0.1-SNAPSHOT.jar!/mybatis/mapper-user.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL: select * from t_user WHERE loginacct like '%?%' limit ?, ? ### Cause: java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). ; SQL []; Parameter index out of range (3 > number of parameters, which is 2).; nested exception is java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). |
Id = 100 OR 1=1
SELECT * FROM t_user WHERE id= ${id}
SELECT * FROM t_user WHERE id= 100 OR 1=1
在特定场合可以使用${}:
例如:
- Create table ${tableName} … //表名称位置不能使用?占位符,所以也就不能使用#{}
- Order by ${fieldName} asc //对字段进行排序,可以 传递动态字段名称.
<select id="queryCount" resultType="int"> select count(*) from t_user <where> <if test="queryText!=null"> loginacct like '%${queryText}%' </if> </where> </select> <select id="pageQuery" resultType="User"> select * from t_user <where> <if test="queryText!=null"> loginacct like '%${queryText}%' </if> </where> limit #{start}, #{size} </select> |
<select id="queryCount" resultType="int"> select count(*) from t_user <where> <if test="queryText!=null"> loginacct like concat('%',#{queryText},'%') </if> </where> </select> <select id="pageQuery" resultType="User"> select * from t_user <where> <if test="queryText!=null"> loginacct like concat('%',#{queryText},'%') </if> </where> limit #{start}, #{size} </select> |
- 查询关键字为% 和 \
- 查询关键字为%将数据都查询出来了,不安全.
- 对查询的特殊符号进行转译.
String queryText = "%"; if(StringUtil.isNotEmpty(queryText)){ //斜线本身需要转译,regex中两个\\表示一个\ ; Java中也是两个\\表示一个\;所以,需要四个斜线 queryText = queryText.replaceAll("%", "\\\\%"); System.out.println("--------------"+queryText); } |
- SQL语句中转译
- 对特殊符号进行转译;斜杠本身也属于特殊符号,需要转译.
select * from t_user where loginacct like '%\\%%' |
- 注意:
- 尽量在保存数据时,验证数据的合法性,尽量避免存在的字符串中含有特殊符号.
- 但是,有时无法避免,则需要进行特殊处理:
例如:
"<<合同编号[101]文件>>"
"D:\\atguigu"