对mybatis进一步加深了解

对mybatis功能测试(加载mybatis配置文件对所有用户信息进行查询并遍历)

//1.加载mybatis配置文件:   mybatis/mybatis.xml
InputStream in =
Resources.getResourceAsStream(
"mybatis/mybatis.xml");
//2.获取SqlSessionFactory工厂
SqlSessionFactory factory =
new SqlSessionFactoryBuilder()
.build(in);
//3.通过工厂获取SqlSession对象
SqlSession session = factory.openSession();
//4.获取UserMapper接口的实例
DoorMapper mapper = session.getMapper(DoorMapper.class);
//5.调用findAll方法查询所有用户信息
List<User> list = mapper.findAll();
//6.遍历所有用户信息
for(User user:list){
System.out.println(user);
}

posted @ 2020-03-02 10:39  唯恐不及  阅读(107)  评论(0编辑  收藏  举报