1. 开发环境
IDE开发工具:Eclipse Java EE IDE for Web Developers
EJB容器:jboss-4.2.3.GA
后台数据库:MysQL5.1
2. 开发步骤
2.1 在Eclipse中配置jboss-4.2.3.GA
1. 点击小三角,选中New server
2. 选中JBOSS AS4.2
3. 点击ADD,进行JBOSS配置
4. 在Home Directory中,点击Browser选择JBOSS4.2的安装目录,在JRE中配置JRE的安装目录,点击finish
在finish,即可完成对JBOSS在Eclipse中的配置。
2.2 新建EJB3.0工程titan
1. 右键->New->Project
2. 选择EJB Project->next
3. Project name为“titan”,Targetruntime选择配置好的JBOSS4.2,EJB module version选择3.0,然后finish。新的EJB3.0工程创建成功。
2.3 项目开发
2.3.1 开发Cabin实体Bean
package com.titan.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity//告知persistence provider这是一个映射到数据的实体类,并且可以受管于EntityManager服务。
@Table(name="CABIN")//告知EJB容器,beanclass 应该被映射到哪一张数据库表。
public classCabin implementsjava.io.Serializable{
/**
*
*/
private staticfinal longserialVersionUID = 1L;
/**
*
*/
private intid;
private String name;
private intdeckLevel;
private intshipId;
private intbedCount;
@Id//表示CAbin实体的主键,主键标志是必须的
@Column(name="ID")//定义如何将成员属性映射到数据库表CABIN中的字段。并非是必须的
public intgetId() {
return id;
}
public voidsetId(int id) {
this.id =id;
}
@Column(name="NAME")
public String getName() {
return name;
}
public voidsetName(String name) {
this.name =name;
}
@Column(name="DECK_LEVEL")
public intgetDeckLevel() {
return deckLevel;
}
public voidsetDeckLevel(int deckLevel) {
this.deckLevel= deckLevel;
}
@Column(name="SHIP_ID")
public intgetShipId() {
returnshipId;
}
public voidsetShipId(int shipId) {
this.shipId= shipId;
}
@Column(name="BED_COUNT")
public intgetBedCount() {
return bedCount;
}
public voidsetBedCount(int bedCount) {
this.bedCount= bedCount;
}
}
2.3.2开发远程接口TravelAgentRemote
package com.titan.travelagent;
import javax.ejb.Remote;
import com.titan.domain.Cabin;
@Remote//表示这是一个远程接口
public interfaceTravelAgentRemote {
public voidcreateCabin(Cabin cabin);
public Cabin findCabin(int id);
}
2.3.3开发远程接口的实现Bean类TravelAgentBean(StatelessBean)
package com.titan.travelagent;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.titan.domain.Cabin;
@Stateless//表示这是一个无状态sessionbean
public classTravelAgentBean implementsTravelAgentRemote {
//@PersistenceContext告知EJB容器,必须用一个EntityManager实例来设置manager数据成员。
@PersistenceContext(unitName="titan") privateEntityManager manager;
public voidcreateCabin(Cabin cabin) {
// TODO Auto-generated method stub
manager.persist(cabin);
}
public Cabin findCabin(int id) {
// TODO Auto-generated method stub
return manager.find(Cabin.class, id);
}
}
2.3.4在META-INF下创建文件persistence.xml文件
配置如下:
<?xml version="1.0"encoding="UTF-8" ?>
<persistence version="1.0"xmlns="http://java.sun.com/xml/ns/persistence"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/persistencehttp://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="titan">
<jta-data-source>java:/titan</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto"value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
2.3.5在com.titan.clients包下开发客户端程序
packagecom.titan.clients;
importjavax.naming.InitialContext;
importjavax.naming.Context;
importjavax.naming.NamingException;
importjavax.rmi.PortableRemoteObject;
importcom.titan.domain.Cabin;
importcom.titan.travelagent.TravelAgentRemote;
importjava.util.Properties;
importjavax.rmi.PortableRemoteObject;
public classClient {
public static void main(String[]args){
try {
Context jndiContext=getInitialContext();
Objectref=jndiContext.lookup("TravelAgentBean/remote");
TravelAgentRemotedao=(TravelAgentRemote)
PortableRemoteObject.narrow(ref, TravelAgentRemote.class);
Cabin cabin_1=new Cabin();
cabin_1.setId(1);
cabin_1.setName("MasterSuite");
cabin_1.setDeckLevel(1);
cabin_1.setShipId(1);
cabin_1.setBedCount(3);
dao.createCabin(cabin_1);
Cabin cabin_2=dao.findCabin(1);
System.out.println(cabin_2.getId());
System.out.println(cabin_2.getName());
System.out.println(cabin_2.getShipId());
System.out.println(cabin_2.getDeckLevel());
System.out.println(cabin_2.getBedCount());
} catch (ClassCastException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static Context getInitialContext()throws NamingException {
// TODO Auto-generated method stub
Properties p=new Properties();
//...制定专有EJB厂商的JNDI属性
p.put(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
p.put(Context.PROVIDER_URL, "://localhost:1099");
return newjavax.naming.InitialContext(p);
}
}
至此项目的客户端开发完成。
2.4 配置MySql的datasource
1. 将mysql-connector-java-5.1.16.jar包放置到JBOSS_HOME\server\default\lib目录下.
2. 将配置好的mysql-ds.xml放到JBOSS_HOME\server\default\deploy目录下。配置如下:
<?xmlversion="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>titan</jndi-name>
<connection-url>jdbc:mysql://192.168.9.85:3306/titan</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>root</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
2.4.1创建数据库
1、创建数据库titan
2、创建数据表cabin
DROP TABLE IFEXISTS `cabin`;
CREATE TABLEcabin (
ID int(11) NOT NULL,
NAME varchar(255) DEFAULT NULL,
DECK_LEVEL int(11) DEFAULT NULL,
SHIP_ID int(11) DEFAULT NULL,
BED_COUNT int(11) DEFAULT NULL,
PRIMARY KEY (`ID`)
)
3. 运行
1. 将titan工程发布到JBOSS,启动JBOSS
2. 打开client程序,右键->Run As->Java Application
3. 在数据库里可以查询到相应的数据记录,在控制台中可以看到相应的查询结果。