MyEclipse创建EJB3.0

MyEclipse版本:6.0.1
JBoss版本:4.0.4 GA
Tomcat版本:6.0.14

 

一、创建EJB Project
1.


2.


3.

 

Persistence Unit Name:
对应persistence.xml中

Xml代码
  1. <persistence-unit name="stuSystemPU" transaction-type="JTA">  
<persistence-unit name="stuSystemPU" transaction-type="JTA">


的信息。

 

JNDI Data Source:
对应JBoss部署文件夹deploy下mysql-ds.xml中<jndi-name>的信息。

下面的JPA设置不影响JBoss服务器的数据源设置。只是用于在设计EJB的时帮助查看数据库元数据和表数据。

4.


5. mysql-ds.xml

 

Xml代码
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2.   
  3. <datasource>  
  4.     <local-tx-datasource>  
  5.         <jndi-name>stuMySqlDS</jndi-name>  
  6.         <connection-url>  
  7.             jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=GB2312  
  8.         </connection-url>  
  9.         <driver-class>com.mysql.jdbc.Driver</driver-class>  
  10.         <user-name>root</user-name>  
  11.         <password>admin</password>  
  12.         <exception-sorter-class-name>  
  13.             org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter  
  14.         </exception-sorter-class-name>  
  15.         <min-pool-size>3</min-pool-size>  
  16.         <max-pool-size>32</max-pool-size>  
  17.         <!-- 抛出异常前最大等待时间 -->  
  18.         <blocking-timeout-millis>60000</blocking-timeout-millis>  
  19.         <!-- 关闭连接前最大空闲时间 -->  
  20.         <idel-timeout-minutes>5</idel-timeout-minutes>  
  21.   
  22.         <metadata>mySQL</metadata>  
  23.     </local-tx-datasource>  
  24. </datasource>  
<?xml version="1.0" encoding="UTF-8" ?>

 

<datasource>
<local-tx-datasource>
<jndi-name>stuMySqlDS</jndi-name>
<connection-url>
jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=GB2312
</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>admin</password>
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<min-pool-size>3</min-pool-size>
<max-pool-size>32</max-pool-size>
<!-- 抛出异常前最大等待时间 -->
<blocking-timeout-millis>60000</blocking-timeout-millis>
<!-- 关闭连接前最大空闲时间 -->
<idel-timeout-minutes>5</idel-timeout-minutes>

<metadata>mySQL</metadata>
</local-tx-datasource>
</datasource>

 


6.persistence.xml

 

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <persistence xmlns="http://java.sun.com/xml/ns/persistence"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/persistence  
  5.     http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  
  6.     version="1.0">  
  7.   
  8.     <persistence-unit name="StuEntityPU" transaction-type="JTA">  
  9.         <jta-data-source>java:/stuMySqlDS</jta-data-source>  
  10.           
  11.         <!-- 下面属性只针对JBoss服务器 -->  
  12.         <properties>  
  13.             <property name="hibernate.hbm2ddl.auto" value="create-drop" />  
  14.             <!-- JDBC抓取数量的大小:Statement.setFetchSize() -->  
  15.             <property name="hibernate.jdbc.fetch_size" value="15" />  
  16.             <!-- JDBC批量更新数量 -->  
  17.             <property name="hibernate.jdbc.batch_size" value="10"/>  
  18.             <property name="hibernate.show_sql" value="true" />  
  19.             <property na  
  20. me="hibernate.format_sql" value="true" />  
  21.         </properties>  
  22.     </persistence-unit>  
  23. </persistence>  
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="1.0">

 

<persistence-unit name="StuEntityPU" transaction-type="JTA">
<jta-data-source>java:/stuMySqlDS</jta-data-source>

<!-- 下面属性只针对JBoss服务器 -->
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<!-- JDBC抓取数量的大小:Statement.setFetchSize() -->
<property name="hibernate.jdbc.fetch_size" value="15" />
<!-- JDBC批量更新数量 -->
<property name="hibernate.jdbc.batch_size" value="10"/>
<property name="hibernate.show_sql" value="true" />
<property na
me="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>

 

7. 实体Bean:StudentEntity.java

 

Java代码
  1. @Entity  
  2. @Table(name="Student")  
  3. public class StudentEntity implements Serializable {  
  4.       
  5.     // 属性  
  6.     @Id  
  7.     @GeneratedValue(strategy=GenerationType.AUTO)  
  8.     @Column(name="id")  
  9.     private int id;  
  10.       
  11.     @Column(name="name",nullable=false)  
  12.     private String name;  
  13.   
  14.     @Column(name="age")  
  15.     private int age;  
  16.       
  17.     @Column(name="sex")  
  18.     private String sex;  
  19.     // 省略getter和setter  
  20. }  
@Entity
@Table(name="Student")
public class StudentEntity implements Serializable {

 

// 属性
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;

@Column(name="name",nullable=false)
private String name;

@Column(name="age")
private int age;

@Column(name="sex")
private String sex;
// 省略getter和setter
}

 

 


8. 用户操作接口:BaseOperation.java

 

Java代码
  1. public interface BaseOperaion {  
  2.     List findAll();  
  3.     Object find(Class c, Serializable id);  
  4.     void del(Object obj);  
  5.     Object update(Object obj);  
  6.     void add(Object obj);  
  7. }  
public interface BaseOperaion {
List findAll();
Object find(Class c, Serializable id);
void del(Object obj);
Object update(Object obj);
void add(Object obj);
}

 

 

 

9. 会话Bean:StudentDaoBean.java

 

Java代码
  1. @Stateless  
  2. @Remote(BaseOperaion.class)  
  3. public class StudentDaoBean implements BaseOperaion{  
  4.       
  5.     @PersistenceContext(unitName="StuEntityPU")  
  6.     private EntityManager em;  
  7.       
  8.     public void add(Object obj) {  
  9.         em.persist(obj);  
  10.     }  
  11.   
  12.     public void del(Object obj) {  
  13.         em.remove(obj);  
  14.     }  
  15.   
  16.     public Object find(Class c, Serializable id) {  
  17.         return em.find(c, id);  
  18.     }  
  19.   
  20.     public Object update(Object obj) {  
  21.         return em.merge(obj);  
  22.     }  
  23.   
  24.     public List findAll() {  
  25.         return em.createQuery("FROM StudentEntity").getResultList();  
  26.     }  
  27. }  
@Stateless
@Remote(BaseOperaion.class)
public class StudentDaoBean implements BaseOperaion{

 

@PersistenceContext(unitName="StuEntityPU")
private EntityManager em;

public void add(Object obj) {
em.persist(obj);
}

public void del(Object obj) {
em.remove(obj);
}

public Object find(Class c, Serializable id) {
return em.find(c, id);
}

public Object update(Object obj) {
return em.merge(obj);
}

public List findAll() {
return em.createQuery("FROM StudentEntity").getResultList();
}
}

 

 

10. 部署上面创建的EJB.jar和数据库驱动包到JBoss
注意JBoss与Tomcat的http端口设置。

11. Tomcat 访问
stuSystem.jsp

 

Java代码
  1. <%@ page language="java" pageEncoding="UTF-8"%>  
  2. <%@ page import="java.util.Properties"%>  
  3. <%@ page import="javax.naming.InitialContext"%>  
  4. <%@ page import="javax.naming.Context"%>  
  5. <%@ page import="dao.BaseOperaion"%>  
  6. <%@ page import="java.util.List"%>  
  7. <%@page import="beans.StudentEntity"%>  
  8. <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  9.   
  10.   
  11. <%  
  12.     Properties porps = new Properties();  
  13.     porps.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");  
  14.     porps.setProperty(Context.PROVIDER_URL,"jnp://168.192.0.1:1099");  
  15.     porps.setProperty(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");  
  16.       
  17.     InitialContext context = new InitialContext(porps);  
  18.     BaseOperaion bo = (BaseOperaion) context.lookup("StudentDaoBean/remote");  
  19.     // System.out.println(hw.sayHello("fantasy"));  
  20.       
  21.     // del  
  22.     StudentEntity stu01 = (StudentEntity) bo.find(StudentEntity.class1);  
  23.     bo.del(stu01);  
  24.       
  25.     // show all  
  26.     List result = bo.findAll();  
  27. %>  
  28.   
  29. <table>  
  30.     <tr>  
  31.         <th>编号</th>  
  32.         <th>姓名</th>  
  33.         <th>年龄</th>  
  34.         <th>性别</th>  
  35.     </tr>  
  36.     <c:forEach items="<%=result %>" var="stu" >  
  37.         <tr>  
  38.             <td>${stu.id }</td>  
  39.             <td>${stu.name }</td>  
  40.             <td>${stu.age }</td>  
  41.             <td>${stu.sex }</td>  
  42.         </tr>  
  43.     </c:forEach>  
  44. </table> 
posted on 2010-11-24 21:29  aurawing  阅读(1436)  评论(1编辑  收藏  举报