DAO层实现示例 一:原生Jdbc


项目地址:
https://github.com/FateSolo/DAO-Test

目录:

该系列文章将展示六种不同的DAO层实现方法并总结其不同,本章使用JDK提供的原生级Jdbc完成DAO操作。

1. 示例代码:

1) 在dao包下创建子包impl,用来存放BookDao接口的不同实现类。

2) 创建BookDaoJdbcImpl.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.fatesolo.dao.impl;

import com.fatesolo.dao.BookDao;
import com.fatesolo.entity.Book;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository("bookDaoJdbcImpl")
public class BookDaoJdbcImpl implements BookDao {

@Resource
private DataSource dataSource;

@Override
public Book findById(int id) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;

String sql = "select name, author from book where id = ?";

try {
connection = dataSource.getConnection();

statement = connection.prepareStatement(sql);
statement.setInt(1, id);

rs = statement.executeQuery();
if (rs.next()) {
Book book = new Book();
book.setId(id);
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));

return book;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignored) {
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException ignored) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}

return null;
}

@Override
public List<Book> findByNameContaining(String name) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;

String sql = "select id, name, author from book where name like ?";

List<Book> books = new ArrayList<>();

try {
connection = dataSource.getConnection();

statement = connection.prepareStatement(sql);
statement.setString(1, "%" + name + "%");

rs = statement.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));

books.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignored) {
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException ignored) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}

return books;
}

@Override
public void save(Book book) {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;

String sql = "insert into book(name, author) values(?, ?)";

try {
connection = dataSource.getConnection();

statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, book.getName());
statement.setString(2, book.getAuthor());

statement.executeUpdate();

rs = statement.getGeneratedKeys();
if (rs.next()) {
book.setId(rs.getInt(1));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignored) {
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException ignored) {
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
}

}

3) 打开BookService.java,修改成员变量bookDao的注解:

1
@Resource(name = "bookDaoJdbcImpl")

2. 项目运行:

1) 使用Tomcat 8运行项目,使用curl工具添加一条数据:

1
curl -d "name=Test1&author=Fate" http://localhost:8080/book

得到如下返回:

1
Book{id=1, name='Test1', author='Fate'}

2) 访问http://localhost:8080/book/1 ,得到如下结果:

1
Book{id=1, name='Test1', author='Fate'}

3) 访问http://localhost:8080/book/name/Test ,得到如下结果:

1
[Book{id=1, name='Test1', author='Fate'}]

3. 总结

使用JDK提供的原生Jdbc,无需多余配置,可以直接使用DriverManager.getConnection,更推荐的是注入在spring-common.xml配置的DataSource,可以有不同的连接池实现,如druid、c3p0、dbcp等。

但可以看到代码中,开发者真正关注的逻辑代码只占一部分,剩下的都是对获取数据库连接、关闭连接、异常处理等操作,占据了较大的篇幅,并且每一个方法都需要进行这些相同的操作。在接下来的实现方式中,这些操作将被隐藏。


作者 [@FateSolo]
2017 年 03月 12日