Detailed explanation of mybatis creation method and global configuration tutorial

General introduction: mybatis is actually ibatis3 Persistence layer framework after version 0 [that is, the framework for dealing with databases]!

Technologies dealing with databases include:

Native JDBC technology -- > spring's jdbctemplate Technology

These tools provide simple SQL statement execution, but they are somewhat different from the mybatis framework we learned here,

A framework is a complete set of things, such as transaction control, query caching, field mapping, and so on.

When we use native JDBC to operate the database, we will go through the following steps:

Write SQL -- > precompile -- > set parameters -- > execute SQL -- > encapsulate results

We do not use native JDBC tools because these tools:

1. The function is simple. SQL statements are written in Java code [once SQL is modified, Java and SQL need to be recompiled!] This belongs to hard coding and high coupling.

2. We hope that some developers can write SQL statements by themselves, separate SQL statements from Java code, and write SQL statements in XML configuration files to realize the mapping between objects recorded in data tables!

SQL and Java codes are separated, with clear functional boundaries. One focuses on business and the other on data. Simple XML or annotations can be used for configuration and original mapping. The interface and Java POJO can be mapped into records in the database to complete the medium of business + underlying database!

1. Mybatis history

It was originally an open source project of Apache ibatis. In June 2010, the project was migrated from Apache Software Foundation to Google Code. With the development team switching to Google Code, ibatis 3 X was officially renamed mybatis, and the code was migrated to GitHub in November 2013 (see the download address below).

Ibatis comes from the combination of "Internet" and "abatis". It is a persistence layer framework based on Java. The persistence layer framework provided by ibatis includes SQL maps and data access objects, (DAO)

2. Introduction to mybatis:

Mybatis is an excellent persistence layer framework 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 for configuration and raw mapping, linking interfaces and

Java POJOs (plain old Java objects) are mapped to records in the database

3. Why use mybatis?

Mybatis is a semi automated lightweight persistence layer framework.

JDBC

C SQL is sandwiched in the Java code block, and the high coupling degree leads to hard coding internal injury. C maintenance is not easy, and the SQL changes in the actual development requirements, and frequent modifications are common

Hibernate and JPA

C is long and difficult to complex SQL, and it is not easy for hibernate to process the SQL automatically produced in C, so it is not easy to make special optimization. C is a fully automatic framework based on full mapping. It is difficult to partially map POJOs with a large number of fields. This leads to the degradation of database performance.

For developers, the core SQL still needs to be optimized by themselves

SQL and Java coding are separated, and the functional boundary is clear. One focuses on business and the other on data.

4. Where can I find mybatis?

https://github.com/mybatis/mybatis-3/

Or search mybatis directly in Baidu, and then find the address under GitHub to download! Write a HelloWorld for mybatis:

-Create database and data table and insert data

-Create a dynamic web project, and then create the entity class corresponding to the above data table

-[refer to the official documentation of mybatis] add the required jar package [jar package required by mybatis, jar package for dealing with the database, and jar package for log4j required for log printing]:

1.log4j-1.2. 17. Jar note: the jar package of log4j requires log4j XML file 2 mybatis-3.4. 1.jar 3. mysql-connector-java-5.1. 37-bin. jar

-Create mybatis config XML file, copy the contents of the mybatis document, and replace the database configuration information with your own:

mybatis-config. XML is the general control file, employeemapper XML is the SQL mapping file of mybatis, which also contains SQL statements

All SQL mapping files will be written to the master control file

-Create test cases Copy the official document code of mybatis as follows:

-Create a mapping file for SQL statements employeemapper xml;

Summary:

C create a test table C create a corresponding JavaBean C create a mybatis configuration file, SQL mapping file c test

The above development method is the development method of the old version of mybatis users! The new batch of mybatis users use interface methods

HelloWorld ---- interface programming

-Create a new mapper package, which contains the mapper interface and mapper Corresponding to XML file

In the past, it was necessary to write an implementation class for the interface, but at this time, mybatis provides an interface that can be dynamically bound with the SQL configuration file!

How to bind the two? In the past, the namespace of the SQL configuration file can be written casually, but now it can't be written casually. It needs to be specified as the fully qualified name of the interface!

At this time, the interface is bound to the SQL configuration file, and the ID and method name of the select tag are bound

Summary:

1. Interface programming

Native: Dao ================== > daoimpl

mybatis: xxMapper ================> xxMapper. xml

2. Sqlsession represents a session with the database, which must be closed after use.

3. Like connection, sqlsession is non thread safe. You should get a new object every time you use it. Do not define this object in class variables!

4. The mapper interface does not implement classes, but the mybatis interface generates a proxy object

5. Two important profiles

-Mybatis global configuration file (mybatis config. XML): contains database connection pool information, transaction manager information, etc System operation environment information- SQL mapping file (employeemapper. XML): saves the mapping information of each SQL statement.

Global configuration file for mybatis

The mybatis configuration file contains settings and properties information that deeply affect mybatis behavior. The top-level structure of the document is as follows: - configuration configuration - properties properties: load properties file - settings settings - typealiases type naming - typehandlers type processor - objectfactory object factory - plugins plug-in - environments - environment variables - transactionmanager transaction manager - datasource data source - databaseidp Provider database vendor ID - mappers mapper

The DTD document specifies that the above order cannot be changed

Details:

1. Configuration binds DTD constraints to global configuration files:

1) Networking will bind automatically

2) When there is no Internet [/ org / Apache / ibatis / builder / XML / mybatis-3-config. DTD]: unzip the jar package of mybatis and bind it in eclipse

Window-->Preference-->XML-->XML Catalog

2. Properties property

3. Settings contains many important settings

For example, in the database, the name is user_ Name, in the entity class, if the name is username and the value is true, it can also correspond to

4. Typealiases: alias

Although so many aliases can be used: it is recommended to use the full class name. It is easy to see how SQL statements are encapsulated into Java objects!

5. Typehandlers: type processor

Type processor: a tool class responsible for how to convert the type of database to the type of Java object

6. Environments

7. Databaseidprovider environment

Mybatis can execute different statements according to different database vendors

Type: DB_ VENDOR

C uses the vendordatabase idprovider provided by mybatis to resolve the database vendor ID. You can also customize the databaseidprovider interface.

Property name: database vendor ID

Property value: an alias for identification, which is convenient for SQL statements

After this is configured in the global configuration file of mybatis, we only need to add the attribute databaseid on the label of the execution statement in the SQL mapping file! Databaseid corresponds to value

In this way, when executing different databases, the statements of different databases will be executed

Of course, as shown above: when there are cases where the databaseid attribute is specified or not, the SQL statement with the databaseid attribute is executed!

8. Mapper mapping

Class: use annotation when the mapping file and interface file are not in the same directory

However, it is not recommended to use annotations. It is recommended to use SQL mapping files

9. Finally, the tags in the global configuration file are actually in order!

Use custom data sources

Replace the name in property with the writing method in c3p0

summary

The above is the detailed explanation of mybatis creation method and global configuration tutorial introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>