Mybatis environment construction and use example code
Introduction to mybatis:
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.com in November 2013
Mybatis is an excellent persistence layer framework, which 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 to configure and map native information, and map interfaces and Java POJOs (plain old Java objects) to records in the database.
1. Role of mybatis:
Simply put, mybatis implements the Dao layer, configures SQL through XML files, and maps it to Po.
2. Reference package to prepare
mybatis-3.2. 8. Jar: mybatis framework usage
ojdbc6. Jar: database connection
Configuration of mybatis environment (here is a separate configuration mybatis):
First: Download and import mybatis jar package
v3. The download address of 4.5 is:
http://xiazai.jb51.net/201712/yuanma/mybatis-3.4.5.rar
If you do not want to use 3.4 5 version, but if the address is forgotten, we can go to GitHub Com official website: mybatis
The search results are as follows:
At this time, click download latest, and then select the version you want to download.
Unzip the downloaded compressed package and put mybatis-3.4 5. All jars in jar and lib folder are imported into eclipse. Of course, at this time, you also need to import the jar package of MySQL driver into mysql-connector-java-5.1 44.jar。
Since Maven is not used here, the following configuration is not needed because we have manually imported all jars:
2. Writing of configuration file
Create a source folder in your own project, store your own configuration documents, and create sqlmappersconfig XML, and copy the following configuration (officially provided configuration, change the official configuration to your own):
sqlConnect. The configuration in properties is as follows:
OK, so far, we have completed the basic configuration of config to be configured, but this is only the configuration of config. The test code is as follows:
At this time, printing is valuable, which proves that our configuration is successful, and we can set the mapper for the rest
Second: the creation and use of mapper, because the mapper of mybatis can use dynamic proxy, so we directly use dynamic proxy here:
Create in POJO package:
User. java
Create in the mappers package:
UserMapper. java
In connection with sqlmappersconfig XML is created under the sibling Directory:
User. xml
And in sqlmappersconfig The XML configuration code is as follows:
It is implemented in the simple addition, deletion, modification and query of the database. The specific implementation is shown in the following code:
User. xml
Because it is loaded dynamically, you only need to write the interface here:
Because of the test code, only the fuzzy test code is written here:
summary
The above is the example code for the construction and use of mybatis environment 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!