导读 Hutool-db是一个在JDBC基础上封装的数据库操作工具类,通过包装,使用ActiveRecord思想操作数据库。我们在日常开发中,经常会用到一个系统需要链接多个数据库来实现业务的需求,比如多个系统之间数据调用、两个数据之间同步等等。

今天给大家分享使用Hutool-db实现多数据源配置,大家一起来学习一下吧!

hutool-db介绍

Hutool-db是一个在JDBC基础上封装的数据库操作工具类,通过包装,使用ActiveRecord思想操作数据库。在Hutool-db中,使用Entity(本质上是个Map)代替Bean来使数据库操作更加灵活,同时提供Bean和Entity的转换提供传统ORM的兼容支持。

  • 数据源 DataSource
  • SQL执行器 SqlExecutor
  • CRUD的封装 Db、SqlConnRunner SqlRunner
  • 支持事务的CRUD封装 Session
  • 各种结果集处理类 handler
  • 数据库的一些工具方法汇总 DbUtil
  • 新建一个Maven项目
    导入依赖包
    
                mysql
                mysql-connector-java
                5.1.45
            
            
                com.microsoft.sqlserver
                sqljdbc4
                4.0
            
            
                cn.hutool
                hutool-db
                5.7.22
            
            
                com.alibaba
                druid
                1.2.9
            
    新建db.setting配置文件

    src/main/resources/config/db.setting

    [mysql]
    url = jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
    username = root
    password = 123456
    driver = com.mysql.jdbc.Driver
    [sqlserver]
    url = jdbc:sqlserver://192.168.33.4:1433;DatabaseName=DB
    username = sa
    password = 123456
    driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
    新建测试demo
    /**
         * 测试mysql
         */
        private static void testMysql() {
            DataSource ds = DSFactory.get("mysql");
            Db.use(ds);
            Connection conn = null;
            try {
                conn = ds.getConnection();
                // 插入语句
                SqlExecutor.execute(conn, "insert into t_user (name,age) value ('小张',35)");
                // 更新语句
                SqlExecutor.execute(conn, "update t_user set name='小明002' where id=2 ");
                // 删除语句
                SqlExecutor.execute(conn, "delete from t_user  where id=2 ");
                List entityList = SqlExecutor.query(conn, "select * from t_user limit 50", new EntityListHandler());
                for (Entity entity : entityList) {
                    System.out.println(entity.get("name"));
                }
            } catch (SQLException e) {
    
            } finally {
                DbUtil.close(conn);
            }
        }
    
        /**
         * 测试sqlserver
         */
        private static void testSqlServer() {
            DataSource ds = DSFactory.get("sqlserver");
            Connection conn = null;
            try {
                conn = ds.getConnection();
                List entityList = SqlExecutor.query(conn, "select * from t_user", new EntityListHandler());
                for (Entity entity : entityList) {
                    System.out.println(entity.get("username"));
                }
            } catch (SQLException e) {
    
            } finally {
                DbUtil.close(conn);
            }
        }
    
        /**
         * 直接代码写jdbc数据源 不推荐的方式
         */
        private static void testDefineJdbc() {
            DruidDataSource ds = new DruidDataSource();
            ds.setUrl("jdbc:mysql://127.0.0.1:3306/mydb?characterEncoding=utf-8&useSSL=false&serverTimeznotallow=GMT");
            ds.setUsername("root");
            ds.setPassword("12345678");
            Connection conn = null;
            try {
                conn = ds.getConnection();
                List entityList = SqlExecutor.query(conn, "select * from t_user", new EntityListHandler());
                for (Entity entity : entityList) {
                    System.out.println(entity.get("name"));
                }
            } catch (SQLException e) {
    
            } finally {
                DbUtil.close(conn);
            }
        }

    原文来自:https://www.51cto.com/article/742572.html

    本文地址:https://www.linuxprobe.com/hutool-db-configment.html编辑:王婷,审核员:逄增宝

    Linux命令大全:https://www.linuxcool.com/

    Linux系统大全:https://www.linuxdown.com/

    红帽认证RHCE考试心得:https://www.rhce.net/