一、系统设置:
1.编辑工具:UltraEdit
2.应用服务器:Weblogic 8.1
3.数据库:MySQL4.0
二、基本类的编写:(例子源于MasteringEJB中的第6章的实体Bean实例)
1.Home接口: AccountHome.java
2.本地Home接口: AccountLocalHome.java
3.Remote接口: Account.java
4.Remote Home接口: AccountLocal.java
5.实现类: AccountBean.java
6.主健类: AccountPK.java
7.用户测试类: AccountClient.java
三、数据库设置
1. 数据库用MySQL中的test数据库;
2. 数据表为:
drop table accounts;
create table accounts (id varchar(64),ownername varchar(64),balance numeric(18));
3. 插入一条测试记录
insert into accounts(id,ownername,balance) values(“0000”,”Dosany”,1000000);
四、在WebLogic中设置数据连接池和数据源
1. 下载MySQL的JDBC驱动,并把它的路径加入到系统classpath中;
2. 启动WebLogic中的用户域,这个实例为 mydomain;
3. 打开WebLogic管理界面: http://localhost:7001/console,输入用户名和密码进入;
4. 进入Services-JDBC-ConnectionPools
5. 点击Configure a new JDBC Connection Pool... ,
6. Database Type:MySQL,DatabaseDriver:MySQL’s Driver(Type 4) Version:Any;
7. 点击 Continue;
8. Name:MySQLDatePool;DatabaseName:test;Host Name:127.0.0.1;
Port:3306;Database User Name:hhdsf;Password:baobao 下同;continue;
9. Driver Classname: org.gjt.mm.mysql.Driver
URL: jdbc:mysql://127.0.0.1:3306/test
10. Test Driver Configuration,可以看到成功;
注意:MySQL中的用户hhdsf是安装MySQL后加入的,因为MySQL安装后的默认用户是 root;密码是空,而在WebLogic中密码不能为空;
设置数据源:
1. 进入Services-JDBC-DataSources
2. 选择Configure a new JDBC Data Source
3. Name:MySQLDataSource;JNDI:mySqlDs;点击Next;
4. 在Connect to connection pool 中选择刚刚建立的connection pool;点击Next;
5. 点击Create,创建DataSource。
五、部署文件
1. Ejb-jar部署文件:
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>Account</ejb-name>
<home>AccountHome</home>
<remote>Account</remote>
<local-home>AccountLocalHome</local-home>
<local>AccountLocal</local>
<ejb-class>AccountBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>AccountPK</prim-key-class>
<reentrant>False</reentrant>
<resource-ref>
<res-ref-name>jdbc/ MySQLDatePool </res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Account</ejb-name>
<method-intf>Local</method-intf>
<method-name>*</method-name>
</method>
<method>
<ejb-name>Account</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
2. weblogic-ejb-jar部署文件:
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN' 'http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>Account</ejb-name>
<entity-descriptor>
<entity-cache>
<max-beans-in-cache>100</max-beans-in-cache>
</entity-cache>
</entity-descriptor>
<reference-descriptor>
<resource-description>
<res-ref-name>jdbc/ MySQLDatePool </res-ref-name>
<jndi-name> mySqlDs </jndi-name>
</resource-description>
</reference-descriptor>
<jndi-name>AccountHome</jndi-name>
<local-jndi-name>AccountLocalHome</local-jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
六、编译和运行
1. 编译命令:
@echo *******************************************************
@echo Cleaning things up...
@echo *******************************************************
md classes
md lib
del classes/*.class
del lib/*.jar
@echo *******************************************************
@echo Compiling files...
@echo *******************************************************
javac -d classes src/*.java
@echo *******************************************************
@echo Copying Deployment Descriptor...
@echo *******************************************************
md classes/META-INF
copy deployment/*.xml classes/META-INF
@echo *******************************************************
@echo Creating temporary ejb-jar file...
@echo *******************************************************
cd classes
jar cf ../lib/tmp.jar *
cd ..
@echo *******************************************************
@echo Generating helper code, and creating new ejb-jar file...
@echo *******************************************************
pushd lib
java weblogic.ejbc tmp.jar -d EJB.jar
copy EJB.jar %DEPLOY_HOME%/Account.jar
popd
@echo *******************************************************
@echo Removing temporary ejb-jar file
@echo *******************************************************
del lib/tmp.jar
2. 运行测试客户端程序:
copy src/AccountClient.java classes
cd classes
javac AccountClient.java
del AccountClient.java
java -Djava.naming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.naming.provider.url=t3://localhost:7001 AccountClient
cd ..
1.编辑工具:UltraEdit
2.应用服务器:Weblogic 8.1
3.数据库:MySQL4.0
二、基本类的编写:(例子源于MasteringEJB中的第6章的实体Bean实例)
1.Home接口: AccountHome.java
2.本地Home接口: AccountLocalHome.java
3.Remote接口: Account.java
4.Remote Home接口: AccountLocal.java
5.实现类: AccountBean.java
6.主健类: AccountPK.java
7.用户测试类: AccountClient.java
三、数据库设置
1. 数据库用MySQL中的test数据库;
2. 数据表为:
drop table accounts;
create table accounts (id varchar(64),ownername varchar(64),balance numeric(18));
3. 插入一条测试记录
insert into accounts(id,ownername,balance) values(“0000”,”Dosany”,1000000);
四、在WebLogic中设置数据连接池和数据源
1. 下载MySQL的JDBC驱动,并把它的路径加入到系统classpath中;
2. 启动WebLogic中的用户域,这个实例为 mydomain;
3. 打开WebLogic管理界面: http://localhost:7001/console,输入用户名和密码进入;
4. 进入Services-JDBC-ConnectionPools
5. 点击Configure a new JDBC Connection Pool... ,
6. Database Type:MySQL,DatabaseDriver:MySQL’s Driver(Type 4) Version:Any;
7. 点击 Continue;
8. Name:MySQLDatePool;DatabaseName:test;Host Name:127.0.0.1;
Port:3306;Database User Name:hhdsf;Password:baobao 下同;continue;
9. Driver Classname: org.gjt.mm.mysql.Driver
URL: jdbc:mysql://127.0.0.1:3306/test
10. Test Driver Configuration,可以看到成功;
注意:MySQL中的用户hhdsf是安装MySQL后加入的,因为MySQL安装后的默认用户是 root;密码是空,而在WebLogic中密码不能为空;
设置数据源:
1. 进入Services-JDBC-DataSources
2. 选择Configure a new JDBC Data Source
3. Name:MySQLDataSource;JNDI:mySqlDs;点击Next;
4. 在Connect to connection pool 中选择刚刚建立的connection pool;点击Next;
5. 点击Create,创建DataSource。
五、部署文件
1. Ejb-jar部署文件:
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN' 'http://java.sun.com/dtd/ejb-jar_2_0.dtd'>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>Account</ejb-name>
<home>AccountHome</home>
<remote>Account</remote>
<local-home>AccountLocalHome</local-home>
<local>AccountLocal</local>
<ejb-class>AccountBean</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>AccountPK</prim-key-class>
<reentrant>False</reentrant>
<resource-ref>
<res-ref-name>jdbc/ MySQLDatePool </res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Account</ejb-name>
<method-intf>Local</method-intf>
<method-name>*</method-name>
</method>
<method>
<ejb-name>Account</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
2. weblogic-ejb-jar部署文件:
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 6.0.0 EJB//EN' 'http://www.bea.com/servers/wls600/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>Account</ejb-name>
<entity-descriptor>
<entity-cache>
<max-beans-in-cache>100</max-beans-in-cache>
</entity-cache>
</entity-descriptor>
<reference-descriptor>
<resource-description>
<res-ref-name>jdbc/ MySQLDatePool </res-ref-name>
<jndi-name> mySqlDs </jndi-name>
</resource-description>
</reference-descriptor>
<jndi-name>AccountHome</jndi-name>
<local-jndi-name>AccountLocalHome</local-jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
六、编译和运行
1. 编译命令:
@echo *******************************************************
@echo Cleaning things up...
@echo *******************************************************
md classes
md lib
del classes/*.class
del lib/*.jar
@echo *******************************************************
@echo Compiling files...
@echo *******************************************************
javac -d classes src/*.java
@echo *******************************************************
@echo Copying Deployment Descriptor...
@echo *******************************************************
md classes/META-INF
copy deployment/*.xml classes/META-INF
@echo *******************************************************
@echo Creating temporary ejb-jar file...
@echo *******************************************************
cd classes
jar cf ../lib/tmp.jar *
cd ..
@echo *******************************************************
@echo Generating helper code, and creating new ejb-jar file...
@echo *******************************************************
pushd lib
java weblogic.ejbc tmp.jar -d EJB.jar
copy EJB.jar %DEPLOY_HOME%/Account.jar
popd
@echo *******************************************************
@echo Removing temporary ejb-jar file
@echo *******************************************************
del lib/tmp.jar
2. 运行测试客户端程序:
copy src/AccountClient.java classes
cd classes
javac AccountClient.java
del AccountClient.java
java -Djava.naming.factory.initial=weblogic.jndi.WLInitialContextFactory -Djava.naming.provider.url=t3://localhost:7001 AccountClient
cd ..