衣帶漸寬終不悔,為伊消得人憔悴--DbHelper增強版
阿新 • • 發佈:2018-12-21
於是又對此做了進一步的完善,以期能有一個更優雅的呈現。支援了更多的拓展和自定義行為。
用法大致和之前的第一個版本相當,但是也稍有不同,下面將以一個小案例來進行闡述。
核心理念
本工具以查詢為主要核心理念,即query操作為主,覆蓋了多種查詢方式的自動化執行流程。 其他諸如insert, update, delete以及事務操作,均由QueryRunner的update方法完成。
配備了自定義大小的資料庫連線池,提升程式執行的效率,並由底層自動維護連線池的釋放,申請問題。省心。
如何使用
- Step 1: 首先在專案的src目錄下建立一個db.cfg.xml的檔案,格式如下:
<?xml version="1.0" encoding="UTF-8" ?> <project> <database name="mysql"> <driver>com.mysql.jdbc.Driver</driver> <url>jdbc:mysql://localhost:3306/fams</url> <user>root</user> <password>mysql</password> <poolsize>20</poolsize> </database > ···</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
Step 2: 通過註冊的方式實現配置檔案中的資料來源的配置
Dbhelper.register()
即可Step 3: 主要的業務邏輯操作類
QueryRunner
,封裝了對資料庫JDBC操作的增刪改查等一系列的操作。 對於update方法的JDBC操作,我們無需手動的關閉資料庫連線,僅僅簡單的通過DbHelper的過載的release方法來實現對資料庫連線物件,查詢語句以及資料集的關閉操作,自動的維護資料庫連線池。是不是感覺很省心啊。
測試例項
這裡先根據資料庫中的表結構來建立一個對應的Bean物件吧。
/** * @Date 2016年11月19日 * * @author Administrator *//** * @author 郭璞 * */public class DateTest { private int id; private String name; private Date date; @Override public String toString() { return "DateTest [id=" + id + ", name=" + name + ", date=" + date + "]"; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
資料庫內詳細資料資訊
mysql> use test;Database changedmysql> select * from datetest;+----+----------+------------+| id | name | date |+----+----------+------------+| 1 | dlut | 2016-07-06 || 2 | 清華大學 | 2016-07-03 || 3 | 北京大學 | 2016-07-28 || 4 | Mark | 2016-08-20 || 5 | Tom | 2016-08-19 || 6 | Jane | 2016-08-21 |+----+----------+------------+6 rows in set (0.00 sec)mysql>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
測試程式碼
資料庫連線池測試
public static void main(String[] args) throws Exception { DbHelper helper = new DbHelper(); helper.register(); System.out.println(helper.connpool.size()); Connection conn = helper.getConnection(); System.out.println(helper.connpool.size()); Connection conn1 = helper.getConnection(); System.out.println(helper.connpool.size()); Connection conn2 = helper.getConnection(); System.out.println(helper.connpool.size()); System.out.println(conn.toString()); System.out.println(conn1.toString()); System.out.println(conn2.toString()); helper.release(conn); System.out.println(helper.connpool.size()); PreparedStatement stmt = conn1.prepareStatement("select * from user"); helper.release(conn2, stmt); System.out.println(helper.connpool.size()); PreparedStatement stmt1 = conn1.prepareStatement("select * from user"); ResultSet rs = stmt1.executeQuery(); helper.release(conn2, stmt1, rs); System.out.println(helper.connpool.size()); System.out.println(conn.toString()); System.out.println(conn1.toString()); System.out.println(conn2.toString()); }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
測試結果:
20191817dbhelper.DbHelper$MyConnection@28c97a5dbhelper.DbHelper$MyConnection@6659c656dbhelper.DbHelper$MyConnection@6d5380c2181920dbhelper.DbHelper$MyConnection@28c97a5dbhelper.DbHelper$MyConnection@6659c656dbhelper.DbHelper$MyConnection@6d5380c2
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
測試集
/** * @Date 2016年11月19日 * * @author 郭 璞 * */package mp;import java.sql.Connection;import java.util.List;import org.junit.Test;import dbhelper.DbHelper;import dbhelper.QueryRunner;import handlers.BeanHandler;import handlers.BeanListHandler;/** * @author 郭 璞 * */public class TestCase { @Test public void testSingleBean() throws Exception { System.out.println("-----------------testSingleBean-------------------------------------"); String sql = "select * from datetest where id=?"; QueryRunner runner = new QueryRunner(); DbHelper.register(); DbHelper helper = new DbHelper(); Connection conn = helper.getConnection(); DateTest datetest = runner.query(conn, sql, new BeanHandler<DateTest>(DateTest.class), 1); System.out.println(datetest.toString()); DbHelper.release(conn); } @Test public void testBeanList() throws Exception { System.out.println("-------------- testBeanList----------------------------------------"); String sql = "select * from datetest"; QueryRunner runner = new QueryRunner(); DbHelper.register(); DbHelper helper = new DbHelper(); Connection conn = helper.getConnection(); List<DateTest> dates = runner.query(conn, sql, new BeanListHandler<DateTest>(DateTest.class)); for (DateTest date : dates) { System.out.println(date.toString()); } DbHelper.release(conn); } @Test public void testBeanListWithParams1() throws Exception { System.out.println("--------------testBeanListWithParams1----------------------------------------"); String sql = "select * from datetest where id in (?, ?, ?)"; QueryRunner runner = new QueryRunner(); DbHelper.register(); DbHelper helper = new DbHelper(); Connection conn = helper.getConnection(); Object[] params = new Object[]{1, 3, 6}; List<DateTest> dates = runner.query(conn, sql, new BeanListHandler<DateTest>(DateTest.class), params); for (DateTest date : dates) { System.out.println(date.toString()); } DbHelper.release(conn); } @Test public void testBeanListWithParams2() throws Exception { System.out.println("--------------testBeanListWithParams2----------------------------------------"); String sql = "select * from datetest where id in (?, ?, ?) or name = ?"; QueryRunner runner = new QueryRunner(); DbHelper.register(); DbHelper helper = new DbHelper(); Connection conn = helper.getConnection(); Object[] params = new Object[]{1, 3, 6, "Mark"}; List<DateTest> dates = runner.query(conn, sql, new BeanListHandler<DateTest>(DateTest.class), params); for (DateTest date : dates) { System.out.println(date.toString()); } DbHelper.release(conn); } @Test public void testupdate() throws Exception { System.out.println("--------------testupdate----------------------------------------"); String sql = "update datetest set name=? where name=?"; QueryRunner runner = new QueryRunner(); DbHelper.register(); DbHelper helper = new DbHelper(); Connection conn = helper.getConnection(); Object[] params = new Object[]{"郭璞", "Mark"}; runner.update(conn, sql, params); DbHelper.release(conn); }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
測試結果:
-----------------testSingleBean-------------------------------------DateTest [id=1, name=dlut, date=Wed Jul 06 00:00:00 CST 2016]-------------- testBeanList----------------------------------------DateTest [id=1, name=dlut, date=Wed Jul 06 00:00:00 CST 2016]DateTest [id=2, name=清華大學, date=Sun Jul 03 00:00:00 CST 2016]DateTest [id=3, name=北京大學, date=Thu Jul 28 00:00:00 CST 2016]DateTest [id=4, name=郭璞, date=Sat Aug 20 00:00:00 CST 2016]DateTest [id=5, name=Tom, date=Fri Aug 19 00:00:00 CST 2016]DateTest [id=6, name=Jane, date=Sun Aug 21 00:00:00 CST 2016]--------------testBeanListWithParams1----------------------------------------DateTest [id=1, name=dlut, date=Wed Jul 06 00:00:00 CST 2016]DateTest [id=3, name=北京大學, date=Thu Jul 28 00:00:00 CST 2016]DateTest [id=6, name=Jane, date=Sun Aug 21 00:00:00 CST 2016]--------------testBeanListWithParams2----------------------------------------DateTest [id=1, name=dlut, date=Wed Jul 06 00:00:00 CST 2016]DateTest [id=3, name=北京大學, date=Thu Jul 28 00:00:00 CST 2016]DateTest [id=6, name=Jane, date=Sun Aug 21 00:00:00 CST 2016]--------------testupdate----------------------------------------
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
資料庫中對應更新結果:
mysql> use test;Database changedmysql> select * from datetest;+----+----------+------------+| id | name | date |+----+----------+------------+| 1 | dlut | 2016-07-06 || 2 | 清華大學 | 2016-07-03 || 3 | 北京大學 | 2016-07-28 || 4 | Mark | 2016-08-20 || 5 | Tom | 2016-08-19 || 6 | Jane | 2016-08-21 |+----+----------+------------+6 rows in set (0.00 sec)mysql> select * from datetest;+----+----------+------------+| id | name | date |+----+----------+------------+| 1 | dlut | 2016-07-06 || 2 | 清華大學 | 2016-07-03 || 3 | 北京大學 | 2016-07-28 || 4 | 郭璞 | 2016-08-20 || 5 | Tom | 2016-08-19 || 6 | Jane | 2016-08-21 |+----+----------+------------+6 rows in set (0.00 sec)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
延伸
由於原始碼可以隨意更改,所以你可以根據自己的需求來實現私人定製。僅僅需要實現自己的handler介面就可以了。 如果你看到了這篇文章,歡迎給我提issue。當然也可以修改成你自己的工具。