1. 程式人生 > >MyBatis四大核心概念

MyBatis四大核心概念

本文講解 MyBatis 四大核心概念(SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession、Mapper)。

圖片描述

MyBatis 作為網際網路資料庫對映工具界的“上古神器”,訓有四大“神獸”,謂之:SqlSessionFactoryBuilder、SqlSessionFactory、SqlSession、Mapper。可以說,瞭解了這四大核心,便可知 MyBatis 八九。

SqlSessionFactoryBuilder

從命名上可以看出,這個是一個 Builder 模式的,用於建立 SqlSessionFactory 的類。SqlSessionFactoryBuilder 根據配置來構造 SqlSessionFactory。

其中配置方式有兩種

1. XML 檔案方式

XML 檔案方式是作為常用的一種方式:

String resource = "org/mybatis/example/mybatis-config.xml";

InputStream inputStream = Resources.getResourceAsStream(resource);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

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>

2. Java Config

這是第二種配置方式,通過 Java 程式碼來配置:

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();

TransactionFactory transactionFactory = new JdbcTransactionFactory();

Environment environment = new Environment("development", transactionFactory, dataSource);

Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);

SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

Java Config 相比較 XML 檔案的方式而言,會有一些限制。比如修改了配置檔案需要重新編譯,註解方式沒有 XML 配置項多等。所以,業界大多數情況下是選擇 XML 檔案的方式。但到底選擇哪種方式,這個要取決與自己團隊的需要。比如,專案的 SQL 語句不復雜,也不需要一些高階的 SQL 特性,那麼 Java Config 則會更加簡潔一點;反之,則可以選擇 XML 檔案的方式。

SqlSessionFactory

SqlSessionFactory 顧名思義,是用於生產 SqlSession 的工廠。

通過如下的方式來獲取 SqlSession 例項:

SqlSession session = sqlSessionFactory.openSession();

SqlSession

SqlSession 包含了執行 SQL 的所有的方法。以下是示例:

SqlSession session = sqlSessionFactory.openSession();
try {
Blog blog = session.selectOne(
"org.mybatis.example.BlogMapper.selectBlog", 101);
} finally {
session.close();
}

當然,下面的方式可以做到型別安全:

SqlSession session = sqlSessionFactory.openSession();
try {
BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);
} finally {
session.close();
}

Mapper

Mapper 顧名思義,是用做 Java 與 SQL 之間的對映的。包括了 Java 對映為 SQL 語句,以及 SQL 返回結果對映為 Java。

比如,下面是一個常見的 Mapper 介面對映檔案:

<?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="org.mybatis.example.BlogMapper">
<select id="selectBlog" resultType="Blog">
select * from Blog where id = #{id}
</select>
</mapper>

其中 “org.mybatis.example.BlogMapper” 就是我們要射射的介面,selectBlog 就是BlogMapper上的方法。而這個 selectBlog 具體就是要執行“select * from Blog where id = #{id}”這個 SQL 語句。

這樣,我們就能通過

Blog blog = session.selectOne(
"org.mybatis.example.BlogMapper.selectBlog", 101);

或者是

BlogMapper mapper = session.getMapper(BlogMapper.class);
Blog blog = mapper.selectBlog(101);

來獲取到執行的結果。

當然,如果是採用註解的方式的話,可以省去 XML 檔案:

public interface BlogMapper {
@Select("SELECT * FROM blog WHERE id = #{id}")
Blog selectBlog(int id);
}

參考引用