Detailed tutorial on mybatis
Jar package required by mybatis:
You need to reference two jar packages, one is mybatis and the other is MySQL connector Java. If it is a maven project, you can add dependencies in POM as follows.
Data preparation:
Create point data in MySQL for testing:
Profile:
Two types of configuration files are required. One is mybatis config XML. In the example, it is a very simple configuration. There are many instructions for the detailed configuration on the Internet.
The other is the data provider mapping file: in the example, usermapper xml。 This file is usually found under Src / main / resource or under the subdirectory mybatis, which is located in mybatis config XML is specified by the resource of the mappers / mapper node.
The corresponding mapping file is test The usermapper interface under the mybatis namespace only defines the interface for accessing the data table:
You need a POJO: user java
Test:
A class called sqlsession is used to operate the database through mybatis. This class is generated through sqlsessionfactory. It is generally recommended to maintain a sqlsessionfactory globally.
The test code is as follows:
1. Mybatis will log with log4j, but turning on debug mode seems to have a great impact on performance.
2. The query cache of mybatis has a great impact on performance, and there is a big gap between enabling and not enabling
The above is a detailed tutorial on the use of mybatis 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!