?? 5.mht
字號(hào):
CustomerBean.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D584=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanAttribute=
s.png"=20
width=3D368 align=3Dleft border=3D0 name=3DGraphic8><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Generate EJB classes and examine what =
methods=20
are generated in the various classes, particularly in CustomerBMP and=20
CustomerData</B>.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<H3>Add Finder Methods :</H3>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Now let's add a finder method to our =
bean=20
class</B>. </P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add a method with this =
signature:<BR><BR><FONT=20
color=3D#000000>public CustomerPK</FONT> <FONT=20
color=3D#000000>ejbFindByPrimaryKey</FONT> <FONT =
color=3D#000000>(CustomerPK</FONT>=20
<FONT color=3D#000000>pk)</FONT> throws F<FONT =
color=3D#000000>inderException</FONT>=20
</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in">Put some debug statements in it and =
return null as=20
shown below.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D192=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanEjbFind.p=
ng"=20
width=3D722 align=3Dleft border=3D0 name=3DGraphic15><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Now when we generate our EJB classes =
this=20
method will be overridden in CustomerBMP. Also this method will call a=20
corresponding method from CustomerDAO interface as shown below in this =
code=20
snippet from CustomerBMP.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D342=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBMPjbFind.png=
"=20
width=3D766 align=3Dleft border=3D0 name=3DGraphic39><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>This will also create methods in the =
Home=20
interface and DAO interfaces as shown below.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D270=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerHomeFindBy.pn=
g"=20
width=3D542 align=3Dleft border=3D0 name=3DGraphic41><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D322=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOFindBy.png=
"=20
width=3D854 align=3Dleft border=3D0 name=3DGraphic40><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Add another finder method to =
CustomerBean, with=20
the signature</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B><FONT color=3D#000000>public =
CustomerPK</FONT>=20
<FONT color=3D#000000>ejbFindByUserID</FONT> <FONT =
color=3D#000000>(String=20
userID)</FONT> throws F<FONT color=3D#000000>inderException</FONT> =
</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in">Put some debug statements in it and =
return null as=20
shown below.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D212=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanFindByUse=
rID.png"=20
width=3D590 align=3Dleft border=3D0 name=3DGraphic16><BR =
clear=3Dleft><FONT=20
color=3D#800000><I>Note : As stated in the EJB spec 12.8.1 all finder =
methods=20
should return the Primary Key.</I></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Again, regenerate your EJB classes, =
and there=20
will be methods created in the CustomerHome interface, CustomerBMP and=20
CustomerDAO, similar to those that were created for =
ejbFindByPrimaryKey.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>A code snippet from the CustomerDAO =
class,=20
after generating the EJB classes.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D258=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOFindByUser=
ID.png"=20
width=3D874 align=3Dleft border=3D0 name=3DGraphic17><BR =
clear=3Dleft><BR></P>
<H3>Add Business Methods :</H3>
<P><B>Now, add a business method with the signature<BR><BR><FONT=20
face=3D"Nimbus Roman No9 L"><FONT color=3D#000000>public =
CustomerData</FONT> <FONT=20
color=3D#000000>getCustomerData() with Interface type as=20
local.</FONT></FONT></B></P>
<P><FONT color=3D#800000><I><FONT face=3D"Nimbus Roman No9 L">Note : The =
steps to=20
add a business method are covered in previous chapters (1 and 3), so =
please=20
refer to them. Also, we have chosen the</FONT> interface type to be =
local=20
because these methods will be invoked in the same Java Virtual Machine. =
In this=20
case they will be invoked by the stateless bean =
StoreAccess.</I></FONT></P>
<P><FONT color=3D#000000><FONT face=3D"Nimbus Roman No9 L"><FONT =
size=3D3><B>This will=20
provide the details of an individual customer. Add some debug statements =
and=20
return an instance of CustomerData as shown below in this code snippet =
from=20
CustomerBean. </B></FONT></FONT></FONT></P>
<P style=3D"MARGIN-BOTTOM: 0in"><IMG height=3D226=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanGetCustom=
erData.png"=20
width=3D590 align=3Dleft border=3D0 name=3DGraphic42><BR =
clear=3Dleft><BR></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>Make sure you generate your EJB =
classes again=20
before you start implementing Customer's DAO interface.</B></P>
<H3><BR><BR></H3>
<H3>Implement Customer's DAO Interface :</H3>
<P style=3D"MARGIN-BOTTOM: 0in">Now let's implement our methods in =
CustomerDAOImpl=20
class.</P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>CustomerDOAImp class under package=20
au.com.tusc.dao implements methods generated in CustomerDAO class under =
package=20
au.com.tusc.bmp.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in">First import the following =
packages.</P><PRE><FONT color=3D#000000><FONT face=3DSnas><FONT =
style=3D"FONT-SIZE: 11pt" =
size=3D2>javax.naming.InitialContext;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSnas><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2>javax.sql.DataSource;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSnas><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2>java.sql.Connection;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSnas><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2>java.sql.PreparedStatement;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSnas><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2>java.sql.ResultSet;</FONT></FONT></FONT>
<FONT color=3D#000000><FONT face=3DSnas><FONT style=3D"FONT-SIZE: 11pt" =
size=3D2>java.sql.SQLException;</FONT></FONT></FONT></PRE>
<P><B>Change your class declaration so that CustomerDAOImpl implements=20
CustomerDAO.</B></P>
<P><B>Add a field to store the JDBC resource factory =
reference.</B></P><PRE style=3D"MARGIN-BOTTOM: 0.2in"><FONT =
face=3DSans><FONT style=3D"FONT-SIZE: 11pt" size=3D2><B><FONT =
color=3D#7f0055>private</FONT></B> DataSource =
jdbcFactory;</FONT></FONT></PRE>
<P><B>In the init() method, locate the reference jdbc/DefaultDS using =
the JNDI=20
API, and store the reference in variable jdbcFactory.</B></P>
<P style=3D"MARGIN-BOTTOM: 0in"><B>The lookup string is </B><FONT =
face=3DSans><FONT=20
color=3D#2a00ff>"java:comp/env/jdbc/DefaultDS".</FONT></FONT></P>
<P>Code snippet is shown below.</P>
<P><IMG height=3D572=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOImplInit.p=
ng"=20
width=3D518 align=3Dleft border=3D0 name=3DGraphic20><BR =
clear=3Dleft><BR><BR></P>
<P><B>In method load(), first get the connection to the database using =
field=20
jdbcFactory. Create a SQL statement which searches for a record =
corresponding to=20
customerid in table Customer, where customerid is the primary key. =
</B>Code=20
snippet is shown below.</P>
<P><IMG height=3D588=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOImplLoad.p=
ng"=20
width=3D594 align=3Dleft border=3D0 name=3DGraphic38><BR =
clear=3Dleft><BR><BR></P>
<P><B>In method store(), first get the connection to database using =
field=20
jdbcFactory. Create an SQL statement which updates a record =
corresponding to=20
customerid in table Customer, where customerid is the primary key. =
</B>Code=20
snippet is shown below.</P>
<P><IMG height=3D548=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOImplStore.=
png"=20
width=3D600 align=3Dleft border=3D0 name=3DGraphic36><BR =
clear=3Dleft><BR><BR></P>
<P><B>In method ejbFindByUserID(), first get the connection to database =
using=20
field jdbcFactory. Create an SQL statement which searches for the =
customerid=20
corresponding to a given userid in table Customer, where customerid is =
primary=20
key. </B>Code snippet is shown below<B>.</B></P>
<P><IMG height=3D582=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOImplFindBy=
UserID.png"=20
width=3D696 align=3Dleft border=3D0 name=3DGraphic18><BR =
clear=3Dleft><BR><BR></P>
<P><B>In method ejbfindByPrimaryKeystore(), first get the connection to =
database=20
using field jdbcFactory. Create an SQL statement which searches for =
customerid=20
in table Customer, where customerid is the primary key. Return the =
primary=20
key</B>. Code snippet is shown below.</P>
<P><IMG height=3D560=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerDAOImplFindBy=
.png"=20
width=3D602 align=3Dleft border=3D0 name=3DGraphic46><BR =
clear=3Dleft><B>Also, you should=20
implement methods remove and create. We're leaving those as an exercise =
for you=20
(you've probably got the idea by now!), or you can leave them as stubs =
as shown=20
below.</B></P>
<P><IMG height=3D116=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanRemove.pn=
g"=20
width=3D854 align=3Dleft border=3D0 name=3DGraphic47><BR =
clear=3Dleft><BR><BR></P>
<P><B>Now our CustomerDAOImpl class is finished. Generate your EJB =
classes=20
again.</B></P>
<P><B>After generating your EJB classes, let's look at the Home Local =
Interface=20
and the Remote Local interface. </B></P>
<P><B>In the Remote Local Interface (which is CustomerLocal in this =
case), there=20
is one business method exposed, and the rest are all the methods to =
access the=20
attributes/properties of the bean (as we also declared these methods as=20
interface methods in CustomerBean), as shown below. These methods are =
generated=20
by @ejb.interface-method tag.</B></P>
<P><IMG height=3D338=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerLocal.png" =
width=3D294=20
align=3Dleft border=3D0 name=3DGraphic49><BR clear=3Dleft><BR><BR></P>
<P><B>In the Home Local Interface, which is CustomerLocalHome in this =
case,=20
there are two finder methods as shown below. It has also generated =
JNDI_NAME and=20
COMP_NAME (logical name to lookup the component).</B></P>
<P><IMG height=3D346=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerLocalHome.png=
"=20
width=3D576 align=3Dleft border=3D0 name=3DGraphic19><BR =
clear=3Dleft><B>These names are=20
generated by the following tag declared in CustomerBean as shown =
below.</B></P>
<P><IMG height=3D170=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanNameTag.p=
ng"=20
width=3D266 align=3Dleft border=3D0 name=3DGraphic51><BR =
clear=3Dleft><BR><BR></P>
<P><FONT color=3D#800000><I>Note : We are not currently interested in =
the Customer=20
interface (which is a Remote interface) and CustomerHome (which is a =
Remote Home=20
interface), because we are accessing bean methods in the same Java =
Virtual=20
Machine, so we need only Local interfaces. </I></FONT></P>
<P><FONT color=3D#800000><I>Note : In CustomerBean we haven't =
implemented any=20
methods for the setting and unsetting of context, because it is =
generated by=20
Xdoclet in the CustomerBMP class, as shown below.</I></FONT></P>
<P><IMG height=3D186=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBmpEntityCont=
ext.png"=20
width=3D352 align=3Dleft border=3D0 name=3DGraphic48><BR =
clear=3Dleft><B>Now Customer Bean=20
and its DAO implementation is complete and we can deploy this =
bean.</B></P>
<H3>Deploy Customer Bean :</H3>
<P><B>In order to deploy this bean we have to add a few deployment =
descriptors=20
to it. We will add the two tags shown below.</B></P>
<P><IMG height=3D372=20
src=3D"http://www.tusc.com.au/tutorial/images/chap5/CustomerBeanDeploymen=
tTags.png"=20
width=3D404 align=3Dleft border=3D0 name=3DGraphic21><BR =
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -