Mybatis quick start (simple and easy to understand)

1、 Introduction to 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

JDBC - > dbutils - > mybatis - > hibernate

Mybatis is to write SQL in XML and then access the database.

2、 Mybatis quick start

2.1. New Java project

Add the driver jar of mybatis and MySQL: mybatis-3.1 1.jar,mysql-connector-java-5.1. 7-bin. jar

2.2. new table

2.3. Add the configuration file conf.xml for mybatis

2.4. Define the entity class corresponding to the table

2.5. Define the SQL mapping file usermapper. For the users table xml

2.6. Register usermapper.xml in the conf.xml file XML file

2.7. Write test code: execute the defined select statement

3、 Crud operation users table

3.1. Implementation of XML

3.1. 1. Define SQL mapping XML file:

3.1. 2. In config Register the mapping file in XML

3.1. 3. call in Dao

3.2. Implementation of annotation

3.2. 1. Define the interface of SQL mapping

3.2. 2. Register the mapping interface in config

3.2. 3. call in Dao

4、 Several places that can be optimized

4.1. The configuration of connecting to the database can be placed in a separate properties file.

4.2. Define aliases for entity classes to simplify references in SQL mapping XML files

4.3. The configuration file of log4j can be added under SRC to print log information

1. Add jar:

log4j-1.2. 16.jar

2.1. log4j. Properties (mode 1)

2.2. log4j. XML (mode 2)

5、 Resolve conflicts where field names are different from entity class attribute names

5.1. Prepare tables and fields

5.2. Define entity classes

5.3. Query to implement getorderbyid (ID):

Method 1: define an alias in the SQL statement

Method 2: through < resultmap >

6、 Implement association table query

6.1. One to one Association

6.1. 1. Raise demand

Query class information according to class ID (with teacher information)

6.1. 2. Create tables and data

6.1. 3. Define entity class:

6.1. 4. Define the SQL mapping file classmapper xml

6.1. 5. Test

6.2. One to many Association

6.2. 1. Raise demand

Query the corresponding class information according to the CLassID, including students and teachers

6.2. 2. Create tables and data:

6.2. 3. Define entity class

6.2. 4. Define the SQL mapping file classmapper xml

6.2. 5. Test

7、 Dynamic SQL and fuzzy query

7.1. demand

Realize multi condition query of users (name fuzzy matching, age between the specified minimum value and maximum value).

7.2. Preparing databases and tables

7.4. User table entity class

7.5. userMapper. XML (mapping file)

7.6. Usertest

Dynamic SQL tags available in mybatis

8、 Call stored procedure

8.1. Put forward demand

Query the number of men or women. If 0 is passed in, it is a woman, otherwise it is a man

8.2. Prepare database tables and stored procedures:

8.3. Create entity class for table

8.4. userMapper. xml

8.5. test

9、 Mybatis cache

9.1. Understanding mybatis caching

Like most persistence layer frameworks, mybatis also provides L1 and L2 cache support

1. L1 cache: HashMap local cache based on perpetual cache. Its storage scope is session. After session flush or close, all caches in the session will be cleared.

2. The second level cache has the same mechanism as the first level cache. By default, it is stored in perpetual cache and HashMap. The difference is that its storage scope is mapper (namespace) and the storage source can be customized, such as ehcache.

3. For the cache data update mechanism, when a C / U / D operation is performed on the of a scope (L1 cache session / L2 cache namespaces), by default, the caches in all selections under the scope will be cleared.

9.2. Mybatis L1 cache

9.2. 1. Query by task

Query the corresponding user record object according to the ID.

9.2. 2. Prepare database tables and data

9.2. 3. Create the entity class of the table

9.2. 4.userMapper. xml

9.2. 5. Test

9.3. Mybatis L2 cache

9.3. 1. Add a < cache > in usermapper In XML

9.3. 2. Test

9.3. 3. Supplementary notes

1. All select statements in the mapping statement file will be cached.

2. All insert, update and delete statements in the mapping statement file will refresh the cache.

3. The least recently used (LRU) algorithm will be used to recover the cache.

4. The cache will be refreshed according to the specified time interval.

5. The cache will store 1024 objects

10、 Spring integrates mybatis

10.1. Add jar

【mybatis】

mybatis-3.2. 0.jar mybatis-spring-1.1. 1.jar log4j-1.2. 17.jar

【spring】

spring-aop-3.2. 0.RELEASE. jar spring-beans-3.2. 0.RELEASE. jar spring-context-3.2. 0.RELEASE. jar spring-core-3.2. 0.RELEASE. jar spring-expression-3.2. 0.RELEASE. jar spring-jdbc-3.2. 0.RELEASE. jar spring-test-3.2. 4.RELEASE. jar spring-tx-3.2. 0.RELEASE. jar aopalliance-1.0. jar cglib-nodep-2.2. 3.jar commons-logging-1.1. 1.jar

[MySQL driver package]

mysql-connector-java-5.0. 4-bin. jar

10.2. Database table

10.3. Entity class: user

10.4. Dao interface: usermapper (xxxmapper)

10.5. SQL mapping file: usermapper XML (same name as interface ignoring case)

10.6. Spring configuration file: beans xml

10.7. Configuration file of mybatis: mybatis config xml

10.8. test

The above is a quick start to mybatis (concise and easy to understand) introduced by Xiaobian. I hope it will help 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
分享
二维码
< <上一篇
下一篇>>