Druid DBUtils ORM 封裝

package com.msi.druid;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.msi.model.User;

public class DBHelper {

private static DBHelper dbHlper = null;
private static DruidDataSource druidDataSource;
private DataSource dataSource;
private QueryRunner runner = null;

public static DBHelper getInstance() {
    if (dbHlper == null) {
        dbHlper = new DBHelper();
    }
    return dbHlper;
}

static {
    Properties properties = loadProperties("db_server.properties");
    try {
        druidDataSource = (DruidDataSource) DruidDataSourceFactory
                .createDataSource(properties);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public DruidDataSource getDruidDataSource() {
    return druidDataSource;
}

// 当不支持的时候,显式调用Connection
public DruidPooledConnection getConnection() throws SQLException {
    return druidDataSource.getConnection();
}

public DataSource getDataSource() {
    this.dataSource = DBHelper.getInstance().getDruidDataSource();
    return dataSource;
}

public static Properties loadProperties(String fileName) {
    // TODO Auto-generated method stub
    Properties p = new Properties();

    // 方法1获取配置
    // InputStream inStream =
    // DBHelper.class.getClass().getResourceAsStream("/db_dang.properties");
    InputStream inStream = null;
    try {

// 获取配置的第二种方式
// String path = DBHelper.class.getClassLoader().getResource(“\“)
// .getPath();
// path = new File(path).getParent();
// String profilepath = path + File.separator + fileName;
// System.out.println(“file ===“+profilepath);
// inStream = new FileInputStream(new File(profilepath));

        inStream = DBHelper.class.getClass().getResourceAsStream("/"+fileName);
        p.load(inStream);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        try {
            if (inStream != null) {
                inStream.close();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return p;
}

public QueryRunner initRunner() {
    this.dataSource = DBHelper.getInstance().getDataSource();
    if (this.dataSource != null) {
        this.runner = new QueryRunner(this.dataSource);
    }
    return runner;
}

public QueryRunner getRunner() {
    return initRunner();
}

public static void main(String[] args) throws SQLException {
    // TODO Auto-generated method stub
    List<User> userList = DBHelper
            .getInstance()
            .getRunner()
            .query("select * from t_user",
                    new BeanListHandler<User>(User.class));
    for (int i = 0; i < userList.size(); i++) {
        System.out.print(userList.get(i) + "\r\n");
    }

    DruidPooledConnection conn = DBHelper.getInstance().getConnection();
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select * from t_user");
    User user = null;
    while (rs.next()) {
        user = new User();
        user.setUserName(rs.getString("userName"));
        System.err.println(user.getUserName());
    }
}

}

配置如下
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_diary
username=root
password=yan
filters=stat
initialSize=2
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
minEvictableIdleTimeMillis=300000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=true
maxPoolPreparedStatementPerConnectionSize=200

WebXml中配置監控功能

<servlet>
    <servlet-name>DruidStatView</servlet-name>
    <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>DruidServlet</servlet-name>
    <servlet-class>com.msi.servlet.DruidServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>DruidStatView</servlet-name>
    <url-pattern>/druid/*</url-pattern>
</servlet-mapping>

你可能感兴趣的:(Druid DBUtils ORM 封裝)