论坛首页 Java企业应用论坛

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

浏览 11103 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-03-06  
DAO
假设有两个实体模型User 和 account .
那么在设计DAO的时候:
   UserDAO里面放只涉及到User信息的数据库操作,AccountDAO里面放只涉及到Account的信息,
   涉及到User和Account共同的信息全部放到UserAccountDAO里面.
   不知道大家是怎么样设计的
   发表时间:2007-03-06  
方法:
1)在上面添加一个service层
2)直接在某个DAO中操作。
0 请登录后投票
   发表时间:2007-03-06  
方法:
1)在上面添加一个service层
2)直接在某个DAO中操作。
0 请登录后投票
   发表时间:2007-03-06  
涉及到共同的信息?难道User和Account是多对多的?不合常理阿?如果你指的是数据库方面的多个操作,应该写成Service
0 请登录后投票
   发表时间:2007-03-06  
一对多写了几行代码;改改吧
0 请登录后投票
   发表时间:2007-03-06  
包结构为
com.java.client
  Client.java
com.java.dao
  AccountDao.java
  AccountDaoImpl.java
  UserDao.java
  UserDaoImpl.java
  DaoFactory.java
com.java.db
  Account.java
  User.java
com.java.service
  UserService.java


************************************
 
0 请登录后投票
   发表时间:2007-03-06  
User 类

	/*
	 * 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方法
0 请登录后投票
   发表时间:2007-03-06  
AccountDao


/*
* 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.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 stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#delAccount(int)
	 */
	public boolean delAccount(int accountId) {
		// TODO Auto-generated method stub
		return 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 stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#findUserByAccountId(int)
	 */
	public User findUserByAccountId(int accountId) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#findAllAccount()
	 */
	public List findAllAccount() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#batchDelAccount(int[])
	 */
	public boolean batchDelAccount(int[] accountId) throws Exception {
		// TODO Auto-generated method stub
		return 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 stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#delAccount(int)
	 */
	public boolean delAccount(int accountId) {
		// TODO Auto-generated method stub
		return 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 stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#findUserByAccountId(int)
	 */
	public User findUserByAccountId(int accountId) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#findAllAccount()
	 */
	public List findAllAccount() {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.java.dao.AccountDAO#batchDelAccount(int[])
	 */
	public boolean batchDelAccount(int[] accountId) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

}

0 请登录后投票
   发表时间:2007-03-06  
DaoFactory
/*
 * 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 dbPool
		this.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 block
				e1.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 block
						e1.printStackTrace();
					}
				}
				finally
				{
					try 
					{
						connection.close();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				
	}

}
0 请登录后投票
   发表时间:2007-03-06  
Client客户端调用程序

/*
 * Created on Mar 6, 2007
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;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&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Client 
{

	public static void main(String[] args)
	 {
	 	//定义用户Id
	 	int userId = 100;
	 	UserService userService = new UserService();
	 	userService.delUser(userId);
	 	
	}
}
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics