1. add jar files
2. Set up mybatis-config.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="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
```
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/test";
3. build SqlSessionFacory from configuration XML file
```
String resource = "mybatis-config.xml";
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
System.out.println(sqlSessionFactory);
```
4. Creat mapper xml;
```
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neuedu.model.mapper.DeptMapper">
<select id="selectDept" resultType="com.neuedu.model.po.Dept">
select * from Dept where deptno = #{deptno}
</select>
</mapper>
```
5. Acquiring a SqlSession from SqlSessionFactory
5.1 previous versions of MyBatis
```
SqlSession session = sqlSessionFactory.openSession();
Dept d = session.selectOne("com.neuedu.model.mapper.DeptMapper.selectDept", 10);
System.out.println(d.getDeptno()+"\t"+d.getDname()+"\t"+d.getLoc());
```
5.2 there is now a cleaner approach(Using an interface (e.g. BlogMapper.class) that properly describes the parameter and return value for a given statement)
```
DeptMapper mapper = session.getMapper(DeptMapper.class);
Dept d = mapper.selectDept(10);
System.out.println(d.getDeptno()+"\t"+d.getDname()+"\t"+d.getLoc());
```
6. Use Anotation
7. config log4j, place log4j.properties under classpath:
~~~
# Global logging configuration
#\u751F\u4EA7\u73AF\u5883\u914D\u7F6Einfo ERROR
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
~~~