主要是分析下mybatis传参数的一些细节问题:
先说结论
- dao层有1个参数,不管xml中写啥、写几个参数都生效。
- dao层有多个参数,必须指定@Param(“”),否则报错。
假设我们有一个表,是table表,表里有字段:name、name1、name2 等等
dao不使用@Param() xml只有1个参数 OK
dao层:
Integer count(String name);
xml:
select 1 from table where name = #{name}
测试:count(“A0001”)
最终执行的SQL:
select 1 from table where name = ‘A0001’
dao不使用@Param() xml有多个参数 OK
dao层:
Integer count(String name);
xml:
select 1 from table where name = #{name}
测试:count(“A0001”)
最终执行的SQL: select 1 from table where name = ‘A0001’ and name2 = ‘A0001’
dao不使用@Param(),且有多个参数 xml有多个参数 报错
dao层:
Integer count(String name,String name2);
xml:
select 1 from table where name = #{name} and name2 = #{name2}
测试:count(“A0001”,”测试名称”)
最终执行的SQL:Parameter ‘A0001’ not found. Available parameters are [arg1, arg0, param1, param2]
全文完。