什么是EJB?
EJB 是 Java 企業(yè)Bean, 是JavaEE服務端 企業(yè)組件模型,它的設計目標與核心應用是部署分布式應用程序。話不多說,直接看如何在本機部署EJB3。
部署環(huán)境:
操作系統(tǒng):Windows 8.1
EJB容器:Jboss 7.1
DB: MySQL 5.6.10
IDE: MyEclipse 10
JDK: 1.6
1、創(chuàng)建數據庫、表
由于在此過程中,需要和數據庫通信,需要首先創(chuàng)建數據庫表。
創(chuàng)建數據庫:
1
|
create database student; //創(chuàng)建數據庫 student |
創(chuàng)建表:
1
2
3
4
5
|
create table student( //創(chuàng)建表student 和 數據庫同名 `id` integer (11) not null , ` name ` varchar2(20) default null , primary key (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1 |
1
2
|
insert into student values (1, 'easynoder' ); commit ; |
1
|
grant all privileges on *.* to root@localhost indentified by "1234" |
通過以上步驟,需要的數據庫表已建立好??赏ㄟ^root用戶訪問所有數據。
2、編寫實體Bean、用戶操作接口和會話Bean
建立EJB工程,名為MyEJBProject。該工程META-INFO目錄下包含一個文件persistence.xml文件。該文件用來配置數據源,稍后進行配置。
接著建立實體Bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
@Entity //表明這是一個實體Bean @Table (name = "student" ) //和數據庫表student 建立映射 public class StudentEntity implements Serializable { private static final long serialVersionUID = 4002145187978562529L; @Id // 表明是該實體的id @GeneratedValue (strategy = GenerationType. AUTO ) //id生成策略 @Column (name = "id" ) //對應student表id字段 private int id ; @Column (name = "name" ) // 對應student表name字段 private String name; public int getId() { return id ; } public String getName() { return name ; } public void setId( int id) { this .id = id; } public void setName(String name) { this .name = name; } } |
建立操作接口:
1
2
3
4
|
public interface BaseOperation { public List<?> findAll(); } |
該接口只有一個方法,獲取所有的學生
建立會話Bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
@Stateless //這是一個無狀態(tài)Bean @Remote (BaseOperation. class ) //指明Bean的remote接口 public class StudentDaoBean implements BaseOperation { // EntityManager是由EJB容器自動配置和管理的,unitName屬性的值對應 persistence.xml 中< persistence-unit name = "MyEJBProject" transaction-type = "JTA" ></ persistence-unit > name的配置 @PersistenceContext (unitName = "MyEJBProject" ) private EntityManager em; @SuppressWarnings ( "unchecked" ) public List<?> findAll() { System. out .println( "查詢開始..." ); List<StudentEntity> list = em.createQuery( " from StudentEntity " ).getResultList(); if (list != null ) { Iterator<StudentEntity> it = list.iterator(); while (it.hasNext()) { StudentEntity student = it.next(); System. out .println( "學生id:" + student.getId()); System. out .println( "學生名稱:" + student.getName()); } } System. out .println( "查詢完畢...." ); return list; } } |
3、數據源配置
這里需要注意下,在jboss6 和jboss7的配置是不同的。
jboss6和以前版本都是在deploy目錄下 添加**-ds.xml,這里**表示任意一種數據庫名稱,如果是mysql,則是mysql-ds.xml文件。而在jboss7中,是不一樣的。
將目錄切換至Jboss 的安裝目錄,即%JBOSS_HOME%下,進入modules/com目錄,建立文件夾mysqldatabase,進入,建立mysql文件夾,進入,建立main文件夾。
在main目錄下,建立module.xml文件,該配置文件內容為:
1
2
3
4
5
6
7
8
9
10
11
|
<? xml version = "1.0" encoding = "UTF-8" ?> < module xmlns = "urn:jboss:module:1.1" name = "com.mysqldatabase.mysql" > < resources > < resource-root path = "mysql-connector-java-5.**-bin.jar" /> </ resources > < dependencies > < module name = "javax.api" /> < module name = "javax.transaction.api" /> < module name = "javax.servlet.api" optional = "true" /> </ dependencies > </ module > |
尤其這里需要注意的是,module 節(jié)點屬性name的值,就是剛才咱們建立的文件夾的路徑。resources表示mysql驅動的路徑。意味著,需要將mysql的驅動放在main目錄下。即main目錄下包含兩個文件,module.xml和數據庫驅動文件。
在做完上一步后,切換到%JBOSS_HOME%\standalone\configuration目錄下,
打開standalone.xml,搜索datasources,進行如下配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< datasources > < datasource jndi-name = "java:jboss/KouMySQLDS" pool-name = "MySQLDS" enabled = "true" use-java-context = "true" > < connection-url >jdbc:mysql://localhost:3306/student</ connection-url > < driver >mysql</ driver > < security > < user-name >root</ user-name > < password >1234</ password > </ security > </ datasource > < drivers > < driver name = "mysql" module = "com.mysqldatabase.mysql" > < driver-class >com.mysql.jdbc.Driver</ driver-class > < xa-datasource-class >com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</ xa-datasource-class > </ driver > </ drivers > </ datasources > |
jndi-name表示數據源jndi名稱,connection-url表示連接的url字符串;這里默認使用3306端口,使用student庫,用戶名和密碼即第一步配置的。module配置的即剛剛配置的module的路徑。
jboss的相關配置已經完成,接著切換到剛新建的工程,其中有一個persistence.xml配置文件,該文件做如下配置,其中jta-data-source 就是上面配置的jndi-name.
1
2
3
4
5
6
7
8
|
< jta-data-source > java:jboss/KouMySQLDS </ jta-data-source > < properties > < property name = "hibernate.hbm2ddl.auto" value = "validate" /> < property name = "hibernate.jdbc.fetch_size" value = "15" /> < property name = "hibernate.jdbc.batch_size" value = "10" /> < property name = "hibernate.show_sql" value = "true" /> < property name = "hibernate.format_sql" value = "true" ></ property > </ properties > |
到此為止,服務端代碼和數據源配置已經完成。接下來需要做的就是如何部署代碼以及如何在客戶端調用該EJB服務。
4、部署EJB服務。
將之前在工程中寫的所有代碼打成jar包,命名為ejbservice.jar。同時,只將實體Bean和接口打包成jar包,命名為ebjinterface.jar,這個jar將來用于客戶端調用使用。
將ejbservice.jar放入%JBOSS_HOME%\standalone\deployments目錄下。在jboss啟動時,會自動掃描該目錄。然后部署該jar。
ok,我們將jboss配置到MyEclipse下,在MyEclipse中啟動Jboss,觀察控制臺的輸出。
如果出現了 Deployed "ejbservice.jar" 這個日志,說明ejb就部署成功了。
5、客戶端如何調用呢?
客戶端調用需要兩個必備條件:
引入jboss-ejb-client.properties配置、 jboss-client.jar和ejbinterface.jar。其中jboss-client.jar 位于jboss bin/client目錄下。ejbinterface.jar是我們剛剛創(chuàng)建的客戶端需要使用的接口jar包。
jboss-ejb-client.properties配置如下:
1
2
3
4
5
6
7
8
|
endpoint.name= client-endpoint remote.connectionprovider.create.options.org.xnio.Options.SSL_ENABLED= false remote.connections= default remote.connection. default .host= localhost remote.connection. default .port= 4447 remote.connection. default .connect.options.org.xnio.Options.SASL_POLICY_NOANONYMOUS= false remote.connection. default .username= yourUsername remote.connection. default .password= yourPassword |
有了這兩個條件,就可以安心的建立個測試類EJBTest.java,編寫客戶端方法了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public static void main(String[] args) { Properties props = new Properties(); props.setProperty(Context. URL_PKG_PREFIXES, "org.jboss.ejb.client.naming" ); try { Context context = new InitialContext(props); // 這里需要注意字符串的寫法:ejbservice 表示ejb的包名,StudentDaoBean表示咱們實際調用的會話Bean,org.easynoder.ejb2.dao.BaseOperation表示 對應的接口 BaseOperation op = (BaseOperation) context .lookup( "ejb:/ejbservice//StudentDaoBean!org.easynoder.ejb2.dao.BaseOperation" ); op.findAll(); } catch (NamingException e) { e.printStackTrace(); } } |
運行這段代碼,可以成功的查詢到數據庫的數據啦。
至此,EJB就部署成功啦。