DAO模式

      今天在写数据库层的时候用到了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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值