Technology Sharing

Interview Question 009-Java-MyBatis

2024-07-11

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

Interview Question 009-Java-MyBatis

Self-test

  • 1. What is MyBatis? How is it different from Hibernate?
  • 2. Tell me about the execution process of MyBatis?
  • 3. Does MyBatis support lazy loading?
  • 4. What is the difference between the first-level cache and the second-level cache in MyBatis?
  • 5. What is dynamic SQL in MyBatis?
  • 6. How to implement paging in MyBatis?
  • 7. How does MyBatis insert large-scale data into a MySQL database?
  • 8. Do you know MyBatis-Plus?

Question Answer

1. What is MyBatis? How is it different from Hibernate?

A: MyBatis is a semi-automatic persistence layer framework that can write SQL statements through XML or annotations and map SQL statements to Java objects. MyBatis does not completely automatically generate SQL statements, but allows developers to manually write SQL, which provides greater flexibility and control.
Hibernate is a fully automatic ORM framework that can automatically generate SQL statements and provide more functions, but its configuration is relatively complex.

2. Tell me about the execution process of MyBatis?

A: The execution process of MyBatis includes several main steps, from configuration initialization to executing SQL and returning results. The detailed process is as follows

  1. Load configuration file: record the configuration file (such as mybatis-config.xml), which contains database connection information, mapping file location and other information.
  2. Create SqlSessionFactory: Create a SqlSessionFactory instance through the SqlSessionFactoryBuilder constructor.
  3. Create SqlSession: Get the SqlSession instance through SqlSessionFactory, which provides the methods required to execute SQL statements.
  4. Get Mapper: Get a specific Mapper instance through SqlSession.
  5. Execute SQL: Call the Mapper interface method, MyBatis generates and executes specific SQL statements based on the mapping information in the configuration file.
  6. Processing result sets: MyBatis maps the result sets returned by the database into Java objects and returns them to the caller.
  7. Transaction management: If a transaction manager is configured, SqlSession will handle the start, commit, or rollback of transactions.
  8. Close SqlSession: release resources.
    // 1. 加载配置文件
    String resource = "mybatis-config.xml";
    InputStream inputStream = Resources.getResourceAsStream(resource);
    
    // 2. 创建SqlSessionFactory
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    
    // 3. 创建SqlSession
    try (SqlSession session = sqlSessionFactory.openSession()) {
        
        // 4. 获取Mapper
        UserMapper mapper = session.getMapper(UserMapper.class);
    
        // 5. 执行SQL
        User user = mapper.selectUser(1);
    
        // 6. 处理结果集
        System.out.println(user);
    
        // 7. 管理事务(如果需要)
        session.commit();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 8. 关闭SqlSession
        session.close();
    }