一、接口綁定:把mapper.xml的sql語句綁定到mapper.java接口中的方法中
-
mybatis.xml:
-
<mappers>
-
<package name="com.mybatis.mapper"/>
-
</mappers>
-
StudentMapper.xml:
-
<select id="selOne" resultType="Student">
-
select * from stu where id=#{0} and name=#{1};
-
</select>
-
StudentMapper.java:
-
public interface StudentMapper {
-
Student selOne(int id,String name);
-
}
-
Test.java:
-
StudentMapper sm = session.getMapper(StudentMapper.class);
-
Student stu = sm.selOne(2,"lisi");
二、調用接口中的方法,使用索引傳遞多個參數
-
或者在#{}中寫@Param("內容") 參數中的內容
-
Student selOne(@Param("i") int id,@Param("n") String name);
-
select * from stu where id=#{i} and name=#{n};
-
此時,底層使用map實現,"i"作為key,id的值作為value。