今天在写数据库层的时候用到了DAO模式,发现这样就可以将实现类完全的分隔开来,并且把实现类的类名保存在配置文件中,这样的话即使我下次需要改变实现类,那么也只用将配置文件中实现类的信息修改即可。 例如,我刚开始数据库用的是XML来存储,实现类也是对于XML的操作,当我不想用XML的时候,将它替换成Mysql,那么我只需添加一个关于mysql的实现类,并在配置文件中添加该类的信息。
DAO(Data Access Object)模式就是写一个类,把访问数据库的代码封装起来。DAO在数据库与业务逻辑(Service)之间。
具体步骤如下:
· 实体域,即操作的对象,例如我们操作的表是user表,那么就需要先写一个User类;
·DAO模式需要先提供一个DAO接口;
· 然后再提供一个DAO接口的实现类;
·再编写一个DAO工厂,Service通过工厂来获取DAO实现。
User.java
public class User {
private String uid;
private String username;
private String password;
…
}
UserDao.java
public interface UserDao {
public void add(User user);
public void mod(User user);
public void del(String uid);
public User load(String uid);
public List<User> findAll();
}
UserDaoImpl.java
public class UserDaoImpl implements UserDao {
public void add(User user) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = JdbcUtils.getConnection();
String sql = "insert into user value(?,?,?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUid());
pstmt.setString(2, user.getUsername());
pstmt.setString(3, user.getPassword());
pstmt.executeUpdate();
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}
public void mod(User user) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = JdbcUtils.getConnection();
String sql = "update user set username=?, password=? where uid=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUsername());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getUid());
pstmt.executeUpdate();
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}
public void del(String uid) {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = JdbcUtils.getConnection();
String sql = "delete from user where uid=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, uid);
pstmt.executeUpdate();
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}
public User load(String uid) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = JdbcUtils.getConnection();
String sql = "select * from user where uid=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, uid);
rs = pstmt.executeQuery();
if(rs.next()) {
return new User(rs.getString(1), rs.getString(2), rs.getString(3));
}
return null;
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}
public List<User> findAll() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = JdbcUtils.getConnection();
String sql = "select * from user";
pstmt = con.prepareStatement(sql);
rs = pstmt.executeQuery();
List<User> userList = new ArrayList<User>();
while(rs.next()) {
userList.add(new User(rs.getString(1), rs.getString(2), rs.getString(3)));
}
return userList;
} catch(Exception e) {
throw new RuntimeException(e);
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch(SQLException e) {}
}
}
}
UserDaoFactory.java
public class UserDaoFactory {
private static UserDao userDao;
static {
try {
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("dao.properties");
Properties prop = new Properties();
prop.load(in);
String className = prop.getProperty("cn.dyf.jdbc.UserDao");
Class clazz = Class.forName(className);
userDao = (UserDao) clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static UserDao getUserDao() {
return userDao;
}
}
配置文件dao.properties
cn.dyf.jdbc.UserDao=cn.dyf.jdbc.UserDaoImpl