Mybatis introductory learning tutorial (I) – mybatis quick start

Mybatis was originally an open source project ibatis of Apache. In 2010, the project was migrated from Apache Software Foundation to Google Code and renamed mybatis. Moved to GitHub in November 2013.

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)

First of all, let's introduce the meaning of mybatis

Mybatis is an excellent persistence layer framework that supports common SQL queries, stored procedures and advanced mapping. Mybatis eliminates almost all the manual setting of JDBC code and parameters and the retrieval encapsulation of result sets. Mybatis can use simple XML or annotations for configuration and original mapping, and map the interface and Java POJO (plain old Java objects) into records in the database.

2、 Mybatis quick start

2.1 preparation of development environment

1. Create a test project, either an ordinary java project or a Java Web project, as shown in the following figure:

2. Add the corresponding jar package

  【mybatis】

  mybatis-3.1. 1.jar

[MySQL driver package] mysql-connector-java-5.1.7-bin.jar

3. Create databases and tables for the MySQL database

The SQL script is as follows:

Execute the SQL script in MySQL database to complete the operation of creating database and table, as follows:

By now, the preliminary preparation of the development environment has been completed.

2.2. Use mybatis to query the data in the table

1. Add the configuration file conf.xml of mybatis

Create a conf.xml file in the SRC directory, as shown in the following figure:

The contents in the conf.xml file are as follows:

2. Define the entity class corresponding to the table, as shown in the following figure:

The code of user class is as follows:

3. Define the SQL mapping file usermapper. For operating the users table xml

Create a me gacl. The mapping package is specially used to store SQL mapping files, and create a usermapper XML file, as shown in the following figure:

  userMapper. The contents of the XML file are as follows:

For example, namespace = "me. GaCl. Mapping. Usermapper" is me gacl. Mapping (package name) + usermapper (remove suffix from usermapper.xml file)

-->

4. Register usermapper.xml in conf.xml file XML file

5. Write test code: execute the defined select statement

Create a test1 class and write the following test code:

The results are as follows:

As can be seen from the above figure, the records in the database have been successfully queried.

The above is the whole content of this article for mybatis introductory learning tutorial (I) - mybatis quick start. I hope it will be helpful to you.

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
分享
二维码
< <上一篇
下一篇>>