MyBatis usage steps

Mainly explain MyBatis-basic usage steps

     review:

    MyBatis is a data persistence layer (ORM) framework. The mapping relationship between entity classes and SQL statements is established, which is a semi-automatic ORM implementation. MyBATIS requires developers to write sql statements by themselves, which can increase the flexibility of the program and can be used as a supplement to ORM to a certain extent.

    MyBatis basic elements:

1. configuration.xml global configuration file

2. xml mapping file (mapper) - entity class

3. SqlSession interface

    MyBatis main execution process:

    1. mybatis configuration file, including Mybatis global configuration file and Mybatis mapping file,

The global configuration file configures information such as data sources and transactions; the mapping file configures information related to SQL execution.

    2. Mybatis constructs SqlSessionFactory, the session factory, by reading the configuration file information (global configuration file and mapping file).

    3. Through SqlSessionFactory, you can create SqlSession or session. Mybatis operates the database through SqlSession.

    4. SqlSession itself cannot directly operate the database, it operates the database through the underlying Executor executor interface. The Executor interface has two implementation classes, one is a normal executor and the other is a cache executor (default).

    5. The SQL information to be processed by the Executor is encapsulated into an underlying object MappedStatement.

The object includes: SQL statement, input parameter mapping information, and output result set mapping information. The mapping types of input parameters and output results include java simple types, HashMap collection objects, and POJO object types.

    Practical explanation

    1. Guide package

 Required jar packages: mybatis-3.2.7.jar, mysql-connector-java-5.1.8.jar; [other versions are also available]

         

    2. Log file log4j.properties

    Used to output some debugging information (including the number of SQL statements\input parameters\results)

     Parameters can be configured as needed to facilitate debugging.

    3. Write the core configuration file SqlMapConfig.xml of MyBatis [ Global configuration file: SqlMapConfig.xml]

        The detailed directory and content of the db.properties configuration file are given here.

    4. Write the entity class User -- corresponding to the user table in the database



    5. Write the Mapper mapping file User.xml [Key points]

The mapping file is the core of the framework. The following file configures the mapping between java objects and database tables.

We can see that the four tags: select, insert, delete, and update correspond to the operations of "check, add, delete, and change" respectively. There are also some attributes in each tag, which are explained below:


    6. Write test methods [using JUnit]

     I hope that through this article, readers can easily understand the process of MyBatis development. Of course, this is also the easiest way to use MyBatis. When I have time for advanced development such as MyBatis agent development and reverse engineering, I will write about it. I hope it will be helpful to everyone.

If you need to learn MyBatis completely, you can click to download [ Complete MyBatis Study Notes ]
After writing Spring+MyBatis+Spring MVC and other articles, I have compiled a set of [ Complete Practical Documents ] , click to download if necessary

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325585982&siteId=291194637