#where情况
参数 = "itheima or 1=1"
1.用#{}
select * from foo where bar = #{xxxx}
select * from foo where bar = ?
select * from foo where bar = 'itheima or 1=1' --sql注入失败
##用${}
select * from foo where bar = ${xxxx}
select * from foo where bar = itheima or 1=1; --sql注入成功
#like情况
参数 = "' or '1=1"
##用#{}
select * from foo where bar like '%#{xx}%’
select * from foo where bar like '%?%’
select * from foo where bar like '%'' or '1=1'%' --语法错误,sql注入失败
##用${}
select * from foo where bar like '%${xx}%’
select * from foo where bar like '%' or '1=1%’ --sql注入成功
#order情况
参数 = "name"
##用#{}
select * from foo order by #{xxxx}
select * from foo order by ?
select * from foo order by 'name' --没法排序,没有结果
##用${}
select * from foo order by ${xxxx}
select * from foo order by name --ok
##用${} 可能引起sql注入
比如
[SQL]select * from aaa order by id and(updatexml(1,concat(0x7e,(select system_user())),0));
[Err] 1105 - XPATH syntax error: '~root@localhost'
#from情况 不能使用#{}
参数 = "foo"
##用#{}
select * from #{xxx};
select * from ?;
select * from 'foo'; --语法错误
##用${}
select * from ${xxx};
select * from foo; --ok
8.ssm-mybatis模糊查询like语句该怎么写?
java中用%,sql使用#{}。建议。
java中用%,sql使用${}。不建议,会sql注入。
sql中使用%,sql使用 ${}。不建议,会sql注入。
sql中使用%,sql使用 #{}。可以,但是需要注意选择合适的引号。
sql中使用%,sql使用 #{}。配合concat函数,建议。
简要回答:
不用${},有sql注入风险
用 #{},%号在java中或者sql中都可以。sql中使用%,需要配合concat函数。
1、java中用%,sql使用#{}。建议。
string wildcardname = “%itheima%”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like #{wildcardname}
</select>
#会预编译为 select * from foo where bar like ?
#假如传递【%itheima%】,sql就是select * from foo where bar like '%itheima%'
#假如传递【%itheima% or 1=1】,sql就是select * from foo where bar like '%itheima% or 1=1',也是没有sql注入的。
2、java中用%,sql使用${}。不建议,会sql注入。需要做额外的处理
string wildcardname = “%itheima% or 1=1”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like ${wildcardname}
</select>
#假如传递【%itheima% or 1=1】,sql为select * from foo where bar like %itheima% or 1=1;查询了所有
3、sql中使用%,sql使用 ${}。不建议,会sql注入。需要做额外的处理
string wildcardname = “' or '1=1”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like '%${value}%'
</select>
#假如传递【' or '1=1】,sql就是select * from foo where bar like '%' or '1=1%',能查出所有的数据。
4、sql中使用%,sql使用 #{}。可以,但是需要注意选择合适的引号
string wildcardname = “itheima”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like '%#{value}%'
</select>
#预编译为select * from foo where bar like '%?%'
#假如传递【itheima】,sql就是select * from foo where bar like '%'itheima'%',会出现sql语法错误。
#但是如下写法没问题
<select id=”selectlike”>
select * from foo where bar like "%"#{value}"%"
</select>
#预编译为select * from foo where bar like "%"?"%"
#假如传递【itheima】,sql就是select * from foo where bar like "%"'itheima'"%",不会出现语法错误。
5、sql中使用%,sql使用 #{}。配合concat函数,可以。
string wildcardname = “itheima”;
list<name> names = mapper.selectlike(wildcardname);
<select id=”selectlike”>
select * from foo where bar like concat('%',#{wildcardname,jdbcType=VARCHAR},'%')
</select>
#会预编译为 select * from foo where bar like concat('%',?,'%')
#赋值之后,sql为select * from foo where bar like concat('%','itheima','%')
IoC 和 DI 有什么关系呢?其实它们是同一个概念的不同角度描述。由于控制反转概念比较含糊(可能只是理解为容器控制对象这一个层面,很难让人想到谁来维护对象关系),所以2004年大师级人物Martin Fowler又给出了一个新的名字:“依赖注入”。相对IoC 而言,“依赖注入”明确描述了“被注入对象依赖IoC容器配置依赖对象”。