@Param是MyBatis所提供的(org.apache.ibatis.annotations.Param),作为Dao层的注解,作用是用于传递参数,从而可以与SQL中的的字段名相对应,一般在2=<参数数<=5时使用最佳。
不使用@Param的情况
当存在多个参数时,可使用Map
接口:
public List<Student> findStudentByMap(Map<String, Object> parameter);
XML文件:
<select id="findStudentByMap" parameterType="map" resultType="student">
SELECT * FROM t_student
WHERE Name=#{Name}
AND id=#{id}
<select>
Map可读性差,每次都要阅读他的键才能知道其中的作用,而且不能限定传递的数据类型
使用@Param
public List<Student> findStudentByAnnotation(@Param("Name") String Name, @Param("id") long id);