DruidConnection:
package com.myssm.basedao;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class DruidConnection {
private static Properties properties;
private static DataSource dataSource;
static {
try {
properties= new Properties();
/*DruidConnection.class.getClassLoader().getResourceAsStream("druid.properties")
* getClass().getResourceAsStream("/druid.properties");
* JdbcUtils.class.GetClassLoader().getResourceAsStream(druid配置文件)
* */
properties.load(new FileInputStream("D:\\Program Files\\Pro-web\\druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static Connection getConnection() {
try {
// properties= new Properties();
// properties.load(new FileInputStream("D:\\Program Files\\Pro-web\\pro09-fruit1.4-thymeleaf\\src\\druid.properties"));
dataSource = DruidDataSourceFactory.createDataSource(properties);
return dataSource.getConnection();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void close(ResultSet set, Statement statement, Connection connection){
//判断是否为空
try {
if(set!=null){
set.close();
}
if(statement!=null){
statement.close();
}
if(connection!=null){
connection.close();
}
} catch (SQLException e) {
throw new RuntimeException();
}
}
}
下面这个和小破站的方法稍微有些许不同
package com.myssm.basedao;
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.ScalarHandler;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
public abstract class BaseDAO<T> {
private QueryRunner queryRunner=new QueryRunner();
private Class entityClass ;
// private DruidConnection druidConnection = new DruidConnection();
public BaseDAO(){
Type genericType = getClass().getGenericSuperclass();
//ParameterizedType 参数化类型
Type[] actualTypeArguments = ((ParameterizedType) genericType).getActualTypeArguments();
//获取到的<T>中的T的真实的类型
Type actualType = actualTypeArguments[0];
try {
entityClass = Class.forName(actualType.getTypeName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
// Class<T> clazz=(Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
public List<T> executeQuery(String sql , Object... params){
Connection connection=null;
try {
connection=DruidConnection.getConnection();
List<T> query = queryRunner.query(connection, sql, new BeanListHandler<T>(entityClass),params);
return query;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DruidConnection.close(null,null,connection);
}
}
// 更新
public int executeUpdate(String sql,Object... params){
Connection connection=null;
try {
connection=DruidConnection.getConnection();
int update = queryRunner.update(connection, sql,params);
return update;
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DruidConnection.close(null,null,connection);
}
}
//获取单个
public T executeSingleQuery(String sql , Object... params){
Connection connection=null;
try {
// String sql="select * from t_fruit where fname like ? ";
connection=DruidConnection.getConnection();
return queryRunner.query(connection, sql, new BeanHandler<T>(entityClass),params);
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
DruidConnection.close(null,null,connection);
}
}
//获取列表数量
public int getSum(){
Connection connection=null;
try {
connection=DruidConnection.getConnection();
QueryRunner queryRunner = new QueryRunner();
Object query = queryRunner.query(connection, "select count(*) from t_fruit",new ScalarHandler<>());
int sum=Integer.parseInt(String.valueOf(query));
return sum;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
//模糊查找获取列表数量
public int getSumLike(String fname){
Connection connection=null;
try {
connection=DruidConnection.getConnection();
QueryRunner queryRunner = new QueryRunner();
Object query = queryRunner.query(connection, "select count(*) from t_fruit where fname like ?",new ScalarHandler<>(),"%"+fname+"%");
int sum=Integer.parseInt(String.valueOf(query));
return sum;
}catch (SQLException e) {
throw new RuntimeException(e);
}
}
}