【BaseDao的封装】书城项目,basedao封装模板(参考)
public abstract class BaseDao { QueryRunner queryRunner = new QueryRunner();
/**
* update方法执行insert、update、delete语句
*
* @return
*/
public int update(String sql, Object... args) {Connection connection = JdbcUtils.getConnection();
try {
return queryRunner.update(connection, sql, args);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(connection);
}return -1;
} /**
* 执行select查询语句,查询返回一个javaBean的sql
*
* @param type
* @param sql
* @param args
* @return
*/
public T queryForOne(Class type, String sql, Object... args) {
Connection connection = JdbcUtils.getConnection();
try {
return queryRunner.query(connection, sql, new BeanHandler(type),
args);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(connection);
}return null;
} public List queryForList(Class type, String sql, Object... args) {Connection connection = JdbcUtils.getConnection();
try {
return queryRunner.query(connection, sql, new BeanListHandler(
type), args);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils.close(connection);
}return null;
}}