image.png
    image.png

    1. @Test
    2. public void testSelectById() throws Exception {
    3. // 接受参数
    4. int id = 1;
    5. // 1. 获取SqlSessionFactory
    6. // 1. 加载mybatis的核心配置文件,获取SqlSessionFactory
    7. // 1. 加载mybatis的核心配置文件,获取SqlSessionFactory (sql会话工厂)
    8. String resource = "mybatis-config.xml";
    9. InputStream inputStream = Resources.getResourceAsStream(resource);
    10. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    11. // 2. 获取SqlSession对象
    12. SqlSession sqlSession = sqlSessionFactory.openSession();
    13. //3. 获取Mapper接口的代理对象
    14. BrandMapper brandMapper = sqlSession.getMapper(BrandMapper.class);// 将该接口的的class对象,传进去,就可以获得该借口的代理对象
    15. // 4. 执行方法:
    16. Brand brand = brandMapper.selectById(id);
    17. System.out.println(brand);
    18. // 5. 释放资源
    19. sqlSession.close();
    20. }