EJB笔记

一、EJB基础

1.概念

Enterprice JavaBean是一个用于分布式业务应用的标准服务端组件模型。采用ejb架构的应用是可伸的、事务性、多用户安全的。一般部署在jboss服务器上。

2.ejb类型

会话bean(Session Bean):

会话bean用于实现业务逻辑,它分为有状态bean和无状态bean。会话bean可以直接访问数据库,但更多时候,它是通过实体bean实现数据库访问。

实体bean(Entity Bean):

实体bean主要作为普通java对象使用,负责跟数据库表进行对象与关系映射。

消息驱动bean(MessageDrive Bean):

MDB是设计用来专门处理基于消息请求的组件。它能够收发异步JMS消息,并更够与其他JEB交互。它适用于当一个业务执行的时间很长,而执行的结果无需实时向用户反馈的场景。

二、代码实现

1.服务端

  • 远程无状态会话bean接口
package com.ejbtest.service.bean;

import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import com.ejbtest.service.vo.CtocOrder;

//mappedName 是bean映射名称,客户端调用
@Stateless(mappedName = "ejbTest_jndi")
@Remote({ IEjbService.class })
@TransactionManagement(TransactionManagementType.BEAN)
public class EjbServiceBean implements IEjbService{
        //注解获得实体管理对象
      @javax.persistence.PersistenceUnit()
	private EntityManagerFactory emf;

	public EntityManager getEntityManager() {
		return emf.createEntityManager();
	}
       //指定数据配置单元获得实体管理对象
     public EntityManager getEntityManager01() {
       return Persistence.createEntityManagerFactory("ejbTest-ejbPU").createEntityManager();
        }
	@Override
	public CtocOrder getOrderInfo(String escOrderid) {
		System.out.println("escOrderid="+escOrderid);
		EntityManager em=	getEntityManager();
		String sql="SELECT t FROM CtocOrder t  where escOrderid=:escOrderid ";
		Query query=em.createQuery(sql);
		query.setParameter("escOrderid", escOrderid);
		List<CtocOrder> list=query.getResultList();
		if(list!=null&&list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
}

  • 实体bean

package com.ejbtest.service.vo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
 
@Entity
@Table(name = "CTOC_ORDER",schema = "POSTMALL_USER")
public class CtocOrder implements java.io.Serializable {

	private static final long serialVersionUID = 8527656418454776528L;
	private String escOrderid;
	private String buyerName;
	  
	public CtocOrder() {
	}

	@Column(name = "ESC_ORDERID", length = 20)
	public String getEscOrderid() {
		return this.escOrderid;
	}

	public void setEscOrderid(String escOrderid) {
		this.escOrderid = escOrderid;
	}

	@Column(name = "BUYER_NAME", length = 128)
	public String getBuyerName() {
		return this.buyerName;
	}

	public void setBuyerName(String buyerName) {
		this.buyerName = buyerName;
	}
	
}

  • 数据源配置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/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="ejbTest-ejbPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <!-- jboss 数据源配置jndi-name -->
    <non-jta-data-source>java:ejbTest_db</non-jta-data-source>
    <!-- 数据库映射实体bean -->
    <class>com.ejbtest.service.vo.CtocOrder</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="hibernate.show_sql" value="false" />
      <property name="min-pool-size" value="5" />
      <property name="max-pool-size" value="20" />
      <property name="idle-timeout-minutes" value="5" />
      <property name="hibernate.connection.autocommit" value="false" />
      <property name="hibernate.connection.SetBigStringTryClob" value="true"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
      <property name="hibernate.jdbc.batch_size" value="3"/> 
      <property name="hibernate.max_fetch_depth" value="5"/>
    </properties>
  </persistence-unit>

</persistence>


 注:persistence.xml位于src/META-INF下

  • pom配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                      http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!-- The Basics -->
    <groupId>com.ejbtest</groupId>
    <artifactId>ejbTestServcie</artifactId>
    <version>1.0</version>
    <packaging>ejb</packaging>

    <dependencies>

       <dependency>
			<groupId>javax.ejb</groupId>
			<artifactId>ejb-api</artifactId>
			<version>3.0</version>
		</dependency>
		<dependency>
			<groupId>javax.persistence</groupId>
			<artifactId>persistence-api</artifactId>
			<version>1.0</version>
		</dependency>        
 
    </dependencies>
    
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                    <debug>true</debug>
                    <fork>true</fork>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.8</version>
                <configuration>
                    <wtpversion>2.0</wtpversion>
                    <wtpContextName>/</wtpContextName>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-ejb-plugin</artifactId>
                <configuration>
                    <ejbVersion>3.0</ejbVersion>
                    <generateClient>true</generateClient>
                    <jarName>ejbtest-1.0</jarName>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3</version>
                <executions>
                    <execution>
                        <id>ejbtest-util</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classifier>util</classifier>
                            <jarName>ejbtest-1.0</jarName>
                        </configuration>
                    </execution>
                </executions>
            </plugin>           
        </plugins>
    </build>

    <distributionManagement>
        <repository>
            <id>tom-maven-repository</id>
            <name>Tom Maven Repository</name>
            <url>xxxx</url>
        </repository>
    </distributionManagement>

    <repositories>
        <repository>
            <releases>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </releases>
            <id>internal</id>
            <name>Tom Central Maven Repository</name>
            <layout>default</layout>
            <url>xxx</url>
            <snapshots>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>warn</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>

</project>

2.客户端

package com.ejbtest.client;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.ejbtest.service.bean.IEjbService;
import com.ejbtest.service.vo.CtocOrder;

public class Test {
    public static void main(String[] args) throws NamingException {
        Properties props = new Properties();
        props.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
        props.put("java.naming.provider.url", "127.0.0.1:1004");//ejb服务地址
        props.put("java.naming.factory.url.pkgs", "org.jboss.naming");
        Context ctx = new InitialContext(props);
        IEjbService service = (IEjbService) ctx.lookup("ejbTest_jndi");//ejb bean接口名称
        CtocOrder order=service.getOrderInfo("2017052400494689");
        System.out.println(order.getEscOrderid()+">>>>"+order.getBuyerName());
    }     
}


注:客户端要引用jboss client jar



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值