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中
- <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 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&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>
<?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&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 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>
<?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
- @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
- }
@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
- public interface BaseOperaion {
- List findAll();
- Object find(Class c, Serializable id);
- void del(Object obj);
- Object update(Object obj);
- void add(Object obj);
- }
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
- @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();
- }
- }
@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
- <%@ page language="java" pageEncoding="UTF-8"%>
- <%@ page import="java.util.Properties"%>
- <%@ page import="javax.naming.InitialContext"%>
- <%@ page import="javax.naming.Context"%>
- <%@ page import="dao.BaseOperaion"%>
- <%@ page import="java.util.List"%>
- <%@page import="beans.StudentEntity"%>
- <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
- <%
- Properties porps = new Properties();
- porps.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
- porps.setProperty(Context.PROVIDER_URL,"jnp://168.192.0.1:1099");
- porps.setProperty(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
- InitialContext context = new InitialContext(porps);
- BaseOperaion bo = (BaseOperaion) context.lookup("StudentDaoBean/remote");
- // System.out.println(hw.sayHello("fantasy"));
- // del
- StudentEntity stu01 = (StudentEntity) bo.find(StudentEntity.class, 1);
- bo.del(stu01);
- // show all
- List result = bo.findAll();
- %>
- <table>
- <tr>
- <th>编号</th>
- <th>姓名</th>
- <th>年龄</th>
- <th>性别</th>
- </tr>
- <c:forEach items="<%=result %>" var="stu" >
- <tr>
- <td>${stu.id }</td>
- <td>${stu.name }</td>
- <td>${stu.age }</td>
- <td>${stu.sex }</td>
- </tr>
- </c:forEach>
- </table>