mybatis+proxool 实现第三方数据库连接池

mybatis不直接支持第三方的数据库连接池,需要自己建DataSourceFactory。
用到的jar:mybatis-3.0.4.jar、proxool-0.9.1.jar、proxool-cglib.jar等。

ProxoolDataSourceFactory.java代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class ProxoolDataSourceFactory implements DataSourceFactory {
    protected ProxoolDataSource dataSource;
  
    public ProxoolDataSourceFactory() {
        this.dataSource = new ProxoolDataSource();
    }
  
    /*
     * (non-Javadoc)
     *
     * @see org.apache.ibatis.datasource.DataSourceFactory#getDataSource()
     */
    public DataSource getDataSource() {
        return this.dataSource;
    }
  
    /*
     * (non-Javadoc)
     *
     * @see
     * org.apache.ibatis.datasource.DataSourceFactory#setProperties(java.util
     * .Properties)
     */
    public void setProperties(Properties properties) {
        dataSource.setDriver(properties.getProperty("driver"));
        dataSource.setDriverUrl(properties.getProperty("driverUrl"));
        dataSource.setUser(properties.getProperty("user"));
        dataSource.setPassword(properties.getProperty("password"));
        dataSource.setAlias(properties.getProperty("alias"));
        // 其他参数略
    }
}

mybatis-config.xml配置文件片段:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<typeAliases><typeAlias type="com.lazylei.demo.datasource.proxool.ProxoolDataSourceFactory" alias="PROXOOL" /></typeAliases>
<environments default="db1">
    <environment id="db1">
        <transactionManager type="JDBC" />
        <dataSource type="PROXOOL">
            <property name="driver" value="${proxool.db1.driver}" />
            <property name="driverUrl" value="${proxool.db1.driverUrl}" />
            <property name="user" value="${proxool.db1.user}" />
            <property name="password" value="${proxool.db1.password}" />
            <property name="alias" value="${proxool.db1.alias}" />
        </dataSource>
    </environment>
    <environment id="db2">
        <transactionManager type="JDBC" />
        <dataSource type="PROXOOL">
            <property name="driver" value="${proxool.db2.driver}" />
            <property name="driverUrl" value="${proxool.db2.driverUrl}" />
            <property name="user" value="${proxool.db2.user}" />
            <property name="password" value="${proxool.db2.password}" />
            <property name="alias" value="${proxool.db2.alias}" />
        </dataSource>

 

 

 

你可能感兴趣的:(mybatis)