ejb课程测试代码,eclipse+jboss5.1 ant编译等代码
三个测试项目请下载:正在上传中,请稍等
下面代码只是一个项目测试的代码,
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="rui" transaction-type="JTA"> <jta-data-source>java:ruiDs</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.formate_sql" value="true"/> </properties> </persistence-unit> </persistence>
ant
build.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 定义一些变量,就好比在java中定义一些变量,以备下面使用 --> <!-- 所在项目的位置,。代表项目下,。。项目的子包下 --> <project name="entityBean" basedir="."> <!-- 定义一个属性变量 用作指定项目的原文件所在的目录 "${basedir} =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"/> <!-- 编译 class 存放的目录 --> <property name="build.dir" value="${basedir}\build"/> <!-- 引入的jar包 --> <path id="build.classpath"> <fileset dir="${jboss.home}\client"> <include name="*.jar"/> </fileset> <!-- 把自已的 jar文件也引入 --> <pathelement location="${build.dir}"/> </path> <!-- 创建一个目录 --> <target name="prepare"> <delete dir="${build.dir}"/> <mkdir dir="${build.dir}"/> </target> <!-- 编译 includeantruntime="on" --> <target name="compile" depends="prepare" description="编译"> <javac srcdir="${src.dir}" destdir="${build.dir}" includes="org/**"> <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> <!-- 依懒于ejbjar 把编译好的class拷贝到jboss发布目录下--> <target name="deploy" depends="ejbjar" description="发布ejb"> <copy file="${basedir}\${ant.project.name}.jar" todir="${jboss.home}\server\${jboss.server.config}\deploy"/> </target> <target name="undeploy" description="御载ejb"> <delete file="${jboss.home}\server\${jboss.server.config}\deploy\${ant.project.name}.jar"/> </target> </project>
jndl
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory java.naming.provider.url=localhost:1099
bean
package org.rui.bean; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="person") public class Person implements Serializable { private static final long serialVersionUID = 6623828141664650084L; private Integer id; private String name; public Person() { } public Person(String name) { this.name=name; } //filed @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="ids",length=5) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name="name",length=20) public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (int) (id ^ (id >>> 32)); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (id != other.id) return false; return true; } }
package org.rui.Person.service; import java.util.List; import org.rui.bean.Person; public interface IPersonService { public void save(Person person); public void update(Person person); public void delete(Integer id); public Person getPerson(Integer id); public List<Person> getAllPerson(); }
package org.rui.Person.service.imp; import java.util.List; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import org.rui.Person.service.IPersonService; import org.rui.bean.Person; @Stateless //无状态bean @Remote(IPersonService.class) //远程接口 public class PersonServiceImp implements IPersonService { //实体管理器 @PersistenceContext(unitName="rui") EntityManager em; @Override public void save(Person person) { em.persist(person); } @Override public void update(Person person) { em.merge(person); } @Override public void delete(Integer id) { em.remove(em.getReference(Person.class, id)); } @Override public Person getPerson(Integer id) { return em.find(Person.class, id); } @SuppressWarnings("unchecked") @Override public List<Person> getAllPerson() { //jp ql语句 return em.createQuery("select p from Person p").getResultList(); } }
package junit.test; import java.util.List; import javax.naming.InitialContext; import org.junit.BeforeClass; import org.junit.Test; import org.rui.Person.service.IPersonService; import org.rui.bean.Person; public class IPersonServiceTest { private static IPersonService service; @BeforeClass public static void setUpBeforeClass() throws Exception { try { InitialContext init=new InitialContext(); service=(IPersonService) init.lookup("PersonServiceImp/remote"); } catch (RuntimeException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void testSave() { Person p=new Person(); p.setName("小明"); service.save(p); System.out.println(); } @Test public void testUpdate() { Person p=service.getPerson(2); p.setName("大明"); service.update(p); } @Test public void testDelete() { service.delete(1); } @Test public void testGetPerson() { Person p=service.getPerson(2); System.out.println(p.getName()); } @Test public void testGetAllPerson() { List<Person> list=service.getAllPerson(); for(Person p:list){ System.out.println(p.getName()); } } }