1. In the SSM frame, @ Param annotation parameters mainly used dao class methods, ease of reference in the corresponding file dao.xml
2 to simplify the xml configuration, @ Param annotation action:
(1)给参数命名,参数命名后就能根据名字得到参数值,正确的将参数传入sql语句中
(2)在方法参数的前面写上@Param("参数名"),表示给参数命名,名称就是括号中的内容
(3)在不使用@Param注解的时候,在查询语句取值时只能用#{},所属的类必须为Javabean
(4)使用@Param注解则可以使用多个参数,在查询语句中使用时可以使用#{}或者${}
3. @ Param annotation application examples:
(1)在方法参数的前面写上@Param("参数名"),表示给参数命名,名称就是括号中的内容
public Student select(@Param("aaaa") String name,@Param("bbbb") int password );
(2)然后sql语句where条件:where name= #{aaaa} 就可以根据aaaa得到参数值了
select name,password from user where name=${aaaa} and password=${bbbb}
Application Mabatis $ {} and {#} fuzzy query annotation 4. @ Param
(1)在方法参数的前面写上@Param("参数名"),表示给参数命名,名称就是括号中的内容
public Student select(@Param("aaaa") String name );
(2)模糊查询:like
select name from user where name like concat('%',#{aaaa},'%')
5. Note:
(1)如果方法参数不使用@Param注解,那么sql语句中where条件语句中必须使用字段名
(2)使用模糊查询参数有两种方式:
<1>select * from user where name like '%${name}%'
1.'%${name}%' 解析成:select * from user where name like '%yuan%'
<2>select * from user where name like '%#{name}%'
1.'%#{name}%' 解析成:select * from user where name like '%?%'
<3>但是 '%#{name}%' 解析会发成运行错误
1.Error querying database.
2.可以看出'%#{name}%' 是无法正确运行
3.但是使用 ${} 无法防止sql注入
(3)解决模糊查询传参方法如下:
<1>方法参数前面使用注解@Param,然后sql语句中where条件中就可以使用${}进行传参
<2>不使用注解@Param时,where条件使用#{}传参:使用concat()函数拼接:
select name from user where name like concat('%',#{name},'%')
<3>使用concat()函数拼接可以避免直接使用'%#{}%',运行出错!