MyBatis is a Java-based, open-source persistence layer Framework
First, the environment of MyBatis is built.
1. Download MyBatis https://github.com/mybatis/mybatis-3/releases
2. Import MyBatis-related packages into the project
Unzip the downloaded Mybatis-3.3.0.zip and open the folder
Import Mybatis-3.3.0.jar First
To all the jar packages in the Lib folder
Finally import the database connection-driven jar package (for example, I use MySQL to import Mysql-connector-java-5.1.7-bin.jar)
3. Configure the Log
Create a new configuration file named Log4j.properties under SRC
The code is as follows
Log4j.rootlogger=debug, Console #Console log4j.appender.console=org.apache.log4j.consoleappender Log4j.appender.console.layout=org.apache.log4j.patternlayout log4j.appender.console.layout.conversionpattern=%d [%t]%-5p [%c]-%m%n log4j.logger.java.sql.resultset=info log4j.logger.org.apache=info Log4j.logger.java.sql.Connect Ion=debug Log4j.logger.java.sql.statement=debug Log4j.logger.java.sql.preparedstatement=debug
Then create the Mybaisconfig.java configuration file
<?xml version= "1.0" encoding= "UTF-8" ?><! doctype configuration public "-//mybatis.org//dtd config 3.0//en" "Http://mybatis.org/dtd/mybatis-3-config.dtd" ><configuration> < environments default= "Development" > <environment id= "development" > <transactionmanager type= "JDBC" > </transactionmanager> <datasource type= "POOLED" > //using connection pooling <property name= "Driver" Value= "com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://localhost:3306/dbname"/> < Property name= "username" value= "root"/> <property name= "Password" value= "root"/> </datasource> </environment> </environments > <mappers> <mapper resource= "project/book/map/ Projectuser.xml "/> </mappers></configuration>
Configure your database connection information, Driver,url,username,password the same as the JDBC Connection database configuration
The reference to the map file, in addition to the references in the code, can also
Absolute path Reference <mapper url= "File///var/sqlmap/authormapper.xml"/>
Package path Reference <package name= "Com.bao.mybatis.mapperinterface"/>
4. Create a MySQL database, build a table, enter a record, and then build the user class (including the set Get method) in the project
Create a User.xml file to write SQL statements
<mapper namespace= "/" > <select id= "FindByID" parametertype= "int" resulttype= "project.book.pojo.User" > SE Lect * from User where id=#{id} </select></mapper>
To create a test class
public static void Main (string[] args) {String resource = "Projtct/book/map/mybatisconfig.xml"; Reader reader = null; Sqlsession session;try {reader = Resources.getresourceasreader (Resource);} catch (IOException e) {e.printstacktrace ();} Sqlsessionfactory sqlmapper = new Sqlsessionfactorybuilder (). build (reader);//sqlsessionfactory is a program-oriented session Sqlmapper.opensession (); Session is process-oriented, run out remember to call the Close method to close it jikeuser temp = Session.selectone ("FindByID", 1);//The first parameter is the ID of the previous code select, The second parameter is the corresponding idSystem.out.println (Temp.getusername ()) in the database table; System.out.println (Temp.getpassword ()); Session.close ();}
In this way, the first use of MyBatis program can be successfully run, welcome to point out the shortcomings, mutual exchange, thank you.
Preliminary use of MyBatis