基於Mybatis-3.5.0版本
1.0 資料來源模組
在資料持久層中,資料來源是一個非常重要的元件,期效能直接關係到整個資料持久層的效能。在實踐中比較常見的第三方資料來源元件有DBCP、C3P0、阿里druid和Springboot2推薦的HikariCP等,Mybatis不僅可以整合第三方資料來源元件,還提供了自己的資料來源實現。
Mybatis資料來源模組包結構如下:
2.0 DataSource
常見的資料來源元件都實現了javax.sql.DataSource
介面,Mybatis自身實現的資料來源實現也不例外。Mybatis提供了兩個javax.sql.DataSource
介面實現,分別是PooledDataSource和UnpooledDataSource。
2.1 UnpooledDataSource 非池化DataSource
org.apache.ibatis.datasource.unpooled.UnpooledDataSource
實現DataSource介面,非池化的DataSource物件。程式碼如下:
/**
* 非池化DataSource
* @author Clinton Begin
* @author Eduardo Macarron
*/
public class UnpooledDataSource implements DataSource {
// Driver 類載入器
private ClassLoader driverClassLoader;
// Driver 屬性
private Properties driverProperties;
// 已註冊的 Driver 對映
private static Map<String, Driver> registeredDrivers = new ConcurrentHashMap<>();
// Driver 類名
private String driver;
// 資料庫 URL
private String url;
// 資料庫使用者名稱
private String username;
// 資料庫密碼
private String password;
// 是否自動提交事務
private Boolean autoCommit;
// 預設事務隔離級別
private Integer defaultTransactionIsolationLevel;
static {
// 初始化 registeredDrivers
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
registeredDrivers.put(driver.getClass().getName(), driver);
}
}
public UnpooledDataSource() {
}
public UnpooledDataSource(String driver, String url, String username, String password) {
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
public UnpooledDataSource(String driver, String url, Properties driverProperties) {
this.driver = driver;
this.url = url;
this.driverProperties = driverProperties;
}
public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, String username,
String password) {
this.driverClassLoader = driverClassLoader;
this.driver = driver;
this.url = url;
this.username = username;
this.password = password;
}
public UnpooledDataSource(ClassLoader driverClassLoader, String driver, String url, Properties driverProperties) {
this.driverClassLoader = driverClassLoader;
this.driver = driver;
this.url = url;
this.driverProperties = driverProperties;
}
@Override
public Connection getConnection() throws SQLException {
return doGetConnection(username, password);
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
return doGetConnection(username, password);
}
@Override
public void setLoginTimeout(int loginTimeout) {
DriverManager.setLoginTimeout(loginTimeout);
}
@Override
public int getLoginTimeout() {
return DriverManager.getLoginTimeout();
}
@Override
public void setLogWriter(PrintWriter logWriter) {
DriverManager.setLogWriter(logWriter);
}
@Override
public PrintWriter getLogWriter() {
return DriverManager.getLogWriter();
}
public ClassLoader getDriverClassLoader() {
return driverClassLoader;
}
public void setDriverClassLoader(ClassLoader driverClassLoader) {
this.driverClassLoader = driverClassLoader;
}
public Properties getDriverProperties() {
return driverProperties;
}
public void setDriverProperties(Properties driverProperties) {
this.driverProperties = driverProperties;
}
public String getDriver() {
return driver;
}
public synchronized void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Boolean isAutoCommit() {
return autoCommit;
}
public void setAutoCommit(Boolean autoCommit) {
this.autoCommit = autoCommit;
}
public Integer getDefaultTransactionIsolationLevel() {
return defaultTransactionIsolationLevel;
}
public void setDefaultTransactionIsolationLevel(Integer defaultTransactionIsolationLevel) {
this.defaultTransactionIsolationLevel = defaultTransactionIsolationLevel;
}
private Connection doGetConnection(String username, String password) throws SQLException {
// 建立 Properties 物件
Properties props = new Properties();
if (driverProperties != null) {
// 設定 driverProperties 到 props 中
props.putAll(driverProperties);
}
// 設定 user和password
if (username != null) {
props.setProperty("user", username);
}
if (password != null) {
props.setProperty("password", password);
}
return doGetConnection(props);
}
private Connection doGetConnection(Properties properties) throws SQLException {
// 初始化 Driver
initializeDriver();
// 獲得 Connection 物件
Connection connection = DriverManager.getConnection(url, properties);
// 配置 Connection 物件
configureConnection(connection);
return connection;
}
/**
* 初始化 Driver
*
* @throws SQLException
*/
private synchronized void initializeDriver() throws SQLException {
// 判斷 registeredDrivers是否已經存在該 driver,若不存在,進行初始化
if (!registeredDrivers.containsKey(driver)) {
Class<?> driverType;
try {
// 獲得 driver 類
if (driverClassLoader != null) {
driverType = Class.forName(driver, true, driverClassLoader);
} else {
driverType = Resources.classForName(driver);
}
// DriverManager requires the driver to be loaded via the system ClassLoader.
// http://www.kfu.com/~nsayer/Java/dyn-jdbc.html
// 建立 Driver 物件
Driver driverInstance = (Driver) driverType.newInstance();
// 建立 DriverProxy物件,並註冊到 DriverManager中
DriverManager.registerDriver(new DriverProxy(driverInstance));
// 新增到registeredDrivers中
registeredDrivers.put(driver, driverInstance);
} catch (Exception e) {
throw new SQLException("Error setting driver on UnpooledDataSource. Cause: " + e);
}
}
}
private void configureConnection(Connection conn) throws SQLException {
// 設定自動提交
if (autoCommit != null && autoCommit != conn.getAutoCommit()) {
conn.setAutoCommit(autoCommit);
}
// 設定事務隔離級別
if (defaultTransactionIsolationLevel != null) {
conn.setTransactionIsolation(defaultTransactionIsolationLevel);
}
}
/**
* 靜態代理
* @ClassName: DriverProxy
* @Description: 主要是針對getParentLogger方法,使用 MyBatis 自定義的 Logger 物件
* @date 2019年4月3日
*
*/
private static class DriverProxy implements Driver {
private Driver driver;
DriverProxy(Driver d) {
this.driver = d;
}
@Override
public boolean acceptsURL(String u) throws SQLException {
return this.driver.acceptsURL(u);
}
@Override
public Connection connect(String u, Properties p) throws SQLException {
return this.driver.connect(u, p);
}
@Override
public int getMajorVersion() {
return this.driver.getMajorVersion();
}
@Override
public int getMinorVersion() {
return this.driver.getMinorVersion();
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String u, Properties p) throws SQLException {
return this.driver.getPropertyInfo(u, p);
}
@Override
public boolean jdbcCompliant() {
return this.driver.jdbcCompliant();
}
// @Override only valid jdk7+
public Logger getParentLogger() {
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}
}
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new SQLException(getClass().getName() + " is not a wrapper.");
}
@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
// @Override only valid jdk7+
public Logger getParentLogger() {
// requires JDK version 1.6
return Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);
}
}
複製程式碼
2.2 PooledDataSource 池化的DataSource
org.apache.ibatis.datasource.pooled.PooledDataSource
實現DataSource介面,池化的DataSource實現類。程式碼如下:
複製程式碼
本章內容會比較多,這週會陸續把它更完。防止手賤Ctrl+z把草稿回滾沒了。先發布一版(喜歡的朋友可以關注一下)
失控的阿甘,樂於分享,記錄點滴