mysql的版本:5.7.24
mybatis的版本:3.5.5
Mybatis操作mysql数据库使用$没问题而使用#却报错
Mapper对应的接口
public interface ListDataMapper {
ArrayList selectThirteen(@Param("table")String table, @Param("page")int page);
}
mapper对应的sql
<select id="selectThirteen" resultType="ListData">
select name,REPLACE(score,'.','') as score,num from #{table} order by LPAD(score,7,0) asc limit #{(page-1)*13},13;
select>
mybatis配置信息
configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.supercount.pojo"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///db1?useSSL=false&useServerPrepStmts=true&cachePrepStmts=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="1948"/>
dataSource>
environment>
environments>
<mappers>
<package name="com.supercount.mapper"/>
mappers>
configuration>
运行结果
我使用了$可以解决但是不能防止sql注入
使用#进行预编译可以防止注入漏洞
由于#{}会给传入的参数加上单引号,所以limit后面使用#{}会报错,同样的还有表名动态注入,模糊查询,批量查询等情况下都只能使用${}。可以在代码层面对传到${}中的参数加以校验预防sql注入问题。
mybatis 有一个PageHelper,在Service层调用接口方法的那条上面写,它会监控起来,sql语句不用写分页
由于#{}会给传入的参数加上单引号,所以limit后面使用#{}会报错,同样的还有表名动态注入,模糊查询,批量查询等情况下都只能使用${}。可以在代码层面对传到${}中的参数加以校验预防sql注入问题。