What are the MyBatis programming steps?

What are the problems with traditional JDBC development?
Frequent creation and release of database connection objects can easily cause waste of system resources and affect system performance. You can use connection pooling to solve this problem. But to use jdbc, you need to implement the connection pool yourself.
There are hard-coded SQL statement definitions, parameter settings, and result set processing. In the actual project, the possibility of sql statement changes is greater. Once a change occurs, the java code needs to be modified, and the system needs to be recompiled and republished. Not easy to maintain.
Using preparedStatement to pass parameters to the occupied position symbol is hard-coded, because the where conditions of the SQL statement are not necessarily, there may be more or less, modify the SQL and modify the code, the system is not easy to maintain.
There are duplicate codes in the result set processing, which is troublesome. It will be more convenient if it can be mapped to a Java object.

What are the shortcomings of JDBC programming and how does MyBatis solve it?

1. Frequent database link creation and release cause waste of system resources and affect system performance. This problem can be solved if a database connection pool is used.
Solution: Configure the data link pool in mybatis-config.xml and use the connection pool to manage database connections.
2. Sql statement written in the code makes the code difficult to maintain, and the actual application of sql may change greatly, and sql changes need to change the java code. -
Solution: Configure the Sql statement separate from the java code XXXXmapper.xml file.
3. It is troublesome to pass parameters to the sql statement, because the where condition of the sql statement is not necessarily, there may be more or less, and the placeholders need to correspond to the parameters one by one.
Solution: Mybatis automatically maps java objects to sql statements.
4. It is troublesome to parse the result set. SQL changes cause the parsing code to change, and it needs to be traversed before parsing. It is more convenient if the database records can be encapsulated into pojo object parsing.
Solution: Mybatis automatically maps sql execution results to java objects.

What are the applicable scenarios of MyBatis and Hibernate?
MyBatis focuses on SQL itself and is a sufficiently flexible DAO layer solution.
For projects that have high performance requirements or changes in demand, such as Internet projects, MyBatis will be a good choice.
Development difficulty and learning cost
Hibernate is a heavyweight framework with a high learning threshold, suitable for small and medium-sized projects with relatively stable demand, such as: office automation system
MyBatis is a lightweight framework with low learning threshold, suitable for changes in demand Frequent, large-scale projects, such as: Internet e-commerce system
Summary
MyBatis is a small, convenient, efficient, simple, direct and semi-automated persistence layer framework,
Hibernate is a powerful, convenient, efficient, complex, indirect, fully automated persistence Layer frame.
The architecture of MyBatis

What are the MyBatis programming steps?
1. Create SqlSessionFactory
2. Create SqlSession through SqlSessionFactory
3. Perform database operations through sqlsession
4. Call session.commit() to commit the transaction
5. Call session.close() to close the session

Guess you like

Origin blog.csdn.net/qq_42918433/article/details/113913957