下的mysql-ds.xml作出适当修改后复制到jboss-4.0.3SP1/server/all/deploy目录
下,这是我修改后的mysql-ds.xml文件:
<? xml version="1.0" encoding="UTF-8" ?> <!-- $Id: mysql-ds.xml,v 1.3.2.1 2004/12/01 11:46:00 schrouf Exp $ --> <!-- Datasource config for MySQL using 3.0.9 available from:http://www.mysql.com/downloads/api-jdbc-stable.html --> < datasources > < local-tx-datasource > < jndi-name > MySqlDS </ jndi-name > < connection-url > jdbc:mysql://localhost:3306/test </ connection-url > < driver-class > com.mysql.jdbc.Driver </ driver-class > < user-name > test </ user-name > < password ></ password > < exception-sorter-class- name > org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter </ exception-sorter-class-name > <!-- sql to call when connection is created <new-connection-sql>some arbitrary sql</new-connection-sql> --> <!-- sql to call on an existing pooled connection when it is obtained from pool <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql> --> <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) --> < metadata > < type-mapping > mySQL </ type-mapping > </ metadata > </ local-tx-datasource > </ datasources >这样之后,JBoss下的MySQL Data Source配置完成。
二、创建数据库并编写Entity Bean代码
create table book(id int not null auto_increment primary key,title varchar(20) not null,author varchar(40) not null);新建Java Project,导入User Library:EJB3_JBoss,以下是类代码。
// Book.java package ejb.bean.entity; import java.io.Serializable; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table;@Entity@Table(name = " book " ) public class Book implements Serializable { /** * */ private static final long serialVersionUID = 1L ; private Integer id; private String title; private String author; public Book() { super (); } public Book(Integer id, String title, String author) { super (); this .id = id; this .title = title; this .author = author; } @Override public String toString() { return " Book: " + getId() + " Title " + getTitle() + " Author " + getAuthor(); } public String getAuthor() { return author; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getId() { return id; } public String getTitle() { return title; } public void setAuthor(String author) { this .author = author; } public void setId(Integer id) { this .id = id; } public void setTitle(String title) { this .title = title; }}
三、编写一个简单的Stateless Session Bean 并进行测试
// BookTestLocal.java package ejb.bean.entity; import javax.ejb.Local;@Local public interface BookTestLocal { public void test();}
// BookTestRemote.java package ejb.bean.entity; import javax.ejb.Remote;@Remote public interface BookTestRemote { public void test();}
// BookTestBean.java package ejb.bean.entity; import javax.ejb.Stateless; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext;@Stateless public class BookTestBean implements BookTestLocal, BookTestRemote { @PersistenceContext EntityManager em; public void test() { // TODO Auto-generated method stub Book book = new Book( null , " My first bean book " , " Sebastian " ); em.persist(book); }}
// Client.java package ejb.client.entity; import ejb.bean.entity. * ; import javax.naming.InitialContext; /** * Comment * * @author <a href="mailto:bill@jboss.org">Bill Burke</a> * @version $Revision: 1.1.6.7 $ */ public class Client{ public static void main(String[] args) throws Exception { InitialContext ctx = new InitialContext(); BookTestRemote book = (BookTestRemote) ctx.lookup( " BookTestBean/remote " ); book.test(); System.out.println( " test successful! " ); }}
三、其他文件将jboss-EJB-3.0_RC5-PFD/docs/tutorial中的找到的jndi.properties、log4j.xml、builder.xml等复制到当前工程中,其中builder.xml需要修改。//builder.xml
<? xml version="1.0" ?> <!-- ======================================================================= --> <!-- JBoss build file --> <!-- ======================================================================= --> < project name ="JBoss" default ="ejbjar" basedir ="." > < property file ="../local.properties" /> < property environment ="env" /> < property name ="src.dir" value ="${basedir}/src" /> < property name ="jboss.home" value ="E:/Programming/Servers/jboss-4.0.3SP1/" /> < property name ="jboss.server.config" value ="all" /> < property name ="build.dir" value ="${basedir}/build" /> < property name ="build.classes.dir" value ="${build.dir}/classes" /> <!-- Build classpath --> < path id ="classpath" > <!-- So that we can get jndi.properties for InitialContext --> < pathelement location ="${basedir}" /> < fileset dir ="${jboss.home}/lib" > < include name ="**/*.jar" /> </ fileset > < fileset dir ="${jboss.home}/server/${jboss.server.config}/lib" > < include name ="**/*.jar" /> </ fileset > < fileset dir ="${jboss.home}/server/${jboss.server.config}/deploy/ejb3.deployer" > < include name ="*.jar" /> </ fileset > < fileset dir ="${jboss.home}/server/${jboss.server.config}/deploy/jboss-aop-jdk50.deployer" > < include name ="*.jar" /> </ fileset > < pathelement location ="${build.classes.dir}" /> </ path > < property name ="build.classpath" refid ="classpath" /> <!-- =================================================================== --> <!-- Prepares the build directory --> <!-- =================================================================== --> < target name ="prepare" > < mkdir dir ="${build.dir}" /> < mkdir dir ="${build.classes.dir}" /> </ target > <!-- =================================================================== --> <!-- Compiles the source code --> <!-- =================================================================== --> < target name ="compile" depends ="prepare" > < javac srcdir ="${src.dir}" destdir ="${build.classes.dir}" debug ="on" deprecation ="on" optimize ="off" includes ="**" > < classpath refid ="classpath" /> </ javac > </ target > < target name ="ejbjar" depends ="compile" > < jar jarfile ="build/tutorial.jar" > < fileset dir ="${build.classes.dir}" > < include name ="**/*.class" /> </ fileset > < fileset dir ="." > < include name ="META-INF/persistence.xml" /> </ fileset > </ jar > < copy file ="build/tutorial.jar" todir ="${jboss.home}/server/${jboss.server.config}/deploy" /> </ target > < target name ="run.stateless" depends ="ejbjar" > < java classname ="ejb.client.stateless.Client" fork ="yes" dir ="." > < classpath refid ="classpath" /> </ java > </ target > < target name ="run.stateful" depends ="ejbjar" > < java classname ="ejb.client.stateful.Client" fork ="yes" dir ="." > < classpath refid ="classpath" /> </ java > </ target > < target name ="run.timer" depends ="ejbjar" > < java classname ="ejb.client.timer.Client" fork ="yes" dir ="." > < classpath refid ="classpath" /> </ java > </ target > < target name ="run.entity" depends ="ejbjar" > < java classname ="ejb.client.entity.Client" fork ="yes" dir ="." > < classpath refid ="classpath" /> </ java > </ target > <!-- =================================================================== --> <!-- Cleans up generated stuff --> <!-- =================================================================== --> < target name ="clean.db" > < delete dir ="${jboss.home}/server/${jboss.server.config}/data/hypersonic" /> </ target > < target name ="clean" > < delete dir ="${build.dir}" /> < delete file ="${jboss.home}/server/${jboss.server.config}/deploy/tutorial.jar" /> </ target > </ project >最后,在工程目录下新建目录META-INF,在目录META-INF新建persistence.xml文件,以下是文件内容:
<? xml version="1.0" encoding="UTF-8" ?> < persistence > < persistence-unit name ="test" > < jta-data-source > java:/MySqlDS </ jta-data-source > < properties > < property name ="hibernate.dialect" value ="org.hibernate.dialect.MySQLDialect" /> < property name ="hibernate.hbm2ddl.auto" value ="update" /> </ properties > </ persistence-unit > </ persistence >四、运行测试run as->ant build后,选择运行目标为run.entity->run。运行结果:
Buildfile: D:/Programs/Java/EclipseWork/EJB3/build.xmlprepare:compile:ejbjar:run.entity: [ java ] test successful!BUILD SUCCESSFULTotal time: 9 secondsMySQL中
select * from book; id title author 1 My first bean book Sebastian已经成功写入。
