Dao
访问数据信息的类和接口,包括了对数据的CRUD,而不包含任何业务相关的信息。作用:为了实现功能的模块化,更有利于代码的维护和升级
BaseDao(基本的对数据库的操作)
public class BaseDao<T> {
private Class
<T> clazz
;
{
Type type
= this.getClass().getGenericSuperclass();
ParameterizedType paramType
= (ParameterizedType
) type
;
Type
[] typeArguments
= paramType
.getActualTypeArguments();
clazz
= (Class
<T>) typeArguments
[0];
}
public int update(Connection connection
, String sql
, Object
... args
) {
PreparedStatement statement
= null
;
try {
statement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< args
.length
; i
++) {
statement
.setObject(i
+ 1, args
[i
]);
}
return statement
.executeUpdate();
} catch (Exception e
) {
e
.printStackTrace();
} finally {
JDBCUtils
.closeResource(null
, statement
);
}
return -1;
}
public T
getInstance(Connection connection
, String sql
, Object
... args
) {
PreparedStatement statement
= null
;
ResultSet resultSet
= null
;
try {
statement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< args
.length
; i
++) {
statement
.setObject(i
+ 1, args
[i
]);
}
resultSet
= statement
.executeQuery();
ResultSetMetaData data
= resultSet
.getMetaData();
int count
= data
.getColumnCount();
if (resultSet
.next()) {
T t
= clazz
.newInstance();
for (int i
= 0; i
< count
; i
++) {
Object obj
= resultSet
.getObject(i
+ 1);
String columnName
= data
.getColumnLabel(i
+ 1);
Field field
= clazz
.getDeclaredField(columnName
);
field
.setAccessible(true);
field
.set(t
, obj
);
}
return t
;
}
} catch (Exception e
) {
e
.printStackTrace();
} finally {
JDBCUtils
.closeResource(null
, statement
, resultSet
);
}
return null
;
}
public List
<T> getList(Connection connection
, String sql
, Object
... args
) {
PreparedStatement statement
= null
;
ResultSet resultSet
= null
;
try {
statement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< args
.length
; i
++) {
statement
.setObject(i
+ 1, args
[i
]);
}
resultSet
= statement
.executeQuery();
ResultSetMetaData data
= resultSet
.getMetaData();
int count
= data
.getColumnCount();
ArrayList
<T> results
= new ArrayList<>();
while (resultSet
.next()) {
T t
= clazz
.newInstance();
for (int i
= 0; i
< count
; i
++) {
Object obj
= resultSet
.getObject(i
+ 1);
String columnName
= data
.getColumnLabel(i
+ 1);
Field field
= clazz
.getDeclaredField(columnName
);
field
.setAccessible(true);
field
.set(t
, obj
);
}
results
.add(t
);
}
return results
;
} catch (Exception e
) {
e
.printStackTrace();
} finally {
JDBCUtils
.closeResource(null
, statement
, resultSet
);
}
return null
;
}
public <E> E
getValue(Connection connection
, String sql
, Object
... args
) {
PreparedStatement statement
= null
;
ResultSet resultSet
= null
;
try {
statement
= connection
.prepareStatement(sql
);
for (int i
= 0; i
< args
.length
; i
++) {
statement
.setObject(i
+ 1, args
[i
]);
}
resultSet
= statement
.executeQuery();
if (resultSet
.next()) {
Object obj
= resultSet
.getObject(1);
return (E
) obj
;
}
} catch (SQLException e
) {
e
.printStackTrace();
} finally {
JDBCUtils
.closeResource(null
, statement
, resultSet
);
}
return null
;
}
}
CustomersDao(规范针对于Customers表的常用操作)
public interface CustomersDao {
void insert(Connection connection
, Customers customers
);
void deleteById(Connection connection
, int id
);
void update(Connection connection
, Customers customers
);
Customers
getByID(Connection connection
, int id
);
List
<Customers> getAll(Connection connection
);
Long
getCount(Connection connection
);
Date
getMaxBirth(Connection connection
);
}
CustomersDaoImpl(对于常规操作的具体实现)
public class CustomersDaoImpl extends BaseDao<Customers> implements CustomersDao {
@Override
public void insert(Connection connection
, Customers customers
) {
String sql
= "insert into customers (name,email,birth) values (?,?,?)";
update(connection
, sql
, customers
.getName(), customers
.getEmail(), customers
.getBirth());
}
@Override
public void deleteById(Connection connection
, int id
) {
String sql
= "delete from customers where id = ?";
update(connection
, sql
, id
);
}
@Override
public void update(Connection connection
, Customers customers
) {
String sql
= "update customers set name = ? , email = ? , birth = ? where id = ?";
update(connection
, sql
, customers
.getName(), customers
.getEmail(), customers
.getBirth(), customers
.getId());
}
@Override
public Customers
getByID(Connection connection
, int id
) {
String sql
= "select id,name,email,birth from customers where id = ?";
Customers customer
= getInstance(connection
, sql
, id
);
return customer
;
}
@Override
public List
<Customers> getAll(Connection connection
) {
String sql
= "select id,name,email,birth from customers";
List
<Customers> customers
= getList(connection
, sql
);
return customers
;
}
@Override
public Long
getCount(Connection connection
) {
String sql
= "select count(*) from customers";
Long count
= getValue(connection
, sql
);
return count
;
}
@Override
public Date
getMaxBirth(Connection connection
) {
String sql
= "select max(birth) from customers";
Date birth
= getValue(connection
, sql
);
return birth
;
}
}