mybatis的xml文件中数据传输符号#和$的区别

1.用#传参相当于

String sql = "select * from admin_domain_location order by ?";

PreparedStatement st = con.prepareStatement(sql);

st.setString(1, "domain_id");

System.out.println(st.toString());

这条sql最终为:select * from admin_domain_location order by 'domain_id'

2.用$传参相当于

String input = "domain_id";

String sql = "select * from admin_domain_location order by "+input;

PreparedStatement st = con.prepareStatement(sql);
System.out.println(st.toString());

这条sql最终为:select * from admin_domain_location order by domain_id

3.总结:

#传过来的数据会自动加上引号,$则不会;

因此order by语句后面的列名不能用#传输;

#和$可以用xml文件中显式加引号的方式达到一致的效果,但$容易发生sql注入;

因此,最好是用#。



你可能感兴趣的:(java)