DAO.java
package
com
.
atguigu
.
java2
;
import
java
.
lang
.
reflect
.*;
import
java
.
sql
.*;
import
java
.
util
.*;
import
org
.
apache
.
commons
.
dbutils
.*;
import
org
.
apache
.
commons
.
dbutils
.
handlers
.*;
class
ReflectionUtils
{
//
如何获取形参
clazz
的父类的泛型
public
static
Class getSuperClassGeneric
(
Class clazz
)
{
Type t
=
clazz
.
getGenericSuperclass
();
ParameterizedType type
=
(
ParameterizedType
)
t
;
Type
[]
types
=
type
.
getActualTypeArguments
();
return
(
Class
)
types
[
0
];
}
}
public
class
DAO
<
T
>
{
Class
<
T
>
clazz
=
null
;
public
DAO
()
{
//
此处的
this
相当于正在创建的
customerDAO
对象
clazz
=
ReflectionUtils
.
getSuperClassGeneric
(
this
.
getClass
());
}
//
查询某些特殊数值的数据
public
<
E
>
E getValue
(
Connection conn
,
String sql
,
Object
...
args
){
try
{
ResultSetHandler
<
Object
>
handler
=
new
ScalarHandler
();
QueryRunner runner
=
new
QueryRunner
();
Object obj
=
runner
.
query
(
conn
,
sql
,
handler
,
args
);
return
(
E
)
obj
;
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
return
null
;
}
//
通用的查询方法,返回多个对象的集合
public
List
<
T
>
getForList
(
Connection conn
,
String sql
,
Object
...
args
)
{
QueryRunner runner
=
new
QueryRunner
();
try
{
ResultSetHandler
<
List
<
T
>>
handler
=
new
BeanListHandler
<
T
>(
clazz
);
List
<
T
>
list
=
runner
.
query
(
conn
,
sql
,
handler
);
return
list
;
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
return
null
;
}
//
通用的查询方法,返回一个对象
public
T getInstance
(
Connection conn
,
String sql
,
Object
...
args
)
{
try
{
QueryRunner runner
=
new
QueryRunner
();
ResultSetHandler
<
T
>
handler
=
new
BeanHandler
<
T
>(
clazz
);
return
runner
.
query
(
conn
,
sql
,
handler
,
args
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
return
null
;
}
//
通用的增、删、改的方法(
version 3.0
)
public
void
update
(
Connection conn
,
String sql
,
Object
...
args
)
{
try
{
QueryRunner runner
=
new
QueryRunner
();
runner
.
update
(
conn
,
sql
,
args
);
}
catch
(
SQLException e
)
{
e
.
printStackTrace
();
}
}
}
package
com
.
atguigu
.
java2
;
import
java
.
sql
.
Connection
;
import
java
.
sql
.
Date
;
import
java
.
util
.
List
;
public
class
CustomerDAO
extends
DAO
<
Customer
>
{
/**
*
*
获取
customers
表中的最大的日期
* @param conn
* @return
*/
public
Date getMaxBirth
(
Connection conn
){
String sql
=
"select max(birth) from customers"
;
return
(
Date
)
getValue
(
conn
,
sql
);
}
/**
*
获取
customers
表中的数据的条目数
* @param conn
* @return
*/
public
long
getCount
(
Connection conn
){
String sql
=
"select count(*) from customers"
;
return
(
long
)
getValue
(
conn
,
sql
);
}
/**
*
获取
customers
表中的所有数据
* @param conn
* @return
*/
public
List
<
Customer
>
getAll
(
Connection conn
){
String sql
=
"select id,name,email,birth from customers"
;
return
getForList
(
conn
,
sql
);
}
/**
*
按照指定的
id
查询表中的一条数据,以对象的形式返回
* @param conn
* @param custId
* @return
*/
public
Customer query
(
Connection conn
,
int
custId
){
String sql
=
"select id,name,email,birth from customers where id = ?"
;
return
getInstance
(
conn
,
sql
,
custId
);
}
/**
*
删除
customers
表中指定
id
的数据
* @param conn
* @param custId
:要删除的指定的数据的
id
号
*/
public
void
delete
(
Connection conn
,
int
custId
){
String sql
=
"delete from customers where id = ?"
;
update
(
conn
,
sql
,
custId
);
}
/**
*
修改
customers
表中的一条数据
* @param conn
* @param cust
:将表中与
cust
的
id
相同的数据改为
cust
的信息
*/
public
void
update
(
Connection conn
,
Customer cust
)
{
String sql
=
"update customers set name = ?,email = ?,birth = ? where id = ?"
;
update
(
conn
,
sql
,
cust
.
getName
(),
cust
.
getEmail
(),
cust
.
getBirth
(),
cust
.
getId
());
}
/**
*
向
customers
表中插入一条数据
* @param conn
:数据库的连接
* @param cust
:将此对象添加到
customers
表中
*/
public
void
insert
(
Connection conn
,
Customer cust
)
{
String sql
=
"insert into customers(name,email,birth) values(?,?,?)"
;
update
(
conn
,
sql
,
cust
.
getName
(),
cust
.
getEmail
(),
cust
.
getBirth
());
}
}
package
com
.
atguigu
.
java2
;
import
java
.
sql
.
Connection
;
import
java
.
sql
.
Date
;
import
java
.
util
.
List
;
import
org
.
junit
.
Test
;
import
com
.
atguigu
.
java
.
JDBCUtils
;
public
class
TestCustomerDAO
{
CustomerDAO custDao
=
new
CustomerDAO
();
//
查询年龄最小的日期
@Test
public
void
testGetMaxBirth
(){
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
Date birth
=
custDao
.
getMaxBirth
(
conn
);
System
.
out
.
println
(
birth
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
//
查询
customers
表中的人数
@Test
public
void
testGetCount
(){
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
long
count
=
custDao
.
getCount
(
conn
);
System
.
out
.
println
(
count
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
//
查询
customers
表中所有数据
@Test
public
void
testQuery1
(){
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
List
<
Customer
>
list
=
custDao
.
getAll
(
conn
);
for
(
Customer c
:
list
){
System
.
out
.
println
(
c
);
}
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
//
查询
id=20
的
一条数据
@Test
public
void
testQuery
(){
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
Customer cust
=
custDao
.
query
(
conn
,
20
);
System
.
out
.
println
(
cust
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
//
删除数据
@Test
public
void
testDelete
(){
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
custDao
.
delete
(
conn
,
18
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
//
更新数据
@Test
public
void
testUpdate
()
{
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
new
java
.
util
.
Date
().
getTime
()));
custDao
.
update
(
conn
,
cust
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
//
插入数据
@Test
public
void
testInsert
()
{
Connection conn
=
null
;
try
{
conn
=
JDBCUtils
.
getConnection
();
new
java
.
util
.
Date
().
getTime
()));
custDao
.
insert
(
conn
,
cust
);
}
catch
(
Exception e
)
{
e
.
printStackTrace
();
}
finally
{
JDBCUtils
.
close
(
null
,
null
,
conn
);
}
}
}