Java-mybaits-00102-mybatis Framework Principle

Source: Internet
Author: User

1. What is MyBatis? MyBatis is a persistent layer framework and is the top-level project under Apache.  is an incomplete ORM framework.  MyBatis managed to Goolecode and later managed to GitHub (https://github.com/mybatis/mybatis-3/releases).  MyBatis allows the program to focus primarily on SQL, with free and flexible generation (semi-automated, mostly programmer-written SQL) to satisfy the need for SQL statements through the mapping provided by MyBatis. The MyBatis can automatically map the input parameters in the PreparedStatement to the Java object with a flexible mapping of the query result set. (output mapping) MyBatis is an open source project for Apache Ibatis, 2010, the project was migrated from Apache Software Foundation to Google Code and renamed MyBatis,  In essence mybatis some improvements to ibatis. MyBatis is an excellent persistence layer framework that encapsulates the process of working with databases in JDBC, allowing developers to focus on the SQL itself without having to expend effort to process such as registering drivers, creating connection, creating statement, setting parameters manually,  Result set retrieval and other JDBC complex process code. MyBatis the various statement (statement, PREPAREDSTATEMNT, CallableStatement) that will be executed by XML or annotations, The resulting SQL statements are generated by mapping Java objects and SQL in statement, and finally the SQL is executed by the MyBatis framework and the results are mapped to Java objects and returned.  2, MyBatis, and hibernate essential differences and application scenarios hibernate is a standard ORM framework (Object Relational mapping).    Entry threshold is higher, do not need the program write Sql,sql statement automatically generated.   It is difficult to optimize and modify SQL statements. Application Scenarios:        applications and small and medium-sized projects with little change in demand, such as: Backend management system, ERP, ORM, OA.   MyBatis focus is SQL itself, requires programmers to write their own SQL statements, SQL modification, optimization is more convenient. MyBatis is an incomplete ORM framework, and although the programmer writes Sql,mybatis himself, it can actuallyMap (input map, output map). Application scenario: Suitable for projects with more changes in demand, such as: Internet projects.   Note: Enterprises to carry out technology selection, low-cost high return as the principle of technology selection, according to the project team's technical strength to choose.   3. MyBatis Frame

  

    1. MyBatis Configuration
      Sqlmapconfig.xml, this file is configured as a global configuration file for the MyBatis, with information such as the MyBatis runtime environment.
      The Mapper.xml file is a SQL mapping file that is configured with SQL statements that manipulate the database. This file needs to be loaded in Sqlmapconfig.xml.
    1. Construct Sqlsessionfactory as session factory through configuration information such as MyBatis environment
    2. The sqlsession session is created by the session factory, and the operational database needs to be done through sqlsession. It is recommended to use sqlsession in the method body.
    3. MyBatis the bottom layer customizes the Executor actuator interface operation database, the executor interface has two implementations, one is the basic executor, the other is the cache executor.
    4. Mapped statement is also mybatis a lower-level package object, which wraps mybatis configuration information and SQL mapping information. One SQL in the Mapper.xml file corresponds to a mapped statement object, and the SQL ID is the ID of the mapped statement.
    5. Mapped statement defines SQL execution input parameters, including HashMap, base type, pojo,executor through Mapped statement to map the input Java objects into SQL before executing SQL. The input parameter mapping is the parameter set for PreparedStatement in JDBC programming.
    6. Mapped statement defines SQL execution output, including HashMap, base type, pojo,executor through Mapped statement to map output results to Java objects after executing SQL. The output mapping process is equivalent to the parsing process of the results in JDBC programming.
4, MyBatis solve the problem of JDBC Programming 1. Database link creation, release frequently results in system resource wasting which can affect system performance, if you use a database link pool to resolve this issue. Solve: Configure the data link pool in Sqlmapconfig.xml, and use connection pooling to manage database links. 2.SQL statements written in code cause code to be difficult to maintain, the actual application of SQL changes may be large, SQL changes need to change the Java code. Solve: The SQL statement configuration is separated from the Java code in the Xxxxmapper.xml file. 3. It is troublesome to pass arguments to the SQL statement because the WHERE condition of the SQL statement is not necessarily, possibly more or less, and the placeholder needs to correspond to parameter one by one. Solve: MyBatis automatically maps Java objects to SQL statements, defining the type of input parameters through ParameterType in statement. 4. Trouble with the result set parsing, SQL changes lead to parsing code changes, and need to traverse before parsing, if the database records can be encapsulated into Pojo object parsing is more convenient. Solve: MyBatis automatically maps SQL execution results to Java objects, defining the type of output results through Resulttype in statement.

Java-mybaits-00102-mybatis Framework Principle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.