接口式编程

namespace与接口名相同,id与接口中的方法名相同

// 1、获取sqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
// 2、获取sqlSession对象
SqlSession openSession = sqlSessionFactory.openSession();
try {
// 3、获取接口的实现类对象,mapper接口没有实现类,但是mybatis会为这个接口生成一个代理对象。
//会为接口自动的创建一个代理对象,代理对象去执行增删改查方法
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(mapper.getClass());
System.out.println(employee);
} finally {
openSession.close();
}

}

猜你喜欢

转载自www.cnblogs.com/cn-chy-com/p/9186542.html
今日推荐