mybatis中session.getMapper()方法和resultMap

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实现结果映射

 
 
原文地址:https://www.cnblogs.com/sujulin/p/7613068.html