JavaJDBC Part6 DBUtils工具依赖

Posted mindzone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaJDBC Part6 DBUtils工具依赖相关的知识,希望对你有一定的参考价值。

Apache-DBUtils

开源的JDBC工具类,对JDBC的简单封装

SQL操作交给了QueryRunner的实例

 

Maven依赖

<!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
<dependency>
    <groupId>commons-dbutils</groupId>
    <artifactId>commons-dbutils</artifactId>
    <version>1.7</version>
</dependency>

 

插入语句测试【删改同理操作】

    @Test
    public void dbTest() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        Connection connectionByOriginalJdbc = CompleteJdbcUtils.getConnectionByOriginalJdbc();
        String sql = "insert into user(user_name,user_password,user_createTime) values(?,?,?);";
        int update = queryRunner.update(connectionByOriginalJdbc, sql, "你弟含王", "121314",CompleteJdbcUtils.parseToSqlDate("1997-09-06"));
        System.out.println(update);
        CompleteJdbcUtils.releaseResource(connectionByOriginalJdbc,null,null);
    }

 

查询单个的QueryRunner 

- BeanHandler<T> 

    @Test
    public void dbTest2() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        Connection connectionByOriginalJdbc = CompleteJdbcUtils.getConnectionByHikari();
        // 实体类处理器对象
        BeanHandler<User> beanHandler = new BeanHandler<User>(User.class);
        String sql = "select * from user where user_id = ?;";
        // 根据BeanHandler反射过去 只能处理单个对象返回
        User query = queryRunner.query(connectionByOriginalJdbc, sql, beanHandler,3);
        System.out.println(query);
        CompleteJdbcUtils.releaseResource(connectionByOriginalJdbc,null,null);
    }

 

用Map封装单个记录的 MapHandler,默认泛型<String,Object>

    @Test
    public void dbTest21() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        Connection connectionByOriginalJdbc = CompleteJdbcUtils.getConnectionByHikari();
        // 实体类处理器对象
        MapHandler mapHandler = new MapHandler(); // <String,Object>
        String sql = "select * from user where user_id = ?;";
        // 根据BeanHandler反射过去 只能处理单个对象返回
        Map<String, Object> map = queryRunner.query(connectionByOriginalJdbc, sql, mapHandler, 3);
        System.out.println(map);
        CompleteJdbcUtils.releaseResource(connectionByOriginalJdbc,null,null);
    }

 

多个记录的BeanListHandler<T>处理

    @Test
    public void dbTest3() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        Connection connectionByOriginalJdbc = CompleteJdbcUtils.getConnectionByHikari();
        // 实体类处理器对象
        BeanListHandler<User> beanHandler = new BeanListHandler<User>(User.class);
        String sql = "select * from user;";
        // 根据BeanHandler反射过去 返回List集合封装的对象的处理
        List<User> users = queryRunner.query(connectionByOriginalJdbc, sql, beanHandler);
        for (User user:users) {
            System.out.println(user);
        }
        CompleteJdbcUtils.releaseResource(connectionByOriginalJdbc,null,null);
    }

 

或者是MapListHandler,泛型是<String,Object>

    @Test
    public void dbTest31() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        Connection connectionByOriginalJdbc = CompleteJdbcUtils.getConnectionByHikari();
        // 实体类处理器对象
        MapListHandler mapListHandler = new MapListHandler();
        String sql = "select * from user;";
        List<Map<String, Object>> maps = queryRunner.query(connectionByOriginalJdbc, sql, mapListHandler);
        for (Map map:maps) {
            System.out.println(map);
        }
        CompleteJdbcUtils.releaseResource(connectionByOriginalJdbc,null,null);
    }

 

 

ScalarHandler 用于特殊值的获取,例如聚合函数的操作

https://www.bilibili.com/video/BV1eJ411c7rf?p=53

自定义结果集处理器的方法

https://www.bilibili.com/video/BV1eJ411c7rf?p=54

 

以上是关于JavaJDBC Part6 DBUtils工具依赖的主要内容,如果未能解决你的问题,请参考以下文章

怎么编写dbutil.java

DBUtils工具

开源JDBC工具类DbUtils

DBUtils和连接池

java学习日记(day30--dbutils)

Dbutils工具类的使用