Mybatis的数据源模块,采用了工厂方法设计模式。
如其中DataSourceFactory是工厂接口,而PooledDataSourceFactory等则是其工厂实现类。
Mybatis提供了三个工厂类实现方式:
PooledDataSourceFactory
UnpooledDataSourceFactory
JndiDataSourceFactory
调用方举例:org.apache.ibatis.builder.xml.XMLConfigBuilder#dataSourceElement
DataSourceFactory public interface DataSourceFactory { void setProperties(Properties var1); DataSource getDataSource(); } UnpooledDataSourceFactory public class UnpooledDataSourceFactory implements DataSourceFactory { private static final String DRIVER_PROPERTY_PREFIX = "driver."; private static final int DRIVER_PROPERTY_PREFIX_LENGTH = DRIVER_PROPERTY_PREFIX.length(); protected DataSource dataSource; public UnpooledDataSourceFactory() { this.dataSource = new UnpooledDataSource(); } @Override public void setProperties(Properties properties) { Properties driverProperties = new Properties(); // 利用基础层的配置解析模块,创建DataSource 相应的MetaObject MetaObject metaDataSource = SystemMetaObject.forObject(dataSource); for (Object key : properties.keySet()) { // 遍历Properties,从而获取DataSource所需的配置信息 String propertyName = (String) key; // 以”driver.”开头的配置项,是对DataSource的配置,记录到driverProperties中保存 if (propertyName.startsWith(DRIVER_PROPERTY_PREFIX)) { String value = properties.getProperty(propertyName); driverProperties.setProperty(propertyName.substring(DRIVER_PROPERTY_PREFIX_LENGTH), value); } else if (metaDataSource.hasSetter(propertyName)) { String value = (String) properties.get(propertyName); Object convertedValue = convertValue(metaDataSource, propertyName, value); metaDataSource.setValue(propertyName, convertedValue); } else { throw new DataSourceException("Unknown DataSource property: " + propertyName); } } if (driverProperties.size() > 0) { metaDataSource.setValue("driverProperties", driverProperties); } } @Override public DataSource getDataSource() { return dataSource; } private Object convertValue(MetaObject metaDataSource, String propertyName, String value) { Object convertedValue = value; Class<?> targetType = metaDataSource.getSetterType(propertyName); if (targetType == Integer.class || targetType == int.class) { convertedValue = Integer.valueOf(value); } else if (targetType == Long.class || targetType == long.class) { convertedValue = Long.valueOf(value); } else if (targetType == Boolean.class || targetType == boolean.class) { convertedValue = Boolean.valueOf(value); } return convertedValue; } } UnpooledDataSource public class UnpooledDataSource implements DataSource { // 进行驱动加载的classLoader,可参照JDBC相关处理 private ClassLoader driverClassLoader; // 驱动配置 private Properties driverProperties; // 驱动注册表(全量) private static Map<String, Driver> registeredDrivers = new ConcurrentHashMap<>(); // 当前DataSource所采用的驱动,如mysqlDriver private String driver; // 数据源地址 private String url; // 用户名 private String username; // 密码 private String password; // 是否自动提交(有关于事务),默认自动提交 private Boolean autoCommit; // 默认事务隔离级别 private Integer defaultTransactionIsolationLevel; // 默认网络超时时间 private Integer defaultNetworkTimeout; // 驱动注册 static { Enumeration<Driver> drivers = DriverManager.getDrivers(); while (drivers.hasMoreElements()) { Driver driver = drivers.nextElement(); registeredDrivers.put(driver.getClass().getName(), driver); } } // 方法略 } e.事务管理模块