H__D  

一、Mybatis使用

  1、Mybatis源码编译参考:【Mybatis】MyBatis源码编译

  2、Mybatis使用参考:【Mybatis】MyBatis快速入门(一)

二、Mybatis原理

1、Mybatis相关类的类图

  

2、Mybatis执行过程

  • 分析过程代码
     1 @Test
     2 public void test1() throws IOException {
     3 
     4     // 1、根据mybatis全局配置文件,获取SqlSessionFactory
     5     String resource = "mybatis-config.xml";
     6     // 使用MyBatis提供的Resources类加载mybatis的配置文件,获取输入流
     7     InputStream inputStream = Resources.getResourceAsStream(resource);
     8     // 构建sqlSession的工厂
     9     SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    10 
    11     // 2、从SqlSession工厂中,获取sqlsession,用来执行sql
    12     SqlSession session = sqlSessionFactory.openSession();
    13     try {
    14         // 查询selectOne
    15         // @param statement Unique identifier matching the statement to use.      一个唯一标识
    16         // @param parameter A parameter object to pass to the statement.        参数
    17         Employee employee = (Employee) session.selectOne("com.test.mapper.EmployeeMapper.getEmployeeById", 1);
    18 
    19         Employee employee2  = session.getMapper(EmployeeMapper.class).getEmployeeById(1);
    20         // 输出信息
    21         System.out.println(employee);
    22         System.out.println(employee2);
    23     } finally {
    24         // 关闭session
    25         session.close();
    26     }
    27 }

 

  • 执行过程图

    

  

 

posted on 2021-05-11 22:31  H__D  阅读(141)  评论(0编辑  收藏  举报