[One Day rookie to laruence] MyBatis builds the environment, laruence mybatis
I. Overview
MyBatis is a persistence framework similar to Hiberante. Compared with JDBC, MyBatis encapsulates JDBC to enable object-oriented development. Compared with Hiberante, It is a lightweight framework that removes the heavyweight items of Hiberante. Many Internet companies are willing to use this, such as JD. COM is lightweight, efficient, and flexible in some aspects, in the face of complex business queries, the query efficiency of Hibernate is much slower than that of MyBatis. The Hiberante encapsulation is more comprehensive, just like a person driving a car. Let's go, however, road cars with complex terrain cannot be opened and become heavy, while MyBatis is just like riding a bicycle, No matter what road we can go.
So much nonsense. Let's start building the MyBatis environment for everyone.
2. Build the environment.
1. Add the jar package.
Go to the official website download: https://github.com/mybatis/mybatis-3/releases
I used a mybatis-3.2.8 to download and decompress it, the following figure marked red jar package into the project:
Under the root directory
Under the lib folder
2. Create a java project.
The file directory structure is as follows:
The log4j. properties file must exist. Otherwise, an error is reported.
3. Write the code below.
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 the 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. Check successful
Run GroupTest. java. If you connect to the database below, you will be able to say the following sentence.
Iv. Summary.
In this way, the Mybatis environment has been set up. The next blog will give you a detailed explanation of these XML configurations.