DBUtils資料查詢範例(使用BeanHandler)
前言
使用DBUtils可以使用BeanHandler來做資料查詢結果封裝,這也是楊藝比較喜歡的方式,BeanHandler主要是用來查詢單一筆搜尋結果使用,若要搜尋多筆結果,則得使用BeanListHandler。
實作
Java程式碼
Blog.java
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
114
115
116
117
118
119
120
121
122
123
124
125
| package cc.artyang.bean;
import java.io.Serializable;
import java.util.Date;
/**
* Blog Bean
*
* @author Art Yang
*
*/
public class Blog implements Serializable{
/** serialVersionUID */
private static final long serialVersionUID = 7961994280964175872L;
/** 部落格流水號 */
private Long blogSeqNo;
/** 部落格名稱 */
private String blogName;
/** 作者 */
private String author;
/** 備註 */
private String remark;
/** 建立時間 */
private Date createdTime;
/** 更新時間 */
private Date updatedTime;
/**
* @return {@link #blogSeqNo}
*/
public Long getBlogSeqNo() {
return this.blogSeqNo;
}
/**
* @param BlogSeqNo {@link #blogSeqNo}
*/
public void setBlogSeqNo(Long BlogSeqNo) {
this.blogSeqNo = BlogSeqNo;
}
/**
* @return {@link #blogName}
*/
public String getBlogName() {
return this.blogName;
}
/**
* @param BlogName {@link #blogName}
*/
public void setBlogName(String BlogName) {
this.blogName = BlogName;
}
/**
* @return {@link #createdTime}
*/
public Date getCreatedTime() {
return this.createdTime;
}
/**
* @param createdTime {@link #createdTime}
*/
public void setCreatedTime(Date createdTime) {
this.createdTime = createdTime;
}
/**
* @return {@link #updatedTime}
*/
public Date getUpdatedTime() {
return this.updatedTime;
}
/**
* @param updatedTime {@link #updatedTime}
*/
public void setUpdatedTime(Date updatedTime) {
this.updatedTime = updatedTime;
}
/**
* @return {@link #author}
*/
public String getAuthor() {
return this.author;
}
/**
* @param author {@link #author}
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return {@link #remark}
*/
public String getRemark() {
return this.remark;
}
/**
* @param remark {@link #remark}
*/
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Blog [blogSeqNo=" + this.blogSeqNo + ", blogName=" + this.blogName + ", author=" + this.author + ", remark=" + this.remark
+ ", createdTime=" + this.createdTime + ", updatedTime=" + this.updatedTime + "]";
}
}
|
DBUtilsSelectByBeanhandlerDemo.java
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
| package cc.artyang;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import cc.artyang.bean.Blog;
import cc.artyang.conf.DataSourceConf;
/**
* DBUtils 資料查詢 (使用 BeanHandler)
*
* @author Art Yang
*
*/
public class DBUtilsSelectByBeanhandlerDemo {
/**
* <p>主程式方法</p>
* <p>使用 BeanHandler 進行資料查詢會返回指定封裝好的 Bean,用於查詢一筆資料使用</p>
*
* @param args 外部參數
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String sqlStmt = "select * from blog where blogSeqNo = ?";
QueryRunner queryRunner = new QueryRunner(DataSourceConf.getDataSource());
Blog blog = queryRunner.query(sqlStmt, new BeanHandler<>(Blog.class), 1);
System.out.println(blog);
}
}
|
執行結果
1
| Blog [blogSeqNo=1, blogName=楊藝的洋溢生活, author=ArtYang, remark=生活記事、系統管理、程式設計、資料庫, createdTime=2019-05-01 13:40:57.0, updatedTime=null]
|