The last article has developed a STATELESS SESSION bean, which introduces the development of CMP.
The establishment of the web test module is no longer repeated. Ant scripts do not need to be modified. On the basis of the previous example, we can directly Code CMP.
step:
1 Built a database named mydb in PointBase, build a User table, there are ID, name, and agnus in the table.
2 Create a CMP called Userbean, and there is a three fields in the bean with UserID, Name, AGE.
3 Edit the bean and modify the deployment descriptor
4 compile, deploy
5 Write JSP for testing
Now, Let's Go!
1 In PointBase
In this example we use WebLogic's own PointBase database, if you want to know information about the configuration of PointBase, please refer to my other post - "PointBase Getting Started"
Position to the PointBase folder. It is usually located in:
Enter the Tools directory, start the StartPointBase.cmd script. Then you will see a CMD window that pops up. When you display the database port information, you will get started (very fast, less than 1 second)
Then start the StartPointBaseConsole.cmd script, a dialog box will pop up.
JDBC Driver: No modification
URL: Enter JDBC: PointBase: Server: // localhost / mydb
User: Enter PBPUBLIC
Password: Enter PBPUBLIC
There are three options below, select "CREATE New Database" in the middle, then click OK.
OK, the MYDB database creates success.
Next we create a table. You can use the script to get the Wizard using the script.
Create Table User
ID Integer Not Null,
Name varchar (30),
Age INTEGER,
ConsTRAINT PK_ID PK_ID PRIMARY Key (ID)
);
Then insert three data:
INSERT INTO User (ID, Name, Age) Values (1, 'Diegoyun', 25);
INSERT INTO User (ID, Name, Age) Values (2, 'Houqi', 25);
INSERT INTO User (ID, Name, Age) Values (3, 'Zhihua ", 26);
At this point, the database preparation is completed.
2 Create EJB
New Entity packages under com.diegoyun.ejb.
In Idea, create a new CMP, pop-up EJB's properties box, this and sessionbean, the property settings are as follows:
Package: com.diegoyun.ejb.entity
Primary Key Class: Change to Java.lang.integer
For the sake of simplicity, our CMP here uses the Remote interface, so other information do not have to be modified, point OK
Then the CMP property setting box will pop up, and make the following settings:
Display name: Userbean
Add CMP Field:
Click on this box, add three fields, and make the following settings: UserId Integer PK
Name string
Age Integer
Then switch to the WebLogic Server property page, set the JNDI name: EJB / ENTITY / Userbean
Many properties will also be seen in this property page, this example is not set first.
Close the property page.
In this way, CMP and most of the configuration information including the Remote interface have been created.
You can see the three files of User, Userbean, and UserHome in com.diegoyun.entity, and the contents of the three configuration files in Meta-INF have changed.
3 Edit the bean and modify the deployment descriptor
Here we add a few ways to the Home interface.
Open the UserHome file, add the following method:
Public Collection Findbyage (Integer Age) Throws RemoteException, FINDEREXCEPTION;
Public User Create (Integer Userid, String Name, Integer Age) Throws RemoteException, CreateException;
Then open the Userbean file, add:
Public Integer Ejbcreate (Integer Userid, String Name, Integer Age) throws createException {
SetUserID (userID);
SetName (Name);
Setage (agn);
Return UserId;
}
Public void ejbpostcreate (Integer Userid, String Name, Integer Age) {
}
And add a private field:
Private EntityContext context;
Modify the settentContext and unsetentityContext methods are as follows:
Public void setentityContext (EntityContext EntityContext) THROWS EJBEXCEPTION {
THIS.CONTEXT = EntityContext;
}
Public void unsentityContext () throws ejbexception {
THIS.CONTEXT = NULL;
}
OK, source code Code finish.
Next, modify the deployment descriptor.
Open EJB-JAR.XML
Just now, we define a Finder method in UserHome, you must define it:
There is another line under
Method-Params>
query-method>
ejb-ql>
query>
Then open WebLogic-Cmp-RDBMS-JAR.XML, modified as follows:
XML Version = "1.0"?>
'- // bea systems, Inc.//dtd WebLogic 7.0.0 EJB RDBMS PERSISTENCE // En' 'http://www.bea.com/servers/wls700/dtd/weblogic-rdbms20-persistence-700.dtd' >
field-map>
field-map>
field-map>
TABLE-MAP>
WebLogic-Rdbms-Bean>
WebLogic-Rdbms-Jar>
Next, open WebLogic-ejb-jar.xml, modified as follows:
XML Version = "1.0"?>
persistence-us>
persistence>
Entity-Descriptor>
WebLogic-Enterprise-Bean>
WebLogic-EJB-JAR>
OK, CMP development work has been completed.
4 compile, deploy
Use the original Build script.
Restart the server and log in to the management interface.
Create a ConNetion Pool:
Database Type: Select PointBase
Database driver: Select PointBase's Driver (Type 4) Version: 4.x
Then point Continue
In this interface, enter:
Name: EJBSAMPLESPOOL
Database Name: Enter myDB
Database User Name: Enter PBPUBLIC
Password: Enter PBPUBLIC
Then point Continue. The test connection can then be tested, if there is no accident, Connectin Pool creates complete.
Create Data Source:
Name and JNDI Name Enter: EJBSAMPLES-DS
Pool Select EJBSamplesPool
Deploy EJB:
About Deploy, I said before, if you use UPLOAD DEPLOY, you don't need to re-deploy EJB. But because this CMP is new, you still re-deploy, you can find a potential error when WebLogic is in Deploy.
Delete the original EJB, re-deploy EJB, it should be in:
OK, the deployment work is completed.
5 Write JSP for testing
New four files under Web are as follows:
Index.html (navigation role)
hEAD>
center>
body>
html>
CreateUser.jsp
<% @ page language = "java"%>
<% @ page import = "java.util.arraylist"%>
<% @ Page Import = "java.util.collection"%>
<% @ page import = "javax.ejb.objectNotFoundException"%>
<% @ Page Import = "javax.naming.context"%>
<% @ page import = "javax.naming.initialcontext"%>
<% @ Page Import = "javax.naming.namingexception"%>
<% @ Page Import = "com.diegoyun.ejb.entity.user"%>
<% @ page import = "com.diegoyun.ejb.entity.userhome"%>
<% @ Page Import = "javax.rmi.portableremoteObject"%>
<% @ Page Import = 'Java.util. *'%>
CREATE User: