db.properties
driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test_db?useUnicode=true&characterEncoding=UTF-8&jdbcCompliantTruncation=false
user=root
password=
commonsDbutils工具类
package com.variflight.b.gateway.utils;
import com.variflight.b.gateway.entity.po.VisitorRateConfPO;
import org.apache.commons.dbutils.BasicRowProcessor;
import org.apache.commons.dbutils.GenerousBeanProcessor;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.BeanMapHandler;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* @author muyang
* @date 2019/5/23 11:37
*/
public class CommonsDbutils {
private static Connection conn;
private static String driverClass;
private static String url;
private static String user;
private static String password;
static {
try {
readDBConfig();
Class.forName(driverClass);
conn = DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void readDBConfig(){
try {
InputStream in = CommonsDbutils.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro = new Properties();
pro.load(in);
driverClass = pro.getProperty("driverClass");
url = pro.getProperty("url");
user = pro.getProperty("user");
password = pro.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
public static Connection getConnection(){
return conn;
}
public static <T> T queryBean(Class<T> clazz, String sql){
T t = null;
QueryRunner queryRunner = new QueryRunner();
try {
t = queryRunner.query(conn, sql, new BeanHandler<>(clazz, new BasicRowProcessor(
new GenerousBeanProcessor()
)));
} catch (SQLException e) {
e.printStackTrace();
}
return t;
}
public static <T> List<T> queryBeanList(Class<T> clazz, String sql){
List<T> t = null;
QueryRunner queryRunner = new QueryRunner();
try {
t = queryRunner.query(conn, sql, new BeanListHandler<>(clazz, new BasicRowProcessor(
new GenerousBeanProcessor()
)));
} catch (SQLException e) {
e.printStackTrace();
}
return t;
}
public static <T> Map<String, T> queryMap(Class<T> clazz, String sql){
Map<String, T> t =null;
QueryRunner runner = new QueryRunner();
try {
t = runner.query(conn, sql, new BeanMapHandler<String, T>(clazz));
} catch (SQLException e) {
e.printStackTrace();
}
return t;
}
}
测试
public static void main(String[] args){
//获取连接
String sql = "select * from visitor_rate_conf";
//List<VisitorRateConfPO> confPOList = queryBeanList(conn, sql);
//VisitorRateConfPO confPOList = CommonsDbutils.queryBean(VisitorRateConfPO.class, sql);
List<VisitorRateConfPO> confPOList = CommonsDbutils.queryBeanList(VisitorRateConfPO.class, sql);
//Map<String, VisitorRateConfPO> confPOList = CommonsDbutils.queryMap(VisitorRateConfPO.class,sql);
System.out.println(confPOList);
}