Jar程序使用MyBatis集成阿里巴巴druid鏈接池

在寫jar程序,而不是web程序的時候,使用mybatis做爲持久層,能夠集成POOLED鏈接池,而阿里的druid不能用,確實很鬱悶。不過有辦法。java

首先準備好數據庫配置文件mysql

而後對Druid進行一個簡單的封裝git

package org.datasource;

import java.sql.SQLException;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.ibatis.datasource.DataSourceFactory;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 自定義類,用於將druid集成到配置文件中
 * @author Administrator
 *
 */
public class DruidDataSourceFactory implements DataSourceFactory
{
    private Properties props;

    @Override
    public void setProperties(Properties props)
    {
        this.props = props;
    }

    @Override
    public DataSource getDataSource()
    {
        DruidDataSource dds = new DruidDataSource();
        dds.setDriverClassName(this.props.getProperty("driver"));
        dds.setUrl(this.props.getProperty("url"));
        dds.setUsername(this.props.getProperty("username"));
        dds.setPassword(this.props.getProperty("password"));
        // 其餘配置能夠根據MyBatis主配置文件進行配置
        try
        {
            dds.init();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return dds;
    }
}

封裝好以後,能夠在mybatis的配置文件中使用了github

<?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>
    <!-- 引用db.properties配置文件 -->
    <properties resource="resource/db.properties" />

    <typeAliases>
        <typeAlias type="org.datasource.DruidDataSourceFactory"
            alias="DRUID" />
    </typeAliases>

    <!-- 配置分頁插件 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 設置數據庫類型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六種數據庫 -->
            <property name="dialect" value="mysql" />
        </plugin>
    </plugins>

    <!-- development : 開發模式 work : 工做模式 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <!-- 配置數據庫鏈接信息 -->
            <dataSource type="DRUID">
                <!-- value屬性值引用db.properties配置文件中配置的值 -->
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>
    <!-- mybatis的mapper文件,每一個xml配置文件對應一個接口 -->
    <mappers>
        <mapper resource="*1.xml" />
        <mapper resource="*2.xml" />
    </mappers>
</configuration>

而後就能夠使用了web

package org.util;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil
{
    /*
     * 定義配置文件的位置
     */
    private static final String CONFIG_PATH = "mybatis/mybatis.xml";

    /*
     * 獲取數據庫訪問連接
     */
    public static SqlSession getSqlSession()
    {
        SqlSession session = null;
        try
        {
            InputStream stream = Resources.getResourceAsStream(CONFIG_PATH);
            // 能夠根據配置的相應環境讀取相應的數據庫環境
            // SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream, "development");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
            session = factory.openSession();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return session;
    }

    /*
     * 獲取數據庫訪問連接
     */
    public static void closeSession(SqlSession session)
    {
        session.close();
    }
    
    public static void main(String[] args)
    {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        Mapper test = sqlSession.getMapper(Mapper.class);
        System.out.println(test.countByExample(new Example()));
        MyBatisUtil.closeSession(sqlSession);
    }
}
相關文章
相關標籤/搜索