值得注意的地方:
• 理解Mapper.xml配置文件和Dao层方法,以及主程序之间的调用关系。
• 在运行时,如果遇到类似某某方法没找到,极有可能是Dao层实现类里的方法在调用Mapper.xml文件里的Sql语句时没有和其id保持一致。
• 用Mapper动态代理可以不用我们写Dao层实现类,MyBatis会帮我们写。但这里必须保证Mapper.xml里的sql语句id名和Dao层的接口方法名保持一致。我们还是要自己把selSeesion对象创建出来的,以及它的关闭。(如果是test的话,可以用@Before来创建sqlSession对象,用@After关闭它。
private SqlSession sqlSession;
private StudentDao studentDao;
@Before
public void initStudentDao() {
sqlSession = MybatisUtil.getSession();
studentDao = sqlSession.getMapper(StudentDao.class);
}
@After
public void closeSqlSession() {
if(sqlSession != null) {
sqlSession.close();
}
}
• 用Mapper动态代理的话,Mapper.xml里的<mapper>标签里的namespace要是Dao层接口的全限定名。例如:<mapper namespace="com.bzw.dao.StudentDao">