Mybatis的多参传递方式主要有2种
方式一:在接口中声明方法
import com.asiainfo.pojo.Student; public interface StudentMapper { public Student selectStudentByIdAndName(Integer id, String name); }
在mapper.xml中参数使用#{}中使用012,或param1,param2
<!-- 当多参数时,不需要写 parameterType --> <select id="selectStudentByIdAndName" parameterType="int" resultType="student"> select id,name,sex,age from student where id=#{0} and name=#{1} <!-- select id,name,sex,age from student where id=#{param1} and name=#{param2} --> </select>
方式二:在接口中使用注解
import org.apache.ibatis.annotations.Param; import com.asiainfo.pojo.Student; public interface StudentMapper { public Student selectStudentByIdAndName(@Param("id1")Integer id, @Param("name1")String name); }
在mapper.xml中添加,#{}里的内容是接口参数中@Param(“key”)的key内容
<select id="selectStudentByIdAndName" resultType="student"> select id,name,sex,age from student where id=#{id1} and name=#{name1} </select>