Miscellaneous-java:mybatis

Source: Internet
Author: User

ylbtech-Miscellaneous-java:mybatis

1. back to top
1, MyBatis is one of the Apache Open Source Project Ibatis, the project was migrated from Apache Software Foundation to Google code in 2010 and renamed MyBatis. Migrated to GitHub in November 2013. The word ibatis comes from the combination of "Internet" and "Abatis", is a Java-based persistence layer Framework。 Provided by Ibatis Persistence Layer FrameworkIncluding SQL maps and Data Access Objects (DAOs)2.
2. back to top
1. Basic InformationMyBatis is an excellent persistence layer framework, it supports custom SQL, stored procedures, and advanced mapping。 MyBatis avoids almost all JDBC code and manually setting parameters and getting result sets。 MyBatis can use a simple XML or annotations to configure and map native informationWill interface and Java POJOs(Plain ordinary Java object, plain Java objects) mapping to records in a databaseBackground IntroductionMyBatis is an excellent persistence layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setting of almost all JDBC code and parameters and the retrieval of the result set. MyBatis uses simple XML or annotations for configuration and raw mapping, mapping interfaces and Java POJOs (Plain ordinary Java Objects, plain Java objects) to records in a database. Each MyBatis applications are primarily using the Sqlsessionfactory instance, a Sqlsessionfactory instance can be obtained by Sqlsessionfactorybuilder。 Sqlsessionfactorybuilder can be obtained from an XML configuration file or an instance of a pre-defined configuration class。 Building an Sqlsessionfactory instance with an XML file is a very simple thing to do. Recommended for use in this configuration Class Path Resourceclasspath Resource), but you can use any instance of reader, including using An instance of a URL created at the beginning of a file path or file://。 MyBatis has a utility class----resources, which has many ways to easily load assets from classpath and other locations. features

  • Easy to learn: It's small and simple . No third-party dependencies, the simplest installation as long as two jar files + configuration of several SQL mapping files easy to learn, easy to use, through the documentation and source code, you can compare the complete grasp of its design ideas and implementation.
  • Flexible: MyBatis does not impose any impact on the existing design of an application or database. SQL is written in XML to facilitate unified management and optimization . With SQL you can basically implement all the features that we can do without using the data access framework, perhaps more.
  • Decoupling SQL from program code: By providing a DAO layer that separates business logic from data access logic, the system is designed to be clearer, easier to maintain, and easier to unit test. The separation of SQL and code improves maintainability.
  • Provides mapping labels to support ORM Field relationship mapping for objects and databases
  • Provides object-relational mapping tags to support object-relational build maintenance
  • Provides XML tags that support writing dynamic sqL.

Overall process

(1) Load configuration and initialize trigger conditions: Load configuration fileProcess: Load the SQL configuration information into one Mappedstatement Object(Including the Incoming parameter mapping configuration, executed SQL statement, result mapping configuration), stored in memory。 (2) Receive call request trigger condition: invoke the API provided by MyBatisIncoming parameters: For SQL ID and incoming Parameter object processing: passes the request to the underlying request processing layer for processing. (3) Processing operation request Trigger conditions: API Interface Layer delivery request come overIncoming parameters: For SQL ID and incoming Parameter object processing: (A) based on the SQL IDFinds the corresponding Mappedstatement object. (B) Parse the Mappedstatement object based on the incoming parameter object to get the final SQL to execute and execute the incoming parameters. (C) Obtaining a database connection, based on the resulting SQL statement and executing the incoming parameters into the database execution, and get the results of the implementation。 (D) Conversion processing of the resulting execution results based on the result mapping configuration in the Mappedstatement object and the resulting processing results. (E) Release the connection resources. (4) The return processing result returns the final processing result. Functional architecture We divide the MYBATIS function structure into three layers: (1) API Interface Layer: interface APIs for external use, which developers Local API to manipulate the database。 Once the interface layer receives the call request, it invokes the data processing layer to complete the specific data processing. (2) Data Processing Layer: Responsible for the specific SQL Lookup, SQL parsing, SQL execution, and execution result mapping processingsuch as Its primary purpose is to complete a database operation at the request of the call. (3) Base Support Layer: Responsible for the most basic functional support, including connection management, transaction management, configuration loading, and cache processing, these are common things that are extracted from them as the most basic components. Provides the most basic support for the data processing layer of the upper layer. Framework ArchitectureFrame Architecture explained: (1) Load configuration: Configuration from two placesOne is the configuration file, one is the Java code annotation, the SQL configuration information is loaded into a Mappedstatement object (including the Incoming parameter mapping configuration, executed SQL statement, result mapping configuration), stored in memory. (2) SQL parsing: When the API interface layer receives a call request, it receives the ID of the incoming SQL and the incoming object (which can be Map, JavaBean, or basic data type), MyBatis will find the corresponding mappedstatemen based on the SQL IDT, then the mappedstatement is parsed according to the incoming parameter object, and after parsing, we can get the final SQL statements and Parameters。 (3) SQL execution: Get the resulting SQL and parameters to the database for execution, get the results of the operational database。 (4) Result mapping: The results of the operation database are converted according to the mapped configuration and can be converted into HashMap, JavaBean, or basic data type, and returns the final result

Dynamic SQLOne of the most powerful features of MyBatis is its Dynamic Statement functionality。 If you have experienced using JDBC or similar frameworks before, you will understand how painful it is to connect SQL statement conditions together, make sure you can't forget the spaces or omit a comma after the columns column, and so on. Dynamic statements can completely solve the pain. Although working with dynamic SQL is not a party, MyBatis does improve these conditions by using powerful dynamic SQL in any mapped SQL statement. Dynamic SQL elements are very familiar to anyone who has used Jstl or a text processor like XML. In the previous release, it was necessary to understand and learn a very large number of elements, but there were many improvements in MyBatis 3, and now there are only about one-second elements left. MyBatis uses a powerful OGNL expression to eliminate most of the elementsIntegratedThere are many limitations to using mybatis alone (such as the inability to implement transactions that span multiple sessions), and many business systems are inherently transactions that are managed using spring, so mybatis is best integrated with spring [4]. Spring Transaction Configuration
<!--automatic scanning of business packages -<Context:component-scanbase-package= "Com.xxx.service"/><!--Data Source -<Jee:jndi-lookupid= "Jndidatasource"Jndi-name= "Java:comp/env/jdbc/datasource"/><!--Configuring Transactions -<Beanid= "Txmanager"class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager"><PropertyName= "DataSource"ref= "Jndidatasource"/></Bean><!--Configuring annotation-based things AOP -<Tx:annotation-driventransaction-manager= "Txmanager"Proxy-target-class= "true"/>
2.
3. back to top
4. back to top
5. back to top
0, 0.2, http://www.mybatis.org/mybatis-3/0.3, HTTP://WWW.MYBATIS.ORG/MYBATIS-3/ZH/INDEX.HTML1, https:// Baike.baidu.com/item/mybatis2,
6. back to top
Ylbtech
Source: http://ylbtech.cnblogs.com/
This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

MyBatis is an open source project for Apache Ibatis, which was migrated to Google code by the Apache Software Foundation in 2010 and renamed MyBatis. Migrated to GitHub in November 2013. The term ibatis is derived from the combination of "Internet" and "Abatis" and is a Java-based persistence layer framework. Ibatis provides a durable layer framework including SQL maps and Data Access Objects (DAOs)

Miscellaneous-java:mybatis

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.