struts中action,公共接口,工厂,实现类(执行类)之间的关系

本文介绍了一个基于Struts框架的好友查询功能实现方案,详细解释了FriendFindAllAction类如何利用FriendDaoFactory创建FriendDaoImpl实例来完成好友数据的查询,并展示了具体的代码逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FriendFindAllAction类:

package com.relationinfo.txl.struts.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.relationinfo.txl.dao.FriendDao;
import com.relationinfo.txl.dto.Friend;
import com.relationinfo.txl.factory.FriendDaoFactory;
import com.relationinfo.txl.jdbc.FriendDaoImpl;

public class FriendFindAllAction extends Action
{
    
/**
     * Method 'execute'
     * 
     * 
@param mapping
     * 
@param form
     * 
@param request
     * 
@param response
     * 
@throws Exception
     * 
@return ActionForward
     
*/

    
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
    
{
        
try {
            
            FriendDao dao 
= FriendDaoFactory.create();  //create() 找FriendDaoFactory类里的public static FriendDao create()
//该方法实际上是声明了dao为FriendDaoImpl类的对象,即FriendDaoImpl dao = new FriendDaoImpl();

            
        
            
// ִ�в�ѯs
            Friend dto[] = dao.findAll();  //调用FriendDao类(公共接口)里的findAll()方法
        
            
// �洢���
            request.setAttribute( "result", dto );
        
            
return mapping.findForward( "success" );
        }

        
catch (Exception e) {
            ActionErrors _errors 
= new ActionErrors();
            _errors.add( ActionErrors.GLOBAL_ERROR, 
new ActionError("internal.error", e.getClass().getName() + "" + e.getMessage() ) );
            saveErrors( request, _errors );
            
return mapping.findForward( "failure" );
        }


    }


}

 

FriendDaoFactory类:

package com.relationinfo.txl.factory;

import java.sql.Connection;

import com.relationinfo.txl.dao.FriendDao;
import com.relationinfo.txl.jdbc.FriendDaoImpl;

public class FriendDaoFactory {
    
private static FriendDaoImpl dao = new FriendDaoImpl();  //FriendDaoImpl 类是执行类(执行对数据库的操作)

    
/**
     * Method 'create'
     * 
     * 
@return FriendDao
     
*/

    
public static FriendDao create() {          return dao;  //dao找此类中的private static FriendDaoImpl dao = new FriendDaoImpl();中的dao 
//通过FriendDao 与接口相联系(FriendDao 是公共接口)

    }


    
/**
     * Method 'create'
     * 
     * 
@param conn
     * 
@return FriendDao
     
*/

    
public static FriendDao create(Connection conn) {
        
return new FriendDaoImpl(conn);
    }


}

 

 

FriendDaoImpl类:

 


package com.relationinfo.txl.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.relationinfo.txl.dao.FriendDao;
import com.relationinfo.txl.dto.Friend;
import com.relationinfo.txl.dto.FriendPk;
import com.relationinfo.txl.exceptions.FriendDaoException;

public class FriendDaoImpl extends AbstractDataAccessObject implements FriendDao
{

    
protected java.sql.Connection userConn;

    
protected static  Logger logger = Logger.getLogger( FriendDaoImpl.class );

    
/** 
     * All finder methods in this class use this SELECT constant to build their queries
     
*/

    
protected  String SQL_SELECT = "SELECT xmjp, groupid, name, age, sex, Professional, Scholarship, address, email, phone, note FROM " + getTableName() + "";

    
/** 
     * Finder methods will pass this value to the JDBC setMaxRows method
     
*/

    
private int maxRows;

    
/** 
     * SQL INSERT ���
     
*/

    
protected  String SQL_INSERT = "INSERT INTO " + getTableName() + " ( xmjp, groupid, name, age, sex, Professional, Scholarship, address, email, phone, note ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )";

    
/** 
     * SQL UPDATE ���
     
*/

    
protected  String SQL_UPDATE = "UPDATE " + getTableName() + " SET xmjp = ?, groupid = ?, name = ?, age = ?, sex = ?, Professional = ?, Scholarship = ?, address = ?, email = ?, phone = ?, note = ? WHERE xmjp = ?";

    
/** 
     * SQL DELETE ���
     
*/

    
protected  String SQL_DELETE = "DELETE FROM " + getTableName() + " WHERE xmjp = ?";

    
/** 
     * ����� xmjp
     
*/

    
protected static  int COLUMN_XMJP = 1;

    
/** 
     * ����� groupid
     
*/

    
protected static  int COLUMN_GROUPID = 2;

    
/** 
     * ����� name
     
*/

    
protected static  int COLUMN_NAME = 3;

    
/** 
     * ����� age
     
*/

    
protected static  int COLUMN_AGE = 4;

    
/** 
     * ����� sex
     
*/

    
protected static  int COLUMN_SEX = 5;

    
/** 
     * ����� Professional
     
*/

    
protected static  int COLUMN_PROFESSIONAL = 6;

    
/** 
     * ����� Scholarship
     
*/

    
protected static  int COLUMN_SCHOLARSHIP = 7;

    
/** 
     * ����� address
     
*/

    
protected static  int COLUMN_ADDRESS = 8;

    
/** 
     * ����� email
     
*/

    
protected static  int COLUMN_EMAIL = 9;

    
/** 
     * ����� phone
     
*/

    
protected static  int COLUMN_PHONE = 10;

    
/** 
     * ����� note
     
*/

    
protected static  int COLUMN_NOTE = 11;

    
/** 
     * �е���
     
*/

    
protected static  int NUMBER_OF_COLUMNS = 11;

    
/** 
     * �������� xmjp
     
*/

    
protected static  int PK_COLUMN_XMJP = 1;

    
/** 
     * ����¼�¼�� friend table.
     
*/

    
public FriendPk insert(Friend dto) throws FriendDaoException
    
{
        
long t1 = System.currentTimeMillis();
        
//�������
         boolean isConnSupplied = (userConn != null);
        Connection conn 
= null;
        CallableStatement stmt 
= null;
        ResultSet rs 
= null;
        
        
try {
            
// ����û�ָ����connection ���ߴ�ResourceManager���l��
            conn = isConnSupplied ? userConn : ResourceManager.getConnection();
        
            stmt 
= conn.prepareCall( SQL_INSERT );
            stmt.setString( COLUMN_XMJP, dto.getXmjp() );
            stmt.setString( COLUMN_GROUPID, dto.getGroupid() );
            stmt.setString( COLUMN_NAME, dto.getName() );
            stmt.setString( COLUMN_AGE, dto.getAge() );
            stmt.setString( COLUMN_SEX, dto.getSex() );
            stmt.setString( COLUMN_PROFESSIONAL, dto.getProfessional() );
            stmt.setString( COLUMN_SCHOLARSHIP, dto.getScholarship() );
            stmt.setString( COLUMN_ADDRESS, dto.getAddress() );
            stmt.setString( COLUMN_EMAIL, dto.getEmail() );
            stmt.setString( COLUMN_PHONE, dto.getPhone() );
            stmt.setString( COLUMN_NOTE, dto.getNote() );
            
if (logger.isDebugEnabled()) {
                logger.debug( 
"Executing " + SQL_INSERT + " with DTO: " + dto);
            }

        
            stmt.execute();
            
int rows = stmt.getUpdateCount();
            
if (logger.isDebugEnabled()) {
                logger.debug( rows 
+ " rows affected");
            }

        
            
return dto.createPk();
        }

        
catch (SQLException _e) {
            logger.error( 
"SQLException: " + _e.getMessage(), _e );
            
throw new FriendDaoException( "SQLException: " + _e.getMessage(), _e );
        }

        
catch (Exception _e) {
            logger.error( 
"Exception: " + _e.getMessage(), _e );
            
throw new FriendDaoException( "Exception: " + _e.getMessage(), _e );
        }

        
finally {
            ResourceManager.close(stmt);
            
if (!isConnSupplied) {
                ResourceManager.close(conn);
            }

        
        }

        
    }


    
/** 
     * ���µ��ʼ�¼.
     
*/

    
public void update(FriendPk pk, Friend dto) throws FriendDaoException
    
{
        
long t1 = System.currentTimeMillis();
        
//�������
         boolean isConnSupplied = (userConn != null);
        Connection conn 
= null;
        PreparedStatement stmt 
= null;
        
        
try {
            
// ����û�ָ����connection ���ߴ�ResourceManager���l��
            conn = isConnSupplied ? userConn : ResourceManager.getConnection();
        
            
if (logger.isDebugEnabled()) {
                logger.debug( 
"Executing " + SQL_UPDATE + " with DTO: " + dto);
            }

        
            stmt 
= conn.prepareStatement( SQL_UPDATE );
            
int index=1;
            stmt.setString( index
++, dto.getXmjp() );
            stmt.setString( index
++, dto.getGroupid() );
            stmt.setString( index
++, dto.getName() );
            stmt.setString( index
++, dto.getAge() );
            stmt.setString( index
++, dto.getSex() );
            stmt.setString( index
++, dto.getProfessional() );
            stmt.setString( index
++, dto.getScholarship() );
            stmt.setString( index
++, dto.getAddress() );
            stmt.setString( index
++, dto.getEmail() );
            stmt.setString( index
++, dto.getPhone() );
            stmt.setString( index
++, dto.getNote() );
            stmt.setString( 
12, pk.getXmjp() );
            
int rows = stmt.executeUpdate();
            
long t2 = System.currentTimeMillis();
            
if (logger.isDebugEnabled()) {
                logger.debug( rows 
+ " rows affected (" + (t2-t1) + " ms)");
            }

        
        }

        
catch (SQLException _e) {
            logger.error( 
"SQLException: " + _e.getMessage(), _e );
            
throw new FriendDaoException( "SQLException: " + _e.getMessage(), _e );
        }

        
catch (Exception _e) {
            logger.error( 
"Exception: " + _e.getMessage(), _e );
            
throw new FriendDaoException( "Exception: " + _e.getMessage(), _e );
        }

        
finally {
            ResourceManager.close(stmt);
            
if (!isConnSupplied) {
                ResourceManager.close(conn);
            }

        
        }

        
    }


    
/** 
     * ɾ���¼.
     
*/

    
public void delete(FriendPk pk) throws FriendDaoException
    
{
        
long t1 = System.currentTimeMillis();
        
//�������
         boolean isConnSupplied = (userConn != null);
        Connection conn 
= null;
        PreparedStatement stmt 
= null;
        
        
try {
            
// ����û�ָ����connection ���ߴ�ResourceManager���l��
            conn = isConnSupplied ? userConn : ResourceManager.getConnection();
        
            
if (logger.isDebugEnabled()) {
                logger.debug( 
"Executing " + SQL_DELETE + " with PK: " + pk);
            }

        
            stmt 
= conn.prepareStatement( SQL_DELETE );
            stmt.setString( 
1, pk.getXmjp() );
            
int rows = stmt.executeUpdate();
            
long t2 = System.currentTimeMillis();
            
if (logger.isDebugEnabled()) {
                logger.debug( rows 
+ " rows affected (" + (t2-t1) + " ms)");
            }

        
        }

        
catch (SQLException _e) {
            logger.error( 
"SQLException: " + _e.getMessage(), _e );
            
throw new FriendDaoException( "SQLException: " + _e.getMessage(), _e );
        }

        
catch (Exception _e) {
            logger.error( 
"Exception: " + _e.getMessage(), _e );
            
throw new FriendDaoException( "Exception: " + _e.getMessage(), _e );
        }

        
finally {
            ResourceManager.close(stmt);
            
if (!isConnSupplied) {
                ResourceManager.close(conn);
            }

        
        }

        
    }


    
/** 
     * ���ط��ָ�����ֵ�ļ�¼.
     
*/

    
public Friend findByPrimaryKey(FriendPk pk) throws FriendDaoException
    
{
        
return findByPrimaryKey( pk.getXmjp() );
    }


    
/** 
     * �������еļ�¼��������� 'xmjp = :xmjp'.
     
*/

    
public Friend findByPrimaryKey(String xmjp) throws FriendDaoException
    
{
        Friend ret[] 
= findByDynamicSelect( SQL_SELECT + " WHERE xmjp = ?"new Object[] { xmjp } );
        
return ret.length==0 ? null : ret[0];
    }


    
/** 
     * �������еļ�¼��������� ''.
     
*/

    
public Friend[] findAll() throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " ORDER BY xmjp"null );
    }


    
/** 
     * �������еļ�¼��������� 'groupid = :groupid'.
     
*/

    
public Friend[] findByGroup1(String groupid) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE groupid = ?"new Object[] { groupid } );
    }


    
/** 
     * �������еļ�¼��������� 'xmjp = :xmjp'.
     
*/

    
public Friend[] findWhereXmjpEquals(String xmjp) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE xmjp = ? ORDER BY xmjp"new Object[] { xmjp } );
    }


    
/** 
     * �������еļ�¼��������� 'groupid = :groupid'.
     
*/

    
public Friend[] findWhereGroupidEquals(String groupid) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE groupid = ? ORDER BY groupid"new Object[] { groupid } );
    }


    
/** 
     * �������еļ�¼��������� 'name = :name'.
     
*/

    
public Friend[] findWhereNameEquals(String name) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE name = ? ORDER BY name"new Object[] { name } );
    }


    
/** 
     * �������еļ�¼��������� 'age = :age'.
     
*/

    
public Friend[] findWhereAgeEquals(String age) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE age = ? ORDER BY age"new Object[] { age } );
    }


    
/** 
     * �������еļ�¼��������� 'sex = :sex'.
     
*/

    
public Friend[] findWhereSexEquals(String sex) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE sex = ? ORDER BY sex"new Object[] { sex } );
    }


    
/** 
     * �������еļ�¼��������� 'Professional = :professional'.
     
*/

    
public Friend[] findWhereProfessionalEquals(String professional) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE Professional = ? ORDER BY Professional"new Object[] { professional } );
    }


    
/** 
     * �������еļ�¼��������� 'Scholarship = :scholarship'.
     
*/

    
public Friend[] findWhereScholarshipEquals(String scholarship) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE Scholarship = ? ORDER BY Scholarship"new Object[] { scholarship } );
    }


    
/** 
     * �������еļ�¼��������� 'address = :address'.
     
*/

    
public Friend[] findWhereAddressEquals(String address) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE address = ? ORDER BY address"new Object[] { address } );
    }


    
/** 
     * �������еļ�¼��������� 'email = :email'.
     
*/

    
public Friend[] findWhereEmailEquals(String email) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE email = ? ORDER BY email"new Object[] { email } );
    }


    
/** 
     * �������еļ�¼��������� 'phone = :phone'.
     
*/

    
public Friend[] findWherePhoneEquals(String phone) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE phone = ? ORDER BY phone"new Object[] { phone } );
    }


    
/** 
     * �������еļ�¼��������� 'note = :note'.
     
*/

    
public Friend[] findWhereNoteEquals(String note) throws FriendDaoException
    
{
        
return findByDynamicSelect( SQL_SELECT + " WHERE note = ? ORDER BY note"new Object[] { note } );
    }


    
/**
     * Method 'FriendDaoImpl'
     * 
     
*/

    
public FriendDaoImpl()
    
{
    }


    
/**
     * Method 'FriendDaoImpl'
     * 
     * 
@param userConn
     
*/

    
public FriendDaoImpl( java.sql.Connection userConn)
    
{
        
this.userConn = userConn;
    }


    
/** 
     * ���� maxRows
     
*/

    
public void setMaxRows(int maxRows)
    
{
        
this.maxRows = maxRows;
    }


    
/** 
     * ��� maxRows
     
*/

    
public int getMaxRows()
    
{
        
return maxRows;
    }


    
/**
     * Method 'getTableName'
     * 
     * 
@return String
     
*/

    
public String getTableName()
    
{
        
return "friend";
    }


    
/** 
     * �ӽ���л�õ���
     
*/

    
protected Friend fetchSingleResult(ResultSet rs) throws SQLException
    
{
        
if (rs.next()) {
            Friend dto 
= new Friend();
            populateDto( dto, rs);
            
return dto;
        }
 else {
            
return null;
        }

        
    }


    
/** 
     * �ӽ���л�ö���
     
*/

    
protected Friend[] fetchMultiResults(ResultSet rs) throws SQLException
    
{
        Collection resultList 
= new ArrayList();
        
while (rs.next()) {
            Friend dto 
= new Friend();
            populateDto( dto, rs);
            resultList.add( dto );
        }

        
        Friend ret[] 
= new Friend[ resultList.size() ];
        resultList.toArray( ret );
        
return ret;
    }


    
/** 
     * ������е���ݸ��Ƹ�DTO
     
*/

    
protected void populateDto(Friend dto, ResultSet rs) throws SQLException
    
{
        dto.setXmjp( rs.getString( COLUMN_XMJP ) );
        dto.setGroupid( rs.getString( COLUMN_GROUPID ) );
        dto.setName( rs.getString( COLUMN_NAME ) );
        dto.setAge( rs.getString( COLUMN_AGE ) );
        dto.setSex( rs.getString( COLUMN_SEX ) );
        dto.setProfessional( rs.getString( COLUMN_PROFESSIONAL ) );
        dto.setScholarship( rs.getString( COLUMN_SCHOLARSHIP ) );
        dto.setAddress( rs.getString( COLUMN_ADDRESS ) );
        dto.setEmail( rs.getString( COLUMN_EMAIL ) );
        dto.setPhone( rs.getString( COLUMN_PHONE ) );
        dto.setNote( rs.getString( COLUMN_NOTE ) );
    }


    
/** 
     * ����ض���SQL��䷵��������������ĺ��Ѽ�¼
     
*/

    
public Friend[] findByDynamicSelect(String sql, Object[] sqlParams) throws FriendDaoException
    
{
        
//�������
         boolean isConnSupplied = (userConn != null);
        Connection conn 
= null;
        PreparedStatement stmt 
= null;
        ResultSet rs 
= null;
        
        
try {
            
// ����û�ָ����connection ���ߴ�ResourceManager���l��
            conn = isConnSupplied ? userConn : ResourceManager.getConnection();
        
            
// construct the SQL statement
             String SQL = sql;
        
        
            
if (logger.isDebugEnabled()) {
                logger.debug( 
"Executing " + SQL);
            }

        
            
//׼�� statement
            stmt = conn.prepareStatement( SQL );
            stmt.setMaxRows( maxRows );
        
            
//
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值