偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Java實戰(zhàn):hutool-db實現(xiàn)多數(shù)據(jù)源配置

數(shù)據(jù)庫 其他數(shù)據(jù)庫
Hutool-db是一個在JDBC基礎上封裝的數(shù)據(jù)庫操作工具類,通過包裝,使用ActiveRecord思想操作數(shù)據(jù)庫。

?我們在日常開發(fā)中,經(jīng)常會用到一個系統(tǒng)需要鏈接多個數(shù)據(jù)庫來實現(xiàn)業(yè)務的需求,比如多個系統(tǒng)之間數(shù)據(jù)調(diào)用、兩個數(shù)據(jù)之間同步等等。

今天給大家分享使用Hutool-db實現(xiàn)多數(shù)據(jù)源配置,大家一起來學習一下吧!

1、hutool-db介紹

Hutool-db是一個在JDBC基礎上封裝的數(shù)據(jù)庫操作工具類,通過包裝,使用ActiveRecord思想操作數(shù)據(jù)庫。在Hutool-db中,使用Entity(本質(zhì)上是個Map)代替Bean來使數(shù)據(jù)庫操作更加靈活,同時提供Bean和Entity的轉換提供傳統(tǒng)ORM的兼容支持。

圖片

1.  數(shù)據(jù)源 DataSource

2.  SQL執(zhí)行器 SqlExecutor

3.  CRUD的封裝 Db、SqlConnRunner SqlRunner

4.  支持事務的CRUD封裝 Session

5.  各種結果集處理類 handler

6.  數(shù)據(jù)庫的一些工具方法匯總 DbUtil

2、新建一個Maven項目

2.1 導入依賴包

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-db</artifactId>
<version>5.7.22</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.9</version>
</dependency>

2.2 新建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

2.3 新建測試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<Entity> 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<Entity> 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數(shù)據(jù)源 不推薦的方式
*/
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<Entity> 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);
}
}
責任編輯:武曉燕 來源: IT技術分享社區(qū)
相關推薦

2023-09-07 08:39:39

copy屬性數(shù)據(jù)源

2024-10-30 10:22:17

2020-12-31 07:55:33

spring bootMybatis數(shù)據(jù)庫

2020-11-24 09:56:12

數(shù)據(jù)源讀寫分離

2023-01-04 09:33:31

SpringBootMybatis

2017-07-21 14:50:15

數(shù)據(jù)庫DB分庫事務處理

2025-02-05 09:17:40

2009-06-15 13:24:46

JBoss數(shù)據(jù)源

2010-12-27 09:59:11

ODBC數(shù)據(jù)源

2023-06-07 08:08:37

MybatisSpringBoot

2023-10-18 15:25:29

數(shù)據(jù)源數(shù)據(jù)庫

2020-06-02 07:55:31

SpringBoot多數(shù)據(jù)源

2009-08-14 10:26:27

ibatis多數(shù)據(jù)源

2023-10-31 07:52:53

多數(shù)據(jù)源管理后端

2022-05-18 12:04:19

Mybatis數(shù)據(jù)源Spring

2025-04-14 01:00:00

Calcite電商系統(tǒng)MySQL

2022-05-10 10:43:35

數(shù)據(jù)源動態(tài)切換Spring

2020-03-13 14:05:14

SpringBoot+數(shù)據(jù)源Java

2014-11-20 09:47:06

Java

2024-11-20 09:12:56

點贊
收藏

51CTO技術棧公眾號