自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

?我們?cè)谌粘i_(kāi)發(fā)中,經(jīng)常會(huì)用到一個(gè)系統(tǒng)需要鏈接多個(gè)數(shù)據(jù)庫(kù)來(lái)實(shí)現(xiàn)業(yè)務(wù)的需求,比如多個(gè)系統(tǒng)之間數(shù)據(jù)調(diào)用、兩個(gè)數(shù)據(jù)之間同步等等。

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

1、hutool-db介紹

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

圖片

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

2.  SQL執(zhí)行器 SqlExecutor

3.  CRUD的封裝 Db、SqlConnRunner SqlRunner

4.  支持事務(wù)的CRUD封裝 Session

5.  各種結(jié)果集處理類(lèi) handler

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

2、新建一個(gè)Maven項(xiàng)目

2.1 導(dǎo)入依賴(lài)包

<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 新建測(cè)試demo

/**
* 測(cè)試mysql
*/
private static void testMysql() {
DataSource ds = DSFactory.get("mysql");
Db.use(ds);
Connection conn = null;
try {
conn = ds.getConnection();
// 插入語(yǔ)句
SqlExecutor.execute(conn, "insert into t_user (name,age) value ('小張',35)");
// 更新語(yǔ)句
SqlExecutor.execute(conn, "update t_user set name='小明002' where id=2 ");
// 刪除語(yǔ)句
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);
}
}

/**
* 測(cè)試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);
}
}

/**
* 直接代碼寫(xiě)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);
}
}
責(zé)任編輯:武曉燕 來(lái)源: IT技術(shù)分享社區(qū)
相關(guān)推薦

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ù)庫(kù)

2020-11-24 09:56:12

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

2023-01-04 09:33:31

SpringBootMybatis

2017-07-21 14:50:15

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

2025-02-05 09:17:40

2010-12-27 09:59:11

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

2009-06-15 13:24:46

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

2023-06-07 08:08:37

MybatisSpringBoot

2023-10-18 15:25:29

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

2009-08-14 10:26:27

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

2020-06-02 07:55:31

SpringBoot多數(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ù)源動(dòng)態(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

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)