Java EE MyBatis

Source: Internet
Author: User

1. Introduction

MyBatis, a ibatis version of Apache's Open source project, migrated to GitHub in November 2013 and is a durable layer framework in a three-tier architecture.

Currently available in Java,. NET, and Ruby in three languages.

2, to provide a "semi-automated" ORM implementation. This semi-automation is a "fully automated" ORM implementation that provides a comprehensive database encapsulation mechanism relative to hibernate, and the "fully automatic" ORM implements the mapping between Pojo and database tables, as well as the automatic generation and execution of SQL, and Mybaits's focus is the mapping between Pojo and SQL.

3. The difference between JavaBean EJB POJO entity

JavaBean: A Java class written in compliance with the JavaBean specification, and objects instantiated by that class become JavaBean objects. The purpose of writing JavaBean is to provide reusable components for the Web container, improving the encapsulation of data and reducing coupling.

EJB: In enterprise applications, you need scalable performance and transactions, security mechanisms, and so on to ensure the needs of enterprise-class applications. EJB is the higher protocol requirement based on the JavaBean specification. It is more complex and more powerful than JavaBean, which is the core component of Java EE.

POJO (Plain ordinary Java object): Simple Java objects. In enterprise applications, the discovery of the use of EJB exceptions is cumbersome and complex, it is not necessary to use each time, so the concept of Pojo, the purpose is to avoid and EJB confusion created by the abbreviation. The so-called Pojo refers to the ordinary JavaBean, which is relative to the EJB, but it is more pure than JavaBean, only the attribute and Get/set method, and can only load data, as the carrier of data storage, but not the business logic processing ability.

Entity classes are primarily classes that exist at the data management and business logic processing level and are used to model the information that must be stored and the related behavior, typically in relation to database tables. It is important to note that entity classes involve layers of three-tier architectures, which are generally categorized into the business logic layer.

4. ORM (Object/relation Mapping object/relational database mapping): It is a specification that describes the basic characteristics of the mapping between an object and a relational database.

ORM Framework: It accomplishes the mapping of object-oriented programming language to relational database by implementing ORM specification. It can encapsulate the relational database as an object, and solve the problem that the object-oriented programming language and the underlying relational database are uncoordinated. It is the bridge between object-oriented programming language and relational database.

ORM Mapping Basic Rules:

    • classes correspond to tables, and in normal circumstances a class corresponds to a table;
    • The properties of the class (member variables) correspond to the fields of each column of the table;
    • The instance of the class corresponds to a specific row of records in the table;
    • The table of the database can have no primary key, but the primary key field must be set in the instance of the class;
    • A table can correspond to multiple entity classes, and an entity class can also correspond to multiple tables;
    • The relationship between tables and tables in the database is mapped to the relationship between the objects;
    • The number and name of the attributes in the object may not be the same as the number and name of the fields in the table;

5. Environment configuration

    • Import the jar package of the mybaits framework;
    • Create a basic configuration file under the SRC root of the project Sqlmapconfig.xml;
    • Write Sqlmapconfig.xml file configuration mybaits framework environment;
    • Create a database table;
    • Writing database entity classes;
    • Write the SQL Mapper interface class;
    • Write the SQL mapping file (the mapping file is the same directory as the Mapper interface file);
    • Write the test class call Mapper interface method;

6. Common configuration file elements:

Settings: Used to set the MyBatis runtime mode, such as whether to enable lazy loading, etc.;

Typealiases: Specifies an alias for the Java type, which can be used to replace the fully qualified name of the Java class in the XML file;

Environments: Used to configure data source information, including connection pooling, transaction properties, etc.;

Mappers: All the SQL mapping files used in the program are listed here, these mapping SQL are mybatis managed;

7. Data source

Data sources are a common means of improving database connection performance, and the data source is responsible for maintaining a pool of data connections that, when the program creates the data source instance, creates multiple database connections at one time and saves those database connections in the connection pool. Typical space-changing times to improve performance.

8. SQL mapping File

Mybaits uses a JDBC SQL statement to save to the SQL mapped XML file to complete the mapping between Pojo and SQL.

There are few top-level elements in the SQL mapping file:

Select: Map query statements;

Insert: Map insertion statement;

Update: Map updated statement;

Delete: Map DELETE statement;

SQL: SQL blocks that can be reused, or can be referenced by other statements;

Resultmap: Used to describe how to load objects from a database result set (the most complex);

Cache: Configures caching for a given namespace;

Cache-ref: Reference the cache configuration from another namespace.

The Resultmap element is the most important and powerful element in Mybaits. Its design is that simple statements do not require explicit result mappings, and many complex statements do need to describe their relationships.

It has a number of child elements:

    • ID: Used to mark the primary key;
    • Result: A normal result injected into a field or Pojo property;
    • Association: Used for complex types of association relationships;
    • Collection: An association relationship for a collection type;
    • Discriminator: Use the result value to determine which result map to use for the inheritance relationship.

9. SQL Mapper Interface

Used to invoke SQL statements in the SQL mapping file, only interfaces, no implementation classes.

ThreadLocal

As early as in the JDK1.2 version of the threadlocal, to solve the problem of multi-threaded program concurrency provides a new way of thinking. The use of threadlocal can be very concise to write elegant multithreaded programs.

Threadlocal It is not a thread, but rather a localized object of the thread. When an object working on a thread uses threadlocal to save a variable, threadlocal assigns a separate copy of the variable to each thread that uses the variable. So each thread can change its own copy independently, without affecting the copy of the other thread. From the thread's point of view, this variable is like the local variable of the thread, which is also the meaning that local in the class name wants to express.

Thread-local variables are not a new Java invention, and many languages provide thread-local variables at the syntactic level. Language-level support is not provided in Java, but is supported in a flexible way through the Threadlocal class.

Thread and thread synchronization mechanisms are all designed to solve the problem of access violation of the same variable in multi-threading, so the threadlocal has the following characteristics compared to the thread synchronization mechanism:

    • In the synchronization mechanism, through the lock mechanism of the object to ensure that there is only one thread access variable at the same time, this variable is shared by multiple threads, the use of synchronization mechanism requires the program to know when to read and write variables, when the need to lock an object and other complex problems, programming and writing is relatively difficult.
    • Threadlocal the concurrent access to multithreading from another angle. Threadlocal provides a separate copy of the variable for each thread, so there is no need to synchronize the variables. Threadlocal provides a thread-safe object encapsulation that can encapsulate unsafe variables into threadlocal when writing multithreaded code.
    • For the problem of multi-threaded resource sharing, the thread synchronization mechanism adopts the way of "time-changing Space": Access serialization, object sharing, and Threadlocal adopts "space-time" approach: Access Parallelization, Object-Exclusive. The former provides only one copy of the variable, while different threads queue access; the latter provides a variable for each thread, so it can be accessed at the same time without affecting each other.

MyBatis Association Relationship

One to one, <association property= "" column= "" javatype= "" parametertype= "" select= ""/>

One-to-many, <collection property= "" column= "" javatype= "" parametertype= "" select= ""/>

Inheritance associations:

(discriminator)

<discriminator javatype= "" column= "" >

<case value= "" Resulttype= "" >

<result property= "" column= ""/>

</case>

</discriminator>

MyBatis Dynamic SQL

One of the most powerful features of MyBatis is its dynamic SQL capabilities. The main elements for implementing SQL are: If,choose,trim,foreach.

<if test= "" ></if>

<choose>

<when test= "" ></when>

<otherwise></otherwise>

</choose>

<set>

<if test= "" ></if>

<set>

Java EE 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.