ejb项目Helloword
首先把jboss下client下的jar全都引入项目中
package cn.ldf.bean;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="people")
public class People implements Serializable{
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String sex;
private Integer age;
public People() {
}
public People(String name, String sex, Integer age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
@Id @GeneratedValue
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(length=2)
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
@Column(length=3)
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final People other = (People) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
return false;
return true;
}
}
package cn.ldf.service.impl;
import java.util.List;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import cn.ldf.bean.People;
import cn.ldf.service.PeopleService;
@Stateless
//定义为无状态bean
@Remote(PeopleService.class)
@Local(PeopleService.class)
public class PeopleServiceBean implements PeopleService {
@PersistenceContext(unitName = "itcast")
EntityManager em;
// unitName 为持久化单元名字
@Override
public void delete(Integer id) {
// TODO Auto-generated method stub
em.remove(em.getReference(People.class, id));
}
@SuppressWarnings("unchecked")
public List<People> getAllPeople() {
// TODO Auto-generated method stub
// 面向对象的语句 这里的People为实体的名称
return em.createQuery("select 0 from People 0").getResultList();
}
public People getPeopleById(Integer id) {
// TODO Auto-generated method stub
return em.find(People.class, id);
}
public void save(People p) {
// TODO Auto-generated method stub
// 对新建状态的保存
em.persist(p);
}
public void update(People p) {
// TODO Auto-generated method stub
// 必须是游离状态的时候才能调用这个方法
em.merge(p);
}
}
package cn.ldf.service;
import java.util.List;
import cn.ldf.bean.People;
public interface PeopleService {
public void save(People p);
public void update(People p);
public void delete(Integer id);
public People getPeopleById(Integer id);
public List<People> getAllPeople();
}
build.xml
<?xml version="1.0"?>
<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="itcast" transaction-type="JTA">
<!-- JPA的驱动,就类似JDBC的驱动,是跟JPA实现对接的入口类 -->
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:itcast</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<!--
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="org.gjt.mm.mysql.Driver"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="000000"/>
<property name="hibernate.connection.url" value="jdbc:mysql://127.0.0.1:3306/itcast?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=UTF-8"/>
-->
<property name="hibernate.hbm2ddl.auto" value="create"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
<?xml version="1.0" encoding="UTF-8"?>
<project name="helloWorld" basedir=".">
<property name="src.dir" value="${basedir}\src"/>
<property environment="env"/>
<property name="jboss.home" value="${env.JBOSS_HOME}"/>
<property name="jboss.server.config" value="default"/>
<property name="build.dir" value="${basedir}\build"/>
<path id="build.classpath">
<fileset dir="${jboss.home}\client">
<include name="*.jar"/>
</fileset>
<pathelement location="${build.dir}"/>
</path>
<target name="prepare">
<delete dir="${build.dir}"/>
<mkdir dir="${build.dir}"/>
</target>
<target name="compile" depends="prepare" description="编绎">
<javac srcdir="${src.dir}" destdir="${build.dir}">
<classpath refid="build.classpath"/>
</javac>
</target>
<target name="ejbjar" depends="compile" description="创建EJB发布包 ">
<jar jarfile="${basedir}\${ant.project.name}.jar">
<fileset dir="${build.dir}">
<include name="**/*.class"/>
</fileset>
<metainf dir="${src.dir}\META-INF"></metainf>
</jar>
</target>
<target name="deploy" depends="ejbjar" description="发布ejb">
<copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy"></copy>
</target>
<target name="undeploy" description="卸载ejb">
<delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar"/>
</target>
</project>
把helloword项目放入jboss下。
建一个web项目,把jobss下的client目录下的jbossall-client.jar放入项目里(如果不行可以把client所有的jar包放入了)
把上面的ejb项目打成jar包,这个jar包不包含实现类。把它也放入web项目中
注意:如何你的程序下有资源文件jndi.properties文件的话,你最好能弄好它的位置,因为你要是用main方法运行的话那它就能找到这个文件。如何的用web运行的话,那你还把它把到src下,那么就打不到这个文件啊。就会报,XXXX类 name not boud int this context错误。而你用main函数运行时而不报错,像我下面那样写就一定不会有错的
package cn.ldf;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.ldf.bean.People;
import cn.ldf.service.PeopleService;
public class InsertPeople extends HttpServlet {
private static final long serialVersionUID = 1L;
public InsertPeople() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
String agestr=request.getParameter("age");
String sex=request.getParameter("sex");
int age =Integer.parseInt(agestr);
PeopleService ps=null;
InitialContext ctx=null;
People p = new People();
try {
Properties pro = new Properties();
pro.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
pro.setProperty("java.naming.provider.url", "127.0.0.1:1099");
ctx = new InitialContext(pro);
ps = (PeopleService) ctx.lookup("PeopleServiceBean/remote");
//p.setId(1);
p.setName(name);
p.setSex(sex);
p.setAge(age);
ps.save(p);
System.out.println("添加成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
PeopleService ps=null;
InitialContext ctx=null;
People p = new People();
//jnp\://127.0.0.1\:1099
System.out.println("jnp\\://127.0.0.1\\:1099");
}
}
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
This is my JSP page. <br>
<form action="InsertPeople" method="post">
姓名 <input type="text" name="name"/><br>
年龄<input type="text" name="age"/><br>
性别<input type="text" name="sex"/><br>
<input type="submit" value="提交"/>
<input type="reset" value="重置"/>
</form>
</body>
</html>