⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 day04_1.txt

📁 网络购物系统开发详细步骤
💻 TXT
字号:
1,创建为Category表提供primary key 的 sequence 表
  create table Sequence (
       pKey   number(1) primary key,
       value  number(1000,0) not null 
);

insert into sequence ( pKey,value ) values( 1 , 0 );

创建Category表

create table category(
      categoryid     varchar2(16)  primary key,
      name       varchar2(16)  not null,
      description  varchar2(64)
);


在dto 包中 新建 CategoryDTO class

=========================================================================================

package dto;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class CategoryDTO {
  private Integer id;
  private String  name;
  private String  desc;

  public CategoryDTO(Integer id,String name,String desc) {
    this.id = id;
    this.name = name;
    this.desc = desc;
  }

  public Integer getId(){
    return this.id;
  }
  public void setId( Integer id ){
    this.id = id;
  }

  public String getName(){
    return this.name;
  }
  public void setName( String name ){
    this.name = name;
  }

  public String getDesc(){
    return this.desc;
  }
  public void setDesc( String desc ){
    this.desc = desc;
  }

}

=======================================================================================================================

2, 刷新JBUILDER中的数据源,看到 sequence 表
3,右健点击sequence 表, ---> new cmp 2.0 Entity Bean
   name                : SequenceEJB
   abstract schema name: Sequence
   interface           :local
   always wrap primary key : false
   --> class and package
   package    : ejbs
   Bean class : ejbs.SequenceBean
   local home : ejbs.SequenceLocalHome
   Local      : ejbs.SequenceLocal

====================================================================================================================
4,修改属性:
   pkey :
      is primary key : true
      column name    : PKEY
   value:
      is primary key : false
      column name    : VALUE

=====================================================================================================================

4, 注释 SequenceLocalHome中的 create() 方法。

=====================================================================================================================

5,右键点击SequenceEJB ---> add ---> method
   name : getSequence
   return type : int
   interface : local

=====================================================================================================================

6, 右键点击SequenceEJB ---> view bean source
   为getSequence() 添加函数体
   public int getSequence() {
    int i = this.getValue();
    this.setValue( i + 1 );
    return i;
   }
=====================================================================================================================

7,编译ejb-module

8, 在数据源中右键点击Category表 ---〉new CMP 2.0 entity bean

    name : CategoryEJB
    abstract schema name: Category		
    interface:local
    always wrap primary key : false
    --> package and class
    page       : ejbs
    Bean class : ejbs.CategoryBean
    local home : ejbs.CategoryLocalHome
    Local      : ejbs.CategoryLocal

====================================================================================================================

9, 修改CategoryEJB 的属性:
    
    categoryId:
        is primary key : true
        in ejb create  : true
        column         : CATEGROYID
  
    name:
        is primary key : false
        in ejb create  : true
        column         : NAME
    description:
        is primary key : false
        in ejb create  : true
        column         : DESCRIPTION

==================================================================================================================

10,右键点击CategoryEJB ---> add --->  finder

     name        : findAll;
     return type : java.util.Collection
     home interface: local
     Query: select object( c ) from Category c
 
================================================================================================================

编译ejb - module

================================================================================================================

11,创建一个sessionBean
   name          : ProductMgmSessionFacadeEJB
   interface     : local
   session type  : stateless
   transaction type : container

   package: ejbs
   bean class : ejbs.ProductMgmSessionFacadeBean
   local home : ejbs.ProductMgmSessionFacadeLocalHome
   local      : ejbs.ProductMgmSessionFacadeLocal

======================================================================================================

12 右键点击ProductMgmSessionFacadeEJB --> add --> field

   添加两个field :
   1) field name : categoryHome
      type       : CategoryLocalHome
      getter     : none
      setter     : none

   2) field name : seqHome
      type       : SequenceLocalHome
      getter     : none
      setter     : none
缺少productHome

=====================================================================================================

13 右键点击ProductMgmSessionFacadeEJB --> add --> method 
   
   添加5个方法:

   1)  name : getAllCategory
       return type: java.util.Collection
       input parameter:
       interface      :  local
   2)  name : createCategory
       return void
       input parameter: dto.CategoryDTO
       interface      :  local

    3) name : deleteCategory
       return type: void
       input parameter: String id
       interface      :  local

    4) name : getCategoryById
       return type: dto.CategoryDTO
       input parameter: String id
       interface      :  local

    5) name : updateCategory
       return type: void
       input parameter:  dto.CategoryDTO dto
       interface      :  local

===================================================================================================

14, 在ProductMgmSessionFacadeEJB中配置对CategoryEJB,SequenceEJB,ProductEJB的资源引用

==================================================================================================

15 ---> view bean source 

========================================================================================
16,在setSessionContext中初始化Entity Bean 的 local home   (ejb/categoryEJB,ejb/sequenceEJB)

  public void setSessionContext(SessionContext sessionContext) {
    this.sessionContext = sessionContext;
    try{
      ServiceLocator locator = ServiceLocator.getInstance();
      categoryHome = ( CategoryLocalHome )
          locator.getEJBLocalHome( "java:comp/env/ejb/categoryEJB" );
      seqHome = ( SequenceLocalHome)
          locator.getEJBLocalHome( "java:comp/env/ejb/sequenceEJB" );
    }catch( ServiceLocatorException se ){
      se.printStackTrace();
      throw new EJBException(se.getMessage());
    }
  }

======================================================================================================

17 为上述五个新添加的方法添加函数体:

public java.util.Collection getAllCategory() throws FinderException {
    /**@todo Complete this method*/
    Collection col = categoryHome.findAll();
    HashSet set = new HashSet();
    Iterator i = col.iterator();
    while( i.hasNext() ){
      CategoryLocal local = (CategoryLocal)i.next();
      set.add( new CategoryDTO(
          local.getCategoryid(),
          local.getName(),
          local.getDescription()));

    }
    return set;
  }
  public void createCategory(CategoryDTO category) throws FinderException, CreateException {
    /**@todo Complete this method*/
    SequenceLocal local = seqHome.findByPrimaryKey( new Integer(1) );
    int i = local.getSequence();
    category.setId( new Integer(i) );
    categoryHome.create( category.getId(),category.getName(),category.getDesc());
 }
  public void deleteCategory(String id) throws FinderException, RemoveException {
    /**@todo Complete this method*/
    CategoryLocal local = categoryHome.findByPrimaryKey( new Integer(id));
    local.remove();
  }
  public dto.CategoryDTO getCategoryById(String id) throws FinderException {
    /**@todo Complete this method*/
    CategoryLocal local = categoryHome.findByPrimaryKey( new Integer(id));

    return new CategoryDTO( local.getCategoryid(),
                            local.getName(),
                            local.getDescription() );
  }
  public void updateCategory(dto.CategoryDTO dto)
                  throws FinderException{
    /**@todo Complete this method*/
    CategoryLocal local = categoryHome.findByPrimaryKey( dto.getId() );
    local.setName( dto.getName() );
    local.setDescription( dto.getDesc() );
  }

========================================================================================================

18,修改 ProductMgmSessionFacadeEJB 的 ProductMgmSessionFacadeLocal 接口
    使其中的函数声明与ProductMgmSessionFacadeBean 中的保持一致

package ejbs;

import javax.ejb.*;
import java.util.*;
import dto.CategoryDTO;

public interface ProductMgmSessionFacadeLocal extends javax.ejb.EJBLocalObject {
  public java.util.Collection getAllCategory() throws FinderException;
  public void createCategory(CategoryDTO category) throws FinderException, CreateException;
  public void deleteCategory(String id) throws FinderException, RemoveException;
  public dto.CategoryDTO getCategoryById(String id) throws FinderException;
  public void updateCategory(CategoryDTO dto) throws FinderException;
}

========================================================================================================

编译ejb -- module

========================================================================================================

19 , 在 delegates 包中 新建 class :  ProductMgmDelegate


package delegates;
import exceptions.*;
import util.ServiceLocator;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import dto.*;
import java.util.Collection;
import ejbs.ProductMgmSessionFacadeLocal;
import ejbs.ProductMgmSessionFacadeLocalHome;
import javax.ejb.RemoveException;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class ProductMgmDelegate {
  private ProductMgmSessionFacadeLocal sessionLocal = null;
  public ProductMgmDelegate() throws ServiceLocatorException,CreateException{
    ServiceLocator locator = ServiceLocator.getInstance();
    ProductMgmSessionFacadeLocalHome home = (ProductMgmSessionFacadeLocalHome)
        locator.getEJBLocalHome( "java:comp/env/ejb/productSessionFacade" );
    sessionLocal = home.create();
  }

  public Collection getAllCategory()throws FinderException{
    return sessionLocal.getAllCategory();
  }

  public void createCategory( CategoryDTO category )
                    throws FinderException , CreateException{
    sessionLocal.createCategory( category );
  }

  public void deleteCategory( String id )
                   throws FinderException , RemoveException{
    sessionLocal.deleteCategory( id );
  }

  public CategoryDTO getCategoryById( String id )
                    throws FinderException{
    return sessionLocal.getCategoryById( id );
  }

  public void updateCategory( CategoryDTO category )
                        throws FinderException{
    sessionLocal.updateCategory( category );
  }


}


==========================================================================================================

20 通过struts 创建web 应用

==========================================================================================================













⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -