关于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;}/* * 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.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;}/* * 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.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();