读书人

关于DAO设计的一个有关问题 (讨论)

发布时间: 2012-11-21 08:23:25 作者: rapoo

关于DAO设计的一个问题 (讨论)
假设有两个实体模型User 和 account .
那么在设计DAO的时候:
UserDAO里面放只涉及到User信息的数据库操作,AccountDAO里面放只涉及到Account的信息,
涉及到User和Account共同的信息全部放到UserAccountDAO里面.
不知道大家是怎么样设计的 com.java.client Client.javacom.java.dao AccountDao.java AccountDaoImpl.java UserDao.java UserDaoImpl.java DaoFactory.javacom.java.db Account.java User.javacom.java.service UserService.java

************************************
/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.db;import java.io.Serializable;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class User implements Serializable {private int userId;private String userName;private String firstName;private String lastName;private String nickName;private String email;}


Account类

/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.db;import java.io.Serializable;import java.util.Date;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class Account implements Serializable {private int accountId;private int userId;private String accountType;private Date createDate;private Date updateDate;}


省略get/set方法import java.util.List;import com.java.db.Account;import com.java.db.User;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public interface AccountDAO { public int saveAccount(Account account)throws Exception; public boolean delAccount(int accountId)throws Exception; public void updateAccount(Account account)throws Exception; public Account findAccountByID(int id)throws Exception; public User findUserByAccountId(int accountId)throws Exception; public List findAllAccount()throws Exception; public boolean batchDelAccount(int[] accountId)throws Exception;}


AccountDaoImpl


/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.dao;import java.sql.Connection;import java.util.List;import com.java.db.Account;import com.java.db.User;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class AccountDAOImpl implements AccountDAO {private Connection connection =null;public AccountDAOImpl(Connection connection) {this.connection =connection;// TODO Auto-generated constructor stub}/* (non-Javadoc) * @see com.java.dao.AccountDAO#saveAccount(com.java.db.Account) */public int saveAccount(Account account) {// TODO Auto-generated method stubreturn 0;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#delAccount(int) */public boolean delAccount(int accountId) {// TODO Auto-generated method stubreturn false;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#updateAccount(com.java.db.Account) */public void updateAccount(Account account) {// TODO Auto-generated method stub}/* (non-Javadoc) * @see com.java.dao.AccountDAO#findAccountByID(int) */public Account findAccountByID(int id) {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#findUserByAccountId(int) */public User findUserByAccountId(int accountId) {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#findAllAccount() */public List findAllAccount() {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#batchDelAccount(int[]) */public boolean batchDelAccount(int[] accountId) throws Exception {// TODO Auto-generated method stubreturn false;}}


UserDAO 类

/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.dao;import java.util.List;import com.java.db.User;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public interface UserDAO {public int saveUser(User user)throws Exception;public boolean delUser(int userId) throws Exception;public void updateUser(User user) throws Exception;public User findUserById(int userId) throws Exception;public List findUserByName(String userName) throws Exception;public List findAllUser()throws Exception;public List findAccountByUserId(int userId)throws Exception;}


UserDAOImpl类


/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.dao;import java.sql.Connection;import java.util.List;import com.java.db.Account;import com.java.db.User;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class AccountDAOImpl implements AccountDAO {private Connection connection =null;public AccountDAOImpl(Connection connection) {this.connection =connection;// TODO Auto-generated constructor stub}/* (non-Javadoc) * @see com.java.dao.AccountDAO#saveAccount(com.java.db.Account) */public int saveAccount(Account account) {// TODO Auto-generated method stubreturn 0;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#delAccount(int) */public boolean delAccount(int accountId) {// TODO Auto-generated method stubreturn false;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#updateAccount(com.java.db.Account) */public void updateAccount(Account account) {// TODO Auto-generated method stub}/* (non-Javadoc) * @see com.java.dao.AccountDAO#findAccountByID(int) */public Account findAccountByID(int id) {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#findUserByAccountId(int) */public User findUserByAccountId(int accountId) {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#findAllAccount() */public List findAllAccount() {// TODO Auto-generated method stubreturn null;}/* (non-Javadoc) * @see com.java.dao.AccountDAO#batchDelAccount(int[]) */public boolean batchDelAccount(int[] accountId) throws Exception {// TODO Auto-generated method stubreturn false;}}

/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.dao;import java.sql.Connection; /** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class DaoFactory { public static UserDAO getUserDAO(Connection connection) {return new UserDAOImpl(connection); } public static AccountDAO getAccountDAO(Connection connection) { return new AccountDAOImpl(connection); }}

UserService 类

/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.service;import java.sql.Connection;import java.sql.SQLException;import java.util.List;import com.java.db.Account;import com.java.db.User;import com.java.dao.AccountDAO;import com.java.dao.DaoFactory;import com.java.dao.UserDAO;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class UserService {  private Connection connection;private Connection getConnection(){   //to-do  open a connection form the dbPoolthis.connection =null; return connection;}public  User findUser(int userId){   User user = null ;try{ getConnection(); UserDAO userDAO = DaoFactory.getUserDAO(this.connection); user = userDAO.findUserById(userId);}catch(Exception e){e.printStackTrace();}finally{try {connection.close();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}return user; }public void delUser(int userId){try{ getConnection(); connection.setAutoCommit(false);  AccountDAO accountDao = DaoFactory.getAccountDAO(this.connection); UserDAO userDao =DaoFactory.getUserDAO(this.connection); List list = userDao.findAccountByUserId(userId); int[] accounts = new int[list.size()];  for(int i = 0;i<list.size();i++) { Account account =(Account)list.get(i); accounts[i]= account.getAccountId(); } accountDao.batchDelAccount(accounts); userDao.delUser(userId); connection.commit(); }catch(Exception e){e.printStackTrace();try {connection.rollback();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}finally{try {connection.close();} catch (SQLException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}}}}
/* * Created on Mar 6, 2007 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */package com.java.client;import com.java.service.UserService;/** * @author asdf * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */public class Client {public static void main(String[] args) { //定义用户Id int userId = 100; UserService userService = new UserService(); userService.delUser(userId); }}public class Action{ Connection cn = null; public void preHandler(){ } public void postHandler(){ } public void exceptionHandler(Exception e){ } Connection getConn(){ if(cn==null){cn = DBUtil.getConn();} } void closeConn(){ return DBUtil.close(cn); } public void run(){ try{ preHandler(); getConn(); execute(); postHandler(); }catch(Exception e){ exceptionHandler(e); }finally{closeConn();} } public void execute(){ }}

public class BaseAction extends Action{        //这里可实现 preHandler,postHadler,exceptionHandler   等方法            //连接获取和关闭由getConn()和closeConn()实现}




           XAction extends BaseAction{               //实现业务方法execute        }


Action action = new XAction();        action.run();


16 楼 ASDF1982 2007-03-16 看不太明白楼上的代码,有几个问题呀
1.这里的Action是否和struts等框架的action一样起到控制流程的作用,如果是这样的话那不太好吧,数据库的操作不应该和ation发生关系吧
2. 在哪里用的代理?
3. connection的创键和销毁怎样做才能不和dao以及service绑死呢
(有没有什么办法能象spring那样教给容器处理),提供一个组建呢代替spring的事务控制 17 楼 giscat 2007-03-16 ASDF1982 写道看不太明白楼上的代码,有几个问题呀
1.这里的Action是否和struts等框架的action一样起到控制流程的作用,如果是这样的话那不太好吧,数据库的操作不应该和ation发生关系吧
2. 在哪里用的代理?
3. connection的创键和销毁怎样做才能不和dao以及service绑死呢
(有没有什么办法能象spring那样教给容器处理),提供一个组建呢代替spring的事务控制


设计上是不合理,实践效果确很好

j2ee讲模式讲分层,理论上很上层次上境界
实战效果确输给ROR(这个东东可不管啥分层不分层的)

dao,service,action 呀,搞得一堆一堆
合久必分,分久必合
统统合并掉,啥烦恼都不会有了
唯一不能合的是mvc 三个部分分开(无论是物理上的还是逻辑上的分离)
维护性,扩展性自然而然就会上去
事情变得简单了,工作轻松了,质量上去了,成本下来了,票票进来了,老板也开心了

个人意见,仅供参考

18 楼 ASDF1982 2007-03-16 java是语言,但考虑到java企业应用是面象规范的,考虑到不同的产品很多.分层隔离虽然有点繁琐,但是在大项目中的时候,需要很多人合作编程的时候,需求可能变动的时候,还是必要的. 19 楼 giscat 2007-03-16 分层,MVC即可
至于其他的分层,设计的好,完全可以达到同样的效果

对于频繁变动,更不宜分层太多
一个修改,要去改N个地方,用struts/webwork/spring+hibernate/ibatis做系统的朋友肯定经历过
其实这样的架构,这样的设计不见得好
设计上有一个很重要的原则:扩展开放,修改封闭
一处修改,扩散污染到其他层(层次很深)的设计不见得好
水平方向的分层可细一些,垂直方向上的分层,要控制好层次
(绝不是越多越好)
MVC的垂直分层粒度已经比较适用了

20 楼 ASDF1982 2007-03-16 谢谢楼上的,
首先MVC只是个表示层模式而已
再次越是频繁变动的系统越应该分层,正因为分层了,引入了一些接口辅助模式,才不会改一处牵全身.
包括你说的开闭原则的理解也有问题哈,分层引入接口模式不正式为了符合开闭原则么?
你说的话完全反了,不太理解...

我的问题是怎样在jdbc的情况下没有Spring等容器,怎么设计实现spring所提供的事物管理 比如怎样设计自己 的组建!

读书人网 >软件架构设计

热点推荐