EJB的资料辅导(3)


WebService

WebService是一个SOA(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。

WebService实现不同语言间的调用,是依托于一个标准,webservice是需要遵守WSDL(web服务定义语言)/SOAP(简单请求协议)规范的。

WebService=WSDL+SOAP+UDDI(webservice的注册)

Soap是由Soap的part和0个或多个附件组成,一般只有part,在part中有Envelope和Body

EJB中使用WebService

@WebService(serviceName="",portName=""),使用这个标注可以将SessionBean中用@WebMethod标注来表示的方法发布成WebService

@Stateless
@WebService(serviceName="Greeter",portName="GreeterPost")
public class HelloSessionBean implements HelloSessionRemote {
    @WebMethod
    public String hello(String name) {
        return "Hello world "+name+"!";
    }
}

soap(Service的请求者)

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://ejb/">
 <soapenv:Body>
  <ns1:hello>
  <arg0>li</arg0>
  </ns1:hello>
 </soapenv:Body>
</soapenv:Envelope>

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://ejb/">
 <soapenv:Body>
  <ns1:helloResponse>
   <return>Hello world li!</return>
  </ns1:helloResponse>
 </soapenv:Body>
</soapenv:Envelope>

 

UDDI (注册Service)
WSDL (Service的提供者)

<?xml version="1.0" encoding="UTF-8" ?>
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ejb/"  xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" targetNamespace="http://ejb/"  name="Greeter">
  <types>
   <xsd:schema>
    <xsd:import namespace="http://ejb/" schemaLocation="http://tarena-    59db236e:8088/Greeter/HelloSessionBean/__container$publishing$subctx/META-   INF/wsdl/Greeter_schema1.xsd" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"    xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" />
   </xsd:schema>
  </types>
  <message name="hello">
   <part name="parameters" element="tns:hello" />
  </message>
  <message name="helloResponse">
   <part name="parameters" element="tns:helloResponse" />
  </message>
  <portType name="HelloSessionBean">
   <operation name="hello">
    <input message="tns:hello" />
    <output message="tns:helloResponse" />
   </operation>
  </portType>
  <binding name="GreeterPostBinding" type="tns:HelloSessionBean">
   <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
   <operation name="hello">
    <soap:operation soapAction="" />
    <input>
     <soap:body use="literal" />
    </input>
    <output>
     <soap:body use="literal" />
    </output>
   </operation>
  </binding>
  <service name="Greeter">
   <port name="GreeterPost" binding="tns:GreeterPostBinding">
    <soap:address location="http://tarena-        59db236e:8088/Greeter/HelloSessionBean"        xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"        xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" />
   </port>
  </service>
</definitions>

实体编程

EJB3.0的JPA(Java持久化API)

O/R Mapping(对象关系映射)
TopLink,JDO,Hibernate


类型对应表,属性对应字段,关系对应引用
BO(商业对象,操作数据对象)
DO(数据对象)
持久化的数据对象,也就是已将对象信息同步到数据库中的对象,持久化对象也叫实体。
操作实体也就使操作实体在数据库中所对应的数据。
实体和SessionBean的区别
实体本身不支持远程访问,他的生命周期是比较长的。
实体有唯一性标识,也就对应数据库表中的主键。
注意:在实体中不要写商业方法

实体的唯一标识,可以使用标签@Id(标识属性可以使用public描述,也可以完全封装为其提供set,get方法),也可以使用XML文件来进行配置。

@Entity(name="Account"),实体类标注,其属性name是指定实体名,在EJB-QL中使用,默认是类的全名
@Id,指定实体的唯一标识属性,默认这个属性会合数据库中对应表的主键对应。
@GeneratedValue(strategy = GenerationType.AUTO)指定主键的生成策略。
@Colum(name="...",unique="true|false",nullable="true|false",insertable="true|false",
updateable="true|false",table="..."),
指定类中属性对应的列名以及约束,
name属性指定类中属性对应的列名,默认为属性名
unique属性指定类中属性对应的列是否唯一,默认为false
nullable属性指定类中属性对应的列是否可空,默认为true
insertable="true|false"属性指定类中该属性是否会出现在insert语句中,也就是会不会被同步到数据库,默认为true,也就数会同步到数据库
updateable="true|false"属性指定类中该属性是否会出现在update语句中,也就是会不会被修改,默认为true可以被修改。
table属性指定类中属性的列所对应的表,默认为实体类所对应的表。

在使用实体同步到数据库时,SessionBean中要写EntityManager类型的属性,这个属性在Bean部署在容器中后,在运行时会容器依赖注入,如果没有容器也可以使用,但需要为其赋值。
EntityManager是一个接口,也就是规则,可以有不同的实现,Hibernate3.2就实现了这些JPA的接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值