版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40348465/article/details/83662643
Commons DbUtils是Apache组织提供的一个对JDBC进行简单封装的开源工具类库,使用它能够简化JDBC应用程序的开发,同时也不会影响程序的性能。
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
DbUtils 关闭链接等操作
QueryRunner 进行查询的操作
org.apache.commons.dbutils.handlers
ArrayHandler :将ResultSet中第一行的数据转化成对象数组
ArrayListHandler将ResultSet中所有的数据转化成List,List中存放的是Object[]
BeanHandler :将ResultSet中第一行的数据转化成类对象
BeanListHandler :将ResultSet中所有的数据转化成List,List中存放的是类对象
ColumnListHandler :将ResultSet中某一列的数据存成List,List中存放的是Object对象
KeyedHandler :将ResultSet中存成映射,key为某一列对应为Map。Map中存放的是数据
MapHandler :将ResultSet中第一行的数据存成Map映射
MapListHandler :将ResultSet中所有的数据存成List。List中存放的是Map
ScalarHandler :将ResultSet中一条记录的其中某一列的数据存成Object
org.apache.commons.dbutils.wrappers
SqlNullCheckedResultSet :对ResultSet进行操作,改版里面的值
StringTrimmedResultSet :去除ResultSet中中字段的左右空格。Trim()
主要方法:
DbUtils类:启动类,jdbc辅助方法的集合类,线程安全。
QueryRunner类:执行SQL语句的类,可以设置查询结果集的封装策略,线程安全。
ResultSetHandler接口:转换类型接口,将封装结果集中的数据,转换到另一个对象。
MapListHandler类:实现类,把记录转化成List。
BeanListHandler类:实现类,把记录转化成List,使记录为JavaBean类型的对象。
简单封装示例:
1.首先导入包:commons-dbutils-1.7.jar;mysql-connector-java-8.0.11.jar(也可以用其他版本的)
2.配置META-INF下的contex.xml文件
如:
mysql5用的驱动url是com.mysql.jdbc.Driver,mysql6以后用的是com.mysql.cj.jdbc.Driver。
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mysql" auth="Container" type="javax.sql.DataSource"
maxTotal="100" maxIdle="30" maxWaitMillis="10000"
username="root" password="" driverClassName="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/hrsys1?serverTimezone=UTC"/>
</Context>
3.DataBaseHelper类(加载配置文件,获取Connection)
package com.bookms.util;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DataBaseHelper {
public static Connection getConn() {
try {
//InitialContext()是用来加载配置文件,获取web容器配置的数据源
//InitialContext的构造方法主要是准备JNDI的访问环境,如果不加参数,那就意味着是用本地匿名访问
/*JNDI(Java Naming and Directory Interface,Java命名和目录接口)是一组在Java应用中访问命名和目录服务的API。
命名服务将名称和对象联系起来,使得我们可以用名称访问对象。目录服务是一种命名服务,在这种服务里,对象不但有名称,还有属性。*/
Context initContext = new InitialContext();
//一般情况下,intial.lookup("")中的参数就是JNDI名称。但是用的应用服务器,是把JNDI名放到java:comp/env/ejb/后面的。
//Tomcat容器配置的JNDI需要加固定前缀才是可查找JNDI,需要加前缀"java:comp/env/"。
Context envContext = (Context) initContext.lookup("java:/comp/env");
DataSource ds = (DataSource) envContext.lookup("jdbc/mysql");
Connection conn = ds.getConnection();
return conn;
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// etc.
return null;
}
}
4.DoBusiness(具体的封装)
package com.HR.app.model;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.HR.app.entity.Employee;
import com.HR.app.util.DataBaseHelper;
/*
* DAO
* data access object
* 数据存取对象
*/
public class DoBusiness {
private Connection conn;
QueryRunner run = new QueryRunner();
public List<Employee> getDatas() {
conn = DataBaseHelper.getConn();
ResultSetHandler<List<Employee>> h = new BeanListHandler<Employee>(Employee.class);
try {
return run.query(conn, "select * from employee", h);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
public boolean add(Employee emp) {
conn = DataBaseHelper.getConn();
try {
return run.update(conn, "insert into employee values(?,?,?,?,?,?,?,?,?)", emp.getId(), emp.getName(),
emp.getSex(), emp.getAdd(), emp.getPhone(), emp.getHireDate(), emp.getSal(), emp.getDept(),
emp.getPwd()) > 0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public boolean del(String id) {
conn = DataBaseHelper.getConn();
try {
return run.update(conn, "delte from employee where id = ?", id) > 0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public boolean modify(Employee emp) {
conn = DataBaseHelper.getConn();
try {
return run.update(conn, "update employee set add = ?,phone = ?,sal = ?,dept = ?,pwd = ? where id = ?",
emp.getAdd(), emp.getPhone(), emp.getSal(), emp.getDept(), emp.getPwd(), emp.getId()) > 0;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}
public Employee getEmpById(String id) {
conn = DataBaseHelper.getConn();
ResultSetHandler<Employee> h = new BeanHandler<Employee>(Employee.class);
try {
return run.query(conn, "select * from employee where id = ?", h, id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
// 登陆验证方法
public Employee check(String id, String pwd) {
conn = DataBaseHelper.getConn();
ResultSetHandler<Employee> h = new BeanHandler<Employee>(Employee.class);
try {
return run.query(conn, "select * from employee where id = ? and pwd = ?", h, id, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
}