模拟hibernate框架

hibernate框架

  作为SSH三大框架之一的Hibernate,是用来把程序的Dao层和数据库打交道用的,它封装了JDBC的步骤,是我们对数据库的操作更加简单,更加快捷。利用Hibernate框架我们就可以不再编写重复的JDBC代码,不再反复的测试我们的SQL语句写的如何。这里这需要我们简单配置,调用框架给我们提供的方法,就可以完成对数据增删改查。

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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package com.zx.dao;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sun.xml.internal.bind.v2.model.core.ID;
import com.zx.annotation.Column;
import com.zx.annotation.Id;
import com.zx.annotation.Table;

//模拟Hibernate框架,实现数据的增 删 改 查等操作
public class HibernateDao<T> {


//根据clazz获取表名
public String getTableName(Class<?> clazz) {

// 获取类名作为默认表名
String tableName = clazz.getName();
// 获取类名上的注解
Table table = clazz.getAnnotation(Table.class);
if (table != null) {
// 类上有表名注解
tableName = table.name();
}
return tableName;
}

/*//定义方法获取列名
public String getColumnName(Field field) {

String columnName = field.getName();
//获取字段上的注解
Column column = field.getAnnotation(Column.class);

if(column != null) {
columnName = column.name();
}

return columnName;

}*/

// 保存数据
public void save(T t) {

Connection con =null;
PreparedStatement ps = null;
try {

// 注册数据库连接
Class.forName("com.mysql.jdbc.Driver");

// 获取连接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book", "root", "root");

// 创建StringBuffer对象来拼接sql语句 "insert into 表名(列1,列2,列3...) values(?,?,?...)"
StringBuffer sql = new StringBuffer();
// 获取表名
String tableName =this.getTableName(t.getClass());

//获取对象的class类型
Class<?> clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();

sql.append("insert into " + tableName + "(");

int len = 0;
// 遍历字段数组,拼接列名
for (int i = 0; i < fields.length; i++) {

// 获取字段上注解为id的
Id id = fields[i].getAnnotation(Id.class);
if (id != null) {
// 该字段为主键
continue;
}

len++;
String columnName = fields[i].getName();

// 获取字段上的注解
Column column = fields[i].getAnnotation(Column.class);
if (column != null) {
columnName = column.name();
}

sql.append(i == fields.length - 1 ? columnName + ")" : columnName + ",");

}

sql.append(" values(");
// 拼接values()
for (int i = 0; i < len; i++) {

sql.append(i == len - 1 ? "?)" : "?,");

}

System.out.println("sql:" + sql.toString());

// 获取PrepareStatement
ps = con.prepareStatement(sql.toString());
int num = 0;
for (int i = 0; i < fields.length; i++) {
/*
* fields[i+1].setAccessible(true);
*
* ps.setObject(i+1, fields[i+1].get(t));
* System.out.println("fields[i].get(t):"+fields[i+1].get(t));
*/

Id id = fields[i].getAnnotation(Id.class);
if (id != null) {
continue;
}
System.out.println("I:" + i);
// 根据字段名以及Class获取属性描述其对象
PropertyDescriptor pd = new PropertyDescriptor(fields[i].getName(), clazz);

// 获取字段对应的get方法
Method getMethod = pd.getReadMethod();

// 利用反射技术,调用方法
Object val = getMethod.invoke(t);

System.out.println("val:" + val);
ps.setObject(i, val);

}

// 执行sql
ps.executeUpdate();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(ps!=null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

// 删除数据 ,根据id删
public void delete(Class<?> clazz, int id) {

Connection con = null;
PreparedStatement ps = null;

try {
// 获取数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book", "root", "root");
// 准备sql语句 delete from 表名 where id = ?
// 获取表名
String tableName = this.getTableName(clazz);

StringBuffer sql = new StringBuffer();
sql.append("delete from " + tableName + " where id= ?");
System.out.println("del:" + sql.toString());
// 获取prepareStatement
ps = con.prepareStatement(sql.toString());
ps.setInt(1, id);
// 执行sql
ps.executeUpdate();

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}

// 查询 所有的数据
public List<T> query(Class<?> clazz) {

Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 获取数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book", "root", "root");
// 准备sql语句 select *from 表名
// 获取表 名
String tableName = this.getTableName(clazz);

StringBuffer sql = new StringBuffer();
sql.append("select *from " + tableName);
System.out.println("del:" + sql.toString());
// 获取prepareStatement
ps = con.prepareStatement(sql.toString());
// 执行sql
rs = ps.executeQuery();

// 创建集合用来封装数据
List<Object> list = new ArrayList<>();

// 获取所有字段
Field[] fields = clazz.getDeclaredFields();

while (rs.next()) {

// 创建实例
Object obj = clazz.newInstance();
for (int i = 0; i < fields.length; i++) {

//根据字段名以及Class获取属性描述其对象
PropertyDescriptor pd = new PropertyDescriptor(fields[i].getName(), clazz);

Method setMethod = pd.getWriteMethod();

Object val = rs.getObject(fields[i].getName());

setMethod.invoke(obj, val);

}
list.add(obj);
}

return (List<T>) list;

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}

// 查询数据 根据主键id查询数据
public T query(Class<?> clazz,int id) {
Connection con = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
// 获取数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book", "root", "root");
// 准备sql语句 select *from 表名
//获取表名
String tableName =this.getTableName(clazz);

StringBuffer sql = new StringBuffer();
sql.append("select *from " + tableName+" where id =? ");
System.out.println("del:" + sql.toString());
// 获取prepareStatement
ps = con.prepareStatement(sql.toString());
ps.setInt(1, id);
// 执行sql
rs = ps.executeQuery();

// 创建集合用来封装数据

// 获取所有字段
Field[] fields = clazz.getDeclaredFields();

while (rs.next()) {

// 创建实例
Object obj = clazz.newInstance();
for (int i = 0; i < fields.length; i++) {

// 根据字段名以及Class获取属性描述其对象
PropertyDescriptor pd = new PropertyDescriptor(fields[i].getName(), clazz);

Method setMethod = pd.getWriteMethod();

Object val = rs.getObject(fields[i].getName());

setMethod.invoke(obj, val);

}
return (T) obj;
}


} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return null;
}

//根据id更新数据 update student set sex='男' where id=4;
public void update(Class<?> clazz,int id) {

Connection con = null;
PreparedStatement ps = null;

try {
// 获取数据库驱动
Class.forName("com.mysql.jdbc.Driver");
// 获取连接
con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/book", "root", "root");
// 准备sql语句 select *from 表名

// 获取表名
String tableName = this.getTableName(clazz);

StringBuffer sql = new StringBuffer();
sql.append("update "+ tableName+" set sex='男' where id= ?");
System.out.println("del:" + sql.toString());
// 获取prepareStatement
ps = con.prepareStatement(sql.toString());
ps.setInt(1, id);
// 执行sql
ps.executeUpdate();


} catch (Exception e) {
// TODO: handle exception
}

}
}

-------------本文结束感谢您的阅读-------------