In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how did MyBatis come from". In daily operation, I believe many people have doubts about how MyBatis came from. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "how did MyBatis come from?" Next, please follow the editor to study!
JDBC native operation
When there was no ORM framework in the early development, we had to operate the database through JDBC.
JDBC test code:
/ * * Native operation JDBC mode * @ Author: maomao * @ Date: 2021-04-07 10:59 * / public class JdbcTest {public static final String URL = "jdbc:mysql://127.0.0.1:3306/data_test?useUnicode=true&characterEncoding=utf-8&rewriteBatchedStatements=true"; public static final String USER_NAME = "root"; public static final String PASSWORD = "123456"; @ Test public void testJdbc () {Connection connection = null; Statement statement = null ResultSet resultSet = null; try {/ / Open the connection connection = DriverManager.getConnection (URL,USER_NAME,PASSWORD); / / execute SQL statement = connection.createStatement (); String sql = "select * from user_department where id = 1"; resultSet = statement.executeQuery (sql) / / get the result set and encapsulate it in the java object while (resultSet.next ()) {User user = new User (); user.setId (resultSet.getLong ("id")); user.setName (resultSet.getString ("name")); user.setAge (resultSet.getInt ("age")) System.out.println (user);} / / follow-up use logic. } catch (SQLException e) {e.printStackTrace ();} finally {/ / close the resource try {if (resultSet! = null) resultSet.close ();} catch (SQLException se2) {} try {if (statement! = null) statement.close () } catch (SQLException se2) {} try {if (connection! = null) connection.close ();} catch (SQLException se) {se.printStackTrace ();}
Mainly divided into the following steps:
Register the database driver
Get a Connection connection
Create a Statement object
Execute the execute () method, execute sql, and get the ResultSet result set
Get data through ResultSet, assign values to POJO, and convert them to java objects
Finally, close the database related resources, including ResultSet, Statement, Connection
If you are lucky enough to be exposed to some early old projects, you may see a lot of such operations in the project Dao layer.
A little better will encapsulate these operations in the database as a tool, and if not, you will see that there will be something similar to the above test code in each query method.
You can imagine that every regular database operation, such as byId (), save (), update (), and so on, used to be wrapped in test code like the one above (shivering).
ORM framework
ORM (Object Relational Mapping), that is, object and relation mapping.
One of the more mainstream ORM frameworks born in the early days is Hibernate, which gave birth to the first version in 2001.
It helps us to solve the complex object-relational mapping problem in the period of JDBC and simplifies the database development operation. So that developers no longer have to pay attention to database connections, objects and database table mapping transformation. So the early mainstream development of J2EE started with SSH (struts + spring + hibernate).
Hibernate itself is very powerful, and it has its own query syntax. HQL can generate corresponding SQL statements by operating object relations, and it can also generate SQL compatible with various database scenarios according to the database dialect, so it has good portability. Automatically manages connection resources and provides a caching mechanism.
So hibernate undoubtedly became the mainstream ORM framework when J2EE was developed at that time.
However, Hibernate also has some problems in complex business projects:
Need to learn the new HQL syntax
The way to automatically generate SQL, if you need to do some optimization based on SQL, is very difficult, resulting in performance optimization is a problem.
Dynamic SQL is not supported, and SQL cannot be generated automatically according to conditions.
Based on the above problems, we need a more flexible framework. This is where MyBatis (early iBatis) was born.
MyBatis
MyBatis official website address
The "semi-automatic" ORM framework MyBatis solves the above problems. "semi-automation" is equivalent to the full automation of Hibernate. Its encapsulation degree is not as high as that of Hibernate, and it does not automatically generate all SQL statements, but mainly solves the problem of mapping SQL and objects.
In MyBatis, sql and code are separate, so you can basically use MyBatis to write SQL, with no additional learning costs.
So J2EE ushered in several more architectural changes.
SSI (Struts2 + Spring + iBatis)
SSM (Spring MVC + Spring + MyBatis)
MyBatis has gradually replaced Hibernate as the mainstream ORM framework with the characteristics of compact, easy to learn, flexible, sql and code decoupling.
At this point, the study of "how did MyBatis come from" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.