我用的是 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下
面
修改如下:
<jndi-name>MySqlDS</jndi-name>
<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>
2、将文件JBOSS_INSTALL/docs/examples/jms/mysql-jdbc2-service.xml拷贝到
JBOSS_INSTALL/server/default/deploy/jms下
修改如下:
<mbean code="org.jboss.mq.pm.jdbc2.PersistenceManager"
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如下:
<datasource>java:/MysqlDS</datasource>
<!-- <type-mapping>Hypersonic SQL</type-mapping> -->
<type-mapping>mySQL</type-mapping>
4、修改JBOSS_INSTALL/server/default/deploy/jms/hsqldb-jdbc-state-service.xml如下:
<depends optional-attribute-
name="ConnectionManager">jboss.jca:service=DataSourceBinding,name=MysqlDS</depends>
5、修改JBOSS_INSTALL/server/default/deploy/ejb-deployer.xml如下:
<mbean code="org.jboss.ejb.txtimer.DatabasePersistencePolicy"
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>
6、修改JBOSS_INSTALL/server/default/config/ standardjbosscmp-jdbc.xml如下:
<defaults> <datasource>java:/MysqlDS</datasource>
7、修改JBOSS_INSTALL/server/default/deploy/ uuid-key-generator.sar下面的META-INF下面jboss-service.xml如
下:
<!-- DataSource JNDI name -->
<depends optional-attribute-name="DataSource">jboss.jca:service=DataSourceBinding,name=MysqlDS</depends>
好了数据源配置方面已经完成!可以开始动手开发了!
2.MyEclipse6 开发 EntityBean
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="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>
3.PersonDAO 接口
import java.util.Date;
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);
}
4.PersonDAOBean 无状态的会话Bean
package org.jpa.dao;
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;
}
}
}
5.类似 Hibernate 中开发 POJO 一样开发 EntityBean,不同的就是把 ORM 的 mapping file 改用在 code 中加入注释,效果类似!如果你学习过 jdo,ojb,hibernate,ibatis 等等你很容易上手!
package org.jpa.test;
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;
}
}
本博客为学习交流用,凡未注明引用的均为本人作品,转载请注明出处,如有版权问题请及时通知。由于博客时间仓促,错误之处敬请谅解,有任何意见可给我留言,愿共同学习进步。