DAO模式
来源:互联网 发布:mac适合的浏览器 编辑:程序博客网 时间:2024/05/22 17:50
今天在写数据库层的时候用到了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
0 0
- dao--2.dao模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- Dao模式
- DAO模式
- dao模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- DAO模式
- dao模式
- 取样问题
- Android Volley完全解析(一),初识Volley的基本用法
- vim的安装及部分问题解决方法
- malloc calloc realloc的函数原型和功能
- 使用FAAD库解码AAC实例及 及 faad解码后的通道数不正确的问题
- DAO模式
- java中遇到的对String的分割--------split(新手记录平日所遇到的,请大神海涵)
- Word Ladder II 解题总结
- ZooKeeper的三种角色
- Android开源框架Universal-Image-Loader详解
- mxnet代码解析之computation graph
- CodeForces 288C--异或运算
- poj 1996多项式计算
- 选择排序