码迷,mamicode.com
首页 > 数据库 > 详细

【Java】JDBC Part6 DBUtils工具依赖

时间:2020-04-25 10:44:18      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:apache   实体类   查询   get   简单   方法   repo   nal   exce   

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

 

【Java】JDBC Part6 DBUtils工具依赖

标签:apache   实体类   查询   get   简单   方法   repo   nal   exce   

原文地址:https://www.cnblogs.com/mindzone/p/12769945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!