參考:http://www.mybatis.org/mybatis-3/zh/getting-started.html
從今天開始學習官方文檔。
1.項目搭建
項目結構:
首先,搭建一個maven項目。為了方便以后的測試,這里每次測試都是以子項目的形式。所以,創建一個parent:
在idea中,File->new->project->選擇maven->groupt:com.test,artifact:l4mybatis.
創建好parent后,填充pom:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>l4mybatis</artifactId> 9 <packaging>pom</packaging> 10 <version>1.0-SNAPSHOT</version> 11 12 13 <dependencyManagement> 14 <dependencies> 15 <dependency> 16 <groupId>org.mybatis</groupId> 17 <artifactId>mybatis</artifactId> 18 <version>3.4.1</version> 19 </dependency> 20 <dependency> 21 <groupId>mysql</groupId> 22 <artifactId>mysql-connector-java</artifactId> 23 <version>6.0.2</version> 24 </dependency> 25 <dependency> 26 <groupId>junit</groupId> 27 <artifactId>junit</artifactId> 28 <version>4.12</version> 29 </dependency> 30 </dependencies> 31 </dependencyManagement> 32 33 34 </project>
在項目名稱l4mybatis上右鍵,new -> module->選擇maven->artifact:mybatis-base.
創建好子項目mybatis-base后,編寫pom:

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>l4mybatis</artifactId> 7 <groupId>com.test</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>mybatis-base</artifactId> 13 14 <dependencies> 15 <dependency> 16 <groupId>org.mybatis</groupId> 17 <artifactId>mybatis</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>mysql</groupId> 21 <artifactId>mysql-connector-java</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>junit</groupId> 25 <artifactId>junit</artifactId> 26 </dependency> 27 </dependencies> 28 29 <build> 30 <resources> 31 <resource> 32 <directory>src/main/resources</directory> 33 <includes> 34 <include>**/*.properties</include> 35 <include>**/*.xml</include> 36 </includes> 37 <filtering>true</filtering> 38 </resource> 39 <resource> 40 <directory>src/main/java</directory> 41 <includes> 42 <include>**/*.properties</include> 43 <include>**/*.xml</include> 44 </includes> 45 <filtering>true</filtering> 46 </resource> 47 </resources> 48 </build> 49 50 51 </project>
在helloword測試中,我們依賴有:
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.2</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies>
2. 准備測試數據庫
新建mysql 數據庫mybatis,創建一個表blog:

1 SET FOREIGN_KEY_CHECKS=0; 2 3 -- ---------------------------- 4 -- Table structure for blog 5 -- ---------------------------- 6 DROP TABLE IF EXISTS `blog`; 7 CREATE TABLE `blog` ( 8 `id` int(11) NOT NULL AUTO_INCREMENT, 9 `name` varchar(255) DEFAULT NULL, 10 PRIMARY KEY (`id`) 11 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; 12 13 -- ---------------------------- 14 -- Records of blog 15 -- ---------------------------- 16 INSERT INTO `blog` VALUES ('1', 'test');
3.創建mapper和model
在src\main\java下:
創建com.test.start.mapper.BlogMapper。這個相當於dao。

1 package com.test.start.mapper; 2 3 import com.test.start.model.Blog; 4 5 /** 6 * Created by miaorf on 2016/6/27. 7 */ 8 public interface BlogMapper { 9 10 Blog selectBlog(int id); 11 }
創建com/test/start/mapper/BlogMapper.xml。這個要和mapper一一映射,名字完全相同,命名空間一致。

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.test.start.mapper.BlogMapper"> 6 <select id="selectBlog" resultType="Blog"> 7 select * from Blog where id = #{id} 8 </select> 9 </mapper>
創建com.test.start.model.Blog。這個是實體,就是查詢結果所映射的類。

1 package com.test.start.model; 2 3 /** 4 * Created by miaorf on 2016/6/27. 5 */ 6 public class Blog { 7 int id; 8 String name; 9 10 @Override 11 public String toString() { 12 return "Blog{" + 13 "id=" + id + 14 ", name='" + name + '\'' + 15 '}'; 16 } 17 }
4.創建配置文件
在src\main\resources下:
創建mybatis-config.xml:

1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="db.properties"/> 7 8 <typeAliases> 9 <!--簡化mapper.xml中實體的的命名,否則mapper.xml中的實體必須為全限定名稱--> 10 <package name="com.test.start.model"/> 11 </typeAliases> 12 13 <environments default="development"> 14 <environment id="development"> 15 <transactionManager type="JDBC"/> 16 <dataSource type="POOLED"> 17 <property name="driver" value="${jdbc.driver}"/> 18 <property name="url" value="${jdbc.url}"/> 19 <property name="username" value="${jdbc.username}"/> 20 <property name="password" value="${jdbc.password}"/> 21 </dataSource> 22 </environment> 23 </environments> 24 <mappers> 25 <mapper resource="com/test/start/mapper/BlogMapper.xml"/> 26 </mappers> 27 </configuration>
創建db.properties:

1 #jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.driver=com.mysql.cj.jdbc.Driver 3 jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai&useSSL=false 4 jdbc.username=root 5 jdbc.password=
5.創建測試類
在src\main\java下
創建com.test.start.MybatisStarter:

1 package com.test.start; 2 3 import com.test.start.mapper.BlogMapper; 4 import com.test.start.model.Blog; 5 import org.apache.ibatis.io.Resources; 6 import org.apache.ibatis.mapping.Environment; 7 import org.apache.ibatis.session.Configuration; 8 import org.apache.ibatis.session.SqlSession; 9 import org.apache.ibatis.session.SqlSessionFactory; 10 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 11 import org.apache.ibatis.transaction.TransactionFactory; 12 import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; 13 import org.junit.Test; 14 15 import javax.sql.DataSource; 16 import java.io.IOException; 17 import java.io.InputStream; 18 19 /** 20 * Created by miaorf on 2016/6/27. 21 */ 22 public class MybatisStarter { 23 24 public static void main(String[] args) throws IOException { 25 String resource = "mybatis-config.xml"; 26 InputStream inputStream = Resources.getResourceAsStream(resource); 27 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 28 29 SqlSession session = sqlSessionFactory.openSession(); 30 try { 31 BlogMapper mapper = session.getMapper(BlogMapper.class); 32 Blog blog = mapper.selectBlog(1); 33 System.out.println(blog); 34 } finally { 35 session.close(); 36 } 37 } 38 39 @Test 40 public void sqlSessionByJava(){ 41 // DataSource dataSource = BlogDataSourceFactory.getBlogDataSource(); 42 // TransactionFactory transactionFactory = new JdbcTransactionFactory(); 43 // Environment environment = new Environment("development", transactionFactory, dataSource); 44 // Configuration configuration = new Configuration(environment); 45 // configuration.addMapper(BlogMapper.class); 46 // SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); 47 } 48 }
6.運行
Blog{id=1, name='test'}
Process finished with exit code 0
7.備份官方文檔
范圍(Scope)和生命周期
理解我們目前已經討論過的不同范圍和生命周期類是至關重要的,因為錯誤的使用會導致非常嚴重的並發問題。
提示 對象生命周期和依賴注入框架
依賴注入框架可以創建線程安全的、基於事務的 SqlSession 和映射器(mapper)並將它們直接注入到你的 bean 中,因此可以直接忽略它們的生命周期。如果對如何通過依賴注入框架來使用 MyBatis 感興趣可以研究一下 MyBatis-Spring 或 MyBatis-Guice 兩個子項目。
SqlSessionFactoryBuilder
這個類可以被實例化、使用和丟棄,一旦創建了 SqlSessionFactory,就不再需要它了。因此 SqlSessionFactoryBuilder 實例的最佳范圍是方法范圍(也就是局部方法變量)。你可以重用 SqlSessionFactoryBuilder 來創建多個 SqlSessionFactory 實例,但是最好還是不要讓其一直存在以保證所有的 XML 解析資源開放給更重要的事情。
SqlSessionFactory
SqlSessionFactory 一旦被創建就應該在應用的運行期間一直存在,沒有任何理由對它進行清除或重建。使用 SqlSessionFactory 的最佳實踐是在應用運行期間不要重復創建多次,多次重建 SqlSessionFactory 被視為一種代碼“壞味道(bad smell)”。因此 SqlSessionFactory 的最佳范圍是應用范圍。有很多方法可以做到,最簡單的就是使用單例模式或者靜態單例模式。
SqlSession
每個線程都應該有它自己的 SqlSession 實例。SqlSession 的實例不是線程安全的,因此是不能被共享的,所以它的最佳的范圍是請求或方法范圍。絕對不能將 SqlSession 實例的引用放在一個類的靜態域,甚至一個類的實例變量也不行。也絕不能將 SqlSession 實例的引用放在任何類型的管理范圍中,比如 Serlvet 架構中的 HttpSession。如果你現在正在使用一種 Web 框架,要考慮 SqlSession 放在一個和 HTTP 請求對象相似的范圍中。換句話說,每次收到的 HTTP 請求,就可以打開一個 SqlSession,返回一個響應,就關閉它。這個關閉操作是很重要的,你應該把這個關閉操作放到 finally 塊中以確保每次都能執行關閉。下面的示例就是一個確保 SqlSession 關閉的標准模式:
SqlSession session = sqlSessionFactory.openSession(); try { // do work } finally { session.close(); }
在你的所有的代碼中一致性地使用這種模式來保證所有數據庫資源都能被正確地關閉。
映射器實例(Mapper Instances)
映射器是創建用來綁定映射語句的接口。映射器接口的實例是從 SqlSession 中獲得的。因此從技術層面講,映射器實例的最大范圍是和 SqlSession 相同的,因為它們都是從 SqlSession 里被請求的。盡管如此,映射器實例的最佳范圍是方法范圍。也就是說,映射器實例應該在調用它們的方法中被請求,用過之后即可廢棄。並不需要顯式地關閉映射器實例,盡管在整個請求范圍(request scope)保持映射器實例也不會有什么問題,但是很快你會發現,像 SqlSession 一樣,在這個范圍上管理太多的資源的話會難於控制。所以要保持簡單,最好把映射器放在方法范圍(method scope)內。下面的示例就展示了這個實踐:
SqlSession session = sqlSessionFactory.openSession(); try { BlogMapper mapper = session.getMapper(BlogMapper.class); // do work } finally { session.close(); }
項目:https://github.com/chenxing12/l4mybatis