DBUtils是apache组织的一个工具类,jdbc的框架,更方便我们使用
使用步骤:
1.导入jar包(commons-dbutils-1.4.jar)
2.创建一个queryrunner类
queryrunner作用:操作sql语句
构造方法:
new QueryRunner(Datasource ds);//ds为一个数据源 eg:DataSourceUtils.getDataSource()
3.编写sql
4.执行sql
uery(..):执行r操作
update(...):执行cud操作
一.dbutils的CUD
import java.sql.SQLException; import org.apache.commons.dbutils.QueryRunner; import org.junit.Test; import util.DataSourceUtils; /** *dbutils的 CUD *建立在 DataSourceUtils之上 */ public class DBUtilsCUD { //insert @Test public void f1() throws SQLException{ //创建QueryRunner QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); //编写sql String sql = "insert into student values(31,?)"; //执行sql qr.update(sql, "3131"); } //update @Test public void f2() throws SQLException{ //创建QueryRunner QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); //编写sql String sql = "update student set name=? where id=?"; //执行sql qr.update(sql, "xxxxx",31); } //delete @Test public void f3() throws SQLException{ //创建QueryRunner QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource()); //编写sql String sql = "delete from student where id=?"; //执行sql qr.update(sql,31); } }
二.dbutils的R
主要是几种ResultSetHandler
import java.sql.SQLException; import java.util.List; import java.util.Map; 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.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import org.junit.Test; import domain.Student; import util.DataSourceUtils; /* * dbutils的R操作 * 主要是几种ResultSetHandler * */ public class DBUtilsR { /*☆☆☆☆☆ * 将查询结果的第一条记录封装成指定的bean对象,返回 * */ @Test public void beanHandler() throws SQLException{ QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from student"; Student bean = qr.query(sql,new BeanHandler<>(Student.class)); System.out.println(bean); } /*☆☆☆☆☆ * 将查询结果的每一条记录封装成指定的bean对象,将每一个bean对象放入list中 返回. * */ @Test public void beanListHandler() throws SQLException{ QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from student"; List<Student> list = qr.query(sql,new BeanListHandler<>(Student.class)); for (Student bean : list) { System.out.println(bean); } } /*☆☆☆ * 将查询结果的每一条记录封装map集合,将每一个map集合放入list中返回 * */ @Test public void mapListHandler() throws SQLException{ QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select * from student"; List<Map<String, Object>> list = qr.query(sql,new MapListHandler()); for (Map<String, Object> map : list) { System.out.println(map); } } /*☆☆☆ * 针对于聚合函数 例如:count(*) 返回的是一个Long值 * */ @Test public void ScalarHandler() throws SQLException{ QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource()); String sql="select count(*) from student"; Object obj = qr.query(sql, new ScalarHandler()); System.out.println(obj); } }
其它几种ResultSetHandler (了解)ArrayHandler, 将查询结果的第一条记录封装成数组,返回 (了解)ArrayListHandler, 将查询结果的每一条记录封装成数组,将每一个数组放入list中返回 (了解)ColumnListHandler, 将查询结果的指定一列放入list中返回 (了解)MapHandler, 将查询结果的第一条记录封装成map,字段名作为key,值为value 返回
时间: 2024-12-27 10:10:28