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實現結果映射

 

 

 

 
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM