session.getMapper()方法的使用:
定義接口
/**
* 查詢所有
* @return
*/
public List<Student> getAll();
SQL映射文件,做如下設置
<mapper namespace="cn.happy.dao.IStudentDAO">
<select id="getAll" resultMap="studentMap" useCache="false">
select * from student
</select>
</mapper>
測試類:
/**
* 查詢所有
*/
@Test
public void getAll() throws Exception{
SqlSession session = MyBatisUtil.getSession();
IStudentDAO mapper = session.getMapper(IStudentDAO.class);
List<Student> all = mapper.getAll();
for (Student item:all){
System.out.println(item.getId()+ item.getName()+ item.getAge());
}
//提交事務
session.commit();
//關閉會話,釋放資源
session.close();
}
resultMap實現結果映射