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> 测试类:
/** * 查询所有 */@Testpublic 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实现结果映射
时间: 2024-11-14 12:53:16