I. Overview
MyBatis is a persistence framework similar to that of Hiberante. MyBatis encapsulates JDBC to enable object-oriented development than JDBC. Instead of Hiberante, it was a lightweight framework that removed Hiberante's heavyweight stuff, The reason many Internet companies are willing to use this, such as JD.com in some way to say mybatis relatively lightweight, high efficiency, high flexibility, faced with complex business queries hibernate query efficiency than mybatis slow much more simple hiberante package more comprehensive like a person to drive a car simple way we can go, but the terrain complex road sedan will not open and become cumbersome, and mybatis is like a person riding a bike, no matter what road we can go.
that
's all the crap. Below will give everyone to set up mybatis environment.
Second, build the environment.
1, add the jar package.
go to official website download: https://github.com/mybatis/mybatis-3/releases
I used the mybatis-3.2.8 downloaded after the extract, the following several pictures marked red jar package copied into the project:
The root directory
Lib folder below
2. Create a new Java project.
The file directory structure is as follows:
tell everyone where log4j.properties this file must have an error
3. Here's how to write the code.
Mybatis-cofig.xml
<?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"/> <datasource type= "Pooled" > <property name= "Driver" Value= "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://127.0.0.1:3306/mybatis"/> <property name= "username" value= "root"/> <property name= "password" value= "root123"/> < /datasource> </environment> </environments></configuration>
log4j.properties
log4j.rootlogger=debug, console#consolelog4j.appender.console=org.apache.log4j.consoleappenderlog4j.appender.console.layout= org.apache.log4j.patternlayoutlog4j.appender.console.layout.conversionpattern=%d [%t]%-5p [%c]-%m% nlog4j.logger.java.sql.resultset=debuglog4j.logger.org.apache=debuglog4j.logger.java.sql.connection= Debuglog4j.logger.java.sql.statement=debuglog4j.logger.java.sql.preparedstatement=debug
Grouptest.java
import static Org.junit.assert.*;import Java.io.ioexception;import Java.io.inputstream;import org.apache.ibatis.io.Resources; Import Org.apache.ibatis.session.sqlsessionfactory;import Org.apache.ibatis.session.SqlSessionFactoryBuilder; Import Org.junit.test;import Com.gaowei.utils.mybatisutil;public class Grouptest {//sesionfactory uses a singleton mode to create private Sqlsessionfactory sessionfactory= mybatisutil.getinstance (); @Testpublic void Connection () {}}
Mybatisutil.java
package com.gaowei.utils;import Java.io.reader;import Org.apache.ibatis.io.resources;import Org.apache.ibatis.session.sqlsessionfactory;import Org.apache.ibatis.session.sqlsessionfactorybuilder;public class Mybatisutil {private static sqlsessionfactory Sqlmpper;private static Reader reader;static{try {Reader=resources.getresourceasreader ("com/gaowei/model/ Mybatis-config.xml "); sqlmpper = new Sqlsessionfactorybuilder (). build (reader); System.out.println (sqlmpper);} catch (Exception e) {e.printstacktrace ();}} public static Sqlsessionfactory getinstance () {return sqlmpper; }}
4. View Success
Run the Grouptest.java as long as you connect to the database to promise the following sentence you have succeeded.
Iv. Summary.
In this way, MyBatis environment has been set up the next blog to give you a detailed explanation of these XML configuration.
"One day rookie to the Big Bird" mybatis build the Environment