我用的是 mysql 数据库, EJB3.0 相对于2.0 变化蛮大的,真的是迫于 Spring+Hibernate 的逼式,ejb 3.0 已经出现几年了,自从她轻装上阵,也不知道现在的应用情况如何,不过本人认为, ejb 3.0 是很有市场的。它的简洁和方便无疑是程序员的最佳选择!
1. 配置 JBOSS 数据源:
如果你用默认的数据源就不需要这么改动,默认的是 Hsqldb。为了换成 mysql,而且把数据源的 JNDI 改成自己的名字,你需要如下配置:
一、拷贝驱动
我启动的是 default !
所以请保证JBOSS_INSTALL\server\default\lib下面有mysql jdbc driver
二、修改 JBOSS 配置文件
1、将文件JBOSS_INSTALL/docs/examples/jca/mysql-ds.xml拷贝到JBOSS_INSTALL\server\default\deploy下
面
修改如下:
<connection-url>jdbc:mysql://localhost:3306/jbossdb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>123</password>
JBOSS_INSTALL/server/default/deploy/jms下
修改如下:
name="jboss.mq:service=PersistenceManager">
<depends optional-attribute-
name="ConnectionManager">jboss.jca:service=DataSourceBinding,name=MySqlDS</depends>
<attribute name="SqlProperties">
另外这个文件71行修改:
CREATE_TX_TABLE = CREATE TABLE JMS_TRANSACTIONS ( TXID INTEGER PRIMARY KEY NOT
NULL )
3、修改JBOSS_INSTALL/server/default/conf/standardjaws.xml如下:
<!-- <type-mapping>Hypersonic SQL</type-mapping> -->
<type-mapping>mySQL</type-mapping>
name="ConnectionManager">jboss.jca:service=DataSourceBinding,name=MysqlDS</depends>
name="jboss.ejb:service=EJBTimerService,persistencePolicy=database">
<!-- DataSource JNDI name -->
<depends optional-attribute-name="DataSource">jboss.jca:service=DataSourceBinding,name=MysqlDS</depends>
<!-- The plugin that handles database persistence -->
<attribute
name="DatabasePersistencePlugin">org.jboss.ejb.txtimer.GeneralPurposeDatabasePersistencePlugin</attribute>
</mbean>
下:
<!-- DataSource JNDI name -->
<depends optional-attribute-name="DataSource">jboss.jca:service=DataSourceBinding,name=MysqlDS</depends>
2.MyEclipse6 开发 EntityBean
persistence.xml 配置文件
<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="testUnit">
<jta-data-source>java:/MysqlDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop" />//create-drop 如果有表结构可以换成 update!
//相关详细内容请看书
</properties>
</persistence-unit>
</persistence>
import java.util.List;
import org.jpa.test.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(int max, int whichpage);
}
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 org.jpa.test.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(int max, int whichpage) {
try {
int index = (whichpage - 1) * max;
Query query = em.createQuery("from Person p order by personid asc");
List list = query.setMaxResults(max).setFirstResult(index)
.getResultList();
em.clear();// 分离内存中受EntityManager管理的实体bean,让VM进行垃圾回收
return list;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@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(strategy = GenerationType.IDENTITY)
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;
}
@Temporal(value = TemporalType.DATE)
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}