In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article shares with you the content of a sample analysis of Mybatis-related concepts. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
1.1 traditional JDBC implementation public static void main (String [] args) {Connection connetion = null; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try {/ / load database driver Class.forName ("com.mysql.jdbc.Driver"); connetion = DriverManager.getConnetion ("jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8", "root", "root"); / / define SQL statement String sql = "Select * from user where username =?" / / get preprocessing statement preparedStatement = connection.preparedStatement (sql); preparedStatement.setString (1, "zbc"); resultSet = preparedStatement.executeQuery (); while (resultSet.next ()) {int id = resultSet.getInt ("id"); String userName = resultSet.getString ("username"); / / Encapsulation User user = new User (); user.setId (id) User.setUserName (userName)}} catch (Excetption e) {e.printStatckTrace ();} finally {/ / release Resources}} 1.2 problems and Analysis
There is a hard coding problem in database configuration.
Solution: solve the problem of hard coding of configuration information through configuration files
Create release database connections frequently
Solution: solve the problem of frequent creation and release through connection pooling
There are hard coding problems in SQl statements, parameters and getting result set parameters.
Solution: solve SQL statement and parameter problems through configuration files
The returned result needs to be encapsulated manually, which is cumbersome.
Solution: solve the set through reflection
1.3 Mybatis profile official website
Mybatis is an excellent semi-automatic lightweight persistence framework based on ORM that supports customized SQL, stored procedures, and advanced mapping. Mybatis avoids almost all JDBC code and manually setting parameters and getting result sets. Mybatis can use simple XML or annotations to configure and map native types, interfaces, and Java's POJO (Plain Old Java Objects, plain old Java objects) to records in the database.
1.4 how it works
Load mybatis-config.xml configuration files (data sources, mapper files, and related other files)
Parse the mybatis-config.xml file through SqlSessionFactoryBuilder.build () (using builder mode), and save the parsed data to the configuration object
SqlSessionFactory creates the SqlSession through the configuration object, and then performs CURD and transaction commit operations through the SqlSession. (the process of creating a sqlSession is to create the corresponding class according to the configuration in configuration, and then return the created sqlSession object.)
1.5 Mybatis pros and cons
Advantages
a. Simple and easy to learn
Mybatis itself is small and simple. Without any third-party dependence, the simplest installation is as long as two jar files + configuration several sql mapping files are easy to learn and use. Through documentation and source code, you can fully grasp its design ideas and implementation.
b. Flexible
Mybatis does not impose any impact on the existing design of the application or database. Sql is written in xml for unified management and optimization. With sql, you can basically do everything we can do without using the data access framework, or more.
c. Uncoupling sql from program code
By providing DAL layer, the business logic and data access logic are separated, which makes the design of the system clearer, easier to maintain and easier to unit test. The separation of sql and code improves maintainability.
Shortcoming
a. It takes a lot of work to write SQL statements, especially when there are many fields and associated tables.
B. the SQL statement depends on the database, which leads to the poor portability of the database and cannot replace the database.
c. The framework is still relatively simple, the function is still missing, although it simplifies the data binding code, but the entire underlying database query actually has to be written on its own, the workload is also relatively large, and it is not easy to adapt to rapid database modification.
1.6 Mybatis and Hibernate
Hibernate is a fully automatic framework based on ORM, with strong object / relational mapping ability, and does not need developers to write SQL statements (and can not optimize SQL statements at the same time); Mybatis is based on ORM semi-automatic, can strictly control the performance of SQL execution, high flexibility.
Thank you for reading! This is the end of this article on "sample Analysis of Mybatis-related concepts". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.