首先,mybaits中无论一个参数还是多个参数,映射文件中的parameterType都可以不写
接口:public List<Person> getpersonbygenderAndAge(String gender,int age);//根据性别和年纪获取person对象
mybatis3.4.0以上版本时映射文件写法:
<select id="getPersonByGenderAndAage" resultType="Person">
select * from person where gender=#{0} and age>#{1}
</select>
mybatis3.4.0及以后版本映射文件写法:
<select id="getPersonByGenderAndAage" resultType="Person">
select * from person where gender=#{arg0} and age>#{arg1}
</select>
public List<Person> getpersonbygenderAndAge( @Param("gen") String gender, @Param("ae") int age);
<select id="getPersonByGenderAndAage" resultType="Person">
select * from person where gender=#{gen} and age>#{ae}
</select>
方式三.将多个参数封装到已有实体类中
public List<Person> getpersonbygenderAndAge(Person person);
<select id="getPersonByGenderAndAage" resultType="Person">
select * from person where gender=#{gender} and age>#{age}
</select>
方式四.将多个参数封装到Map集合中
public List<Person> getpersonbygenderAndAge(Map map);
<select id="getPersonByGenderAndAage" parameterType="java.util.Map" resultType="Person">
select * from person where gender=#{gender} and age>#{age}
</select>
#{}和${}的区别
${}用在sql语句中获取字符串的值,有SQL注入的问题,存在安全隐患
#{}是占位符,利用preparestatement的set方法赋值,防止SQL注入.
#{}不能放到字符串里用来like模糊查询,也不能查询表名/字段名,但是#{}可以替代${}的特殊用法
原文:https://www.cnblogs.com/zhonggongjavastudy/p/13591418.html