[Studies] MyBatis Framework

명원식·2023년 8월 14일
0

Studies

목록 보기
6/9
post-thumbnail

When the scale of an application was small, using JDBC was sufficient for development. However, as the number of internet users exploded and application functionality became more complex, the limitations of developing with traditional JDBC became apparent.

When developing with the traditional JDBC approach, not only did developers have to implement numerous repetitive SQL statements, but the SQL queries themselves also became intricate. As a result, naturally, database-related frameworks like MyBatis or Hibernate emerged.

As the number of internet users surged and application functionality grew more complex, the limitations of developing with traditional JDBC became apparent. Developers were required to implement numerous repetitive SQL statements, and the complexity of these queries increased. As a result, database-related frameworks like MyBatis or Hibernate naturally emerged.

The traditional process of integrating JDBC involves:
1. Establishing a connection
2. Creating a Statement object
3. Sending SQL queries
4. Retrieving results
5. Closing resources

Disadvantages of this approach include:

  • Mixing SQL queries within the programming code, leading to code complexity.
  • Lack of readability and ease of use for SQL queries.

To address these issues, the MyBatis framework was developed.

As application functionality becomes more complex, the associated SQL queries tend to become several lines long. Handling lengthy SQL queries as plain strings can be inconvenient. This approach leads to frequent errors during SQL execution and poses maintenance challenges.

With MyBatis, even complex SQL queries can be used in a standardized way, similar to how SQL statements are used in tools like SQL Developer. This not only simplifies developer tasks but also enhances code readability.

Characteristics of the MyBatis framework include:

  • It serves as a Persistence solution that maps SQL execution results to Java beans or Map objects.
    - This means that SQL queries are separated from the source code and are stored in XML files.
  • It separates SQL statements from the application code.
  • It provides features for managing data sources (DataSource) and transaction processing.

MyBatis Framework Structure

  1. Define SQL Queries in Configuration Files:
    • Prepare SQL queries for various functionalities in separate SqlMap.xml files.
    • Associate these queries with specific actions or operations in the SqlMapConfig.xml file.
  2. Pass Data to MyBatis:
    • In your application, gather data required for database interaction and store it in individual parameters.
    • Pass these parameters to MyBatis for further processing.
  3. Select SQL from Configuration:
    • When your application requests a specific action, MyBatis selects the corresponding SQL query from the SqlMap.xml based on the configuration.
  4. Combine Parameters and SQL:
    • Combine the passed parameters and the chosen SQL query to create a complete SQL statement that is ready for execution.
  5. Execute SQL on DBMS:
    • Execute the SQL statement, which now includes the relevant data, on the Database Management System (DBMS).
  6. Store Returned Data:
    • After the DBMS executes the SQL, it returns the resulting data.
    • Store this data in appropriate parameters provided by your application.
  7. Return Data to Application:
    • Provide the stored data back to your application, allowing it to use and process the retrieved information as needed.

In short:
When connecting to a database, pre-written SQL queries are stored in SqlMapConfig.xml. When the application interacts with the database, the required data for these SQL queries is stored in corresponding parameters. These parameters are subsequently passed to the associated SQL queries.

The passed parameters and SQL queries are combined to form an SQL statement, which is sent to the DBMS for execution. The outcome of the execution is returned to the application, formatted according to the specified data types.

profile
but i brought potato salad

0개의 댓글