最后的目录层次结构:

mysql数据库结构:

按目录层次,给出各个类文件:
Person.java:(此为
)
package com.ejb;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@SuppressWarnings("serial")
@Entity
@Table(name = "Person")
public class Person implements Serializable{
private Integer personid;
private String name;
private boolean sex;
private Short age;
private Date birthday;
@Id
@GeneratedValue
public Integer getPersonid() {
return personid;
}
public void setPersonid(Integer personid) {
this.personid = personid;
}
@Column(name = "PersonName",nullable=false,length=32)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(nullable=false)
public boolean getSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Column(nullable=false)
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
PersonDAO.java:
package com.session;
import java.util.Date;
import java.util.List;
import com.ejb.Person;
public interface PersonDAO {
public boolean insertPerson(String name, boolean sex,short age, Date birthday);
public String getPersonNameByID(int personid);
public boolean updatePerson(Person person);
public Person getPersonByID(int personid);
public List getPersonList();
}
PersonDAOBean.java:(此为
,用于测试实体bean)
package com.session;
import java.util.Date;
import java.util.List;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import com.session.PersonDAO;
import com.ejb.Person;
@Stateless
@Remote ({PersonDAO.class})
public class PersonDAOBean implements PersonDAO {
@PersistenceContext
protected EntityManager em;
public String getPersonNameByID(int personid) {
Person person = em.find(Person.class, Integer.valueOf(personid));
return person.getName();
}
public boolean insertPerson(String name, boolean sex,short age, Date birthday) {
try {
Person person = new Person();
person.setName(name);
person.setSex(sex);
person.setAge(Short.valueOf(age));
person.setBirthday(birthday);
em.persist(person);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public Person getPersonByID(int personid) {
return em.find(Person.class, Integer.valueOf(personid));
}
public boolean updatePerson(Person person) {
try {
em.merge(person);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public List getPersonList() {
try {
Query query = em.createQuery("from Person p order by personid asc");
List list = query. getResultList();
em.clear();//分离内存中受EntityManager管理的实体bean,让VM进行垃圾回收
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
然后创建一个web项目EntityBeanTest,在默认路径建一个jsp测试文件Test.jsp:

Test.jsp:
<%@ page contentType="text/html; charset=GBK"%>
<%@ page import="com.session.*,
com.ejb.*,
javax.naming.*,
java.util.Properties,
java.util.Date,
java.util.List,
java.util.Iterator,
java.text.SimpleDateFormat"%>
<%
Properties props = new Properties();
props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "localhost:1099");
props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
InitialContext ctx = new InitialContext(props);
try {
PersonDAO persondao = (PersonDAO) ctx.lookup("PersonDAOBean/remote");
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
persondao.insertPerson("刘宽", true, (short)26,formatter.parse("1980-9-30"));//添加一个人
out.println(persondao.getPersonNameByID(1)); //取personid为1的人姓名
Person person = persondao.getPersonByID(1); //取personid为1的person,此时的person已经脱离容器的管理
person.setName("张小艳"); //把姓名改为张小艳
persondao.updatePerson(person); //更新person
out.println("<br> personid为1的person姓名已由“刘宽”改为“张小艳”<br>");
List list = persondao.getPersonList(); //分页结果集
if (list!=null){
Iterator it = list.iterator();
while (it.hasNext()) {
Person p = (Person)it.next();
out.println("人员编号:"+ p.getPersonid() + "\t姓名:"+ p.getName() + "<Br>");
}
}
} catch (Exception e) {
out.println(e.getMessage());
}
%>
结出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="entityPU" transaction-type="JTA">
<jta-data-source>java:/MySqlDS</jta-data-source>
</persistence-unit>
</persistence>
最后,发布ejb和web项目,在地址栏输入:http://localhost:8080/EntityBeanTest/Test.jsp
在网页下的输出结果如下:
张小艳
personid 为1的person姓名已由“刘宽”改为“张小艳”
人员编号:1 姓名:张小艳
本人亲测可行!至于其他平台和不同版本的情况下可能会有问题,欢迎探讨!
本文详细介绍了使用Java实现数据库操作的实体类设计、持久化层接口定义及远程调用过程,通过创建测试页面展示实体类增删改查功能,并提供了persistence.xml配置文件以确保与数据库的连接。
2292

被折叠的 条评论
为什么被折叠?



