基於EJB3.0的留言板專案的開發全過程

beifengwang發表於2014-03-28

因為工作關係,所以準備了EJB3.0的課程,其實以前就做過了這方面的工作,但是當時沒有想到要講課,所以學的很淺,而且很長時間不用也忘記了,這會又重寫看了一下。其實,網上有很多的視訊或者是什麼的,但是貌似我就是沒有看到一個稍微完整的專案例子的,很多都只是連上了資料庫,稍微寫一個方法就算完事了,所以這一次,我寫了一個留言板,很簡單的功能。當然很簡單了,只是一對一的關係,但是這裡面卻也有一些體會。

好了,開始寫了哦!
首先,不用說別的,肯定需要連線資料庫,所以先建立一個資料庫吧,我用的是mysql。都說了是很簡單的留言板例子了,資料庫當然簡單的不象話了,這個不用多說了。配置一下資料來源,說白了就是xml檔案。這個檔案不詳細講解了,一會我會把所有的檔案打包傳上來,並且在我前面的教程中也有這些內容。
好了,這幾個準備工作完成了,那麼我們建立一個EJB Project(EJB專案)。我們要知道這樣子一個事情,EJB,我們用來作出一個一個的模組,然後我們在專案中去使用它。
專案建立好了以後,匯入jar包,在jboss下的client下面。好了,一個專案建好了,我們在persistence.xml(在META-INF目錄下面),建立專案的時候自動建立了一個,這個檔案裡面的內容是:


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" target=_blank>http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">


java:EJBDS





簡單說一下:ejbdb是我後面要用到的一個名字,隨便起,當然要滿足一定的規則。EJBDS是我配置的mysql-ds.xml裡面的JNDI名字。因為這是一個全域性的JNDI,所以我們使用java:EJBDS就可以找到。

接著看,我們開始建立Entity Bean。這個更加簡單了,尤其是對於有Hibernate基礎的朋友來說,這部分真的實在是太簡單了。先把程式碼給大家。
package org.adam.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="user")
public class UserBean implements Serializable{

private Integer user_id;

private String username;

private String password;

@Id
@Column(name="user_id")
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getUser_id() {
return user_id;
}

public void setUser_id(Integer user_id) {
this.user_id = user_id;
}

@Column(name="username",length=50,nullable=false)
public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

@Column(name="password",length=50,nullable=false)
public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

package org.adam.bean;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="context")
public class ContextBean implements Serializable{

private Integer context_id;

private String title;

private String context;

private String email;

@Id
@Column(name="context_id")
@GeneratedValue(strategy=GenerationType.AUTO)
public Integer getContext_id() {
return context_id;
}

public void setContext_id(Integer context_id) {
this.context_id = context_id;
}

@Column(name="title",length=50,nullable=false)
public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

@Column(name="context",length=200,nullable=false)
public String getContext() {
return context;
}

public void setContext(String context) {
this.context = context;
}

@Column(name="email",length=30,nullable=true)
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}

我們看上面的程式碼,首先我們會發現,和以往我們看到的一般的Java Bean並沒有太大的區別,都是一些屬性,然後set,get方法。當然了,我們仔細觀察,又看出了一些不同。這裡面新增了很多的Java Annotation的內容,就是Java註解的內容。下面,我們來一一看一下這些個Java註解。
@Entity:表示這是一個實體Bean。
@Table(name="context"):對應資料庫中的那一張表。
@Id:主健
@Column(name="context_id"):這一列在資料庫中對應的是那一列,還有其他的一些引數可以設定。
@GeneratedValue(strategy=GenerationType.AUTO):自動增長
@Column(name="email",length=30,nullable=true):對應資料庫中的email列,長度50,可以為空。
當然了,我們還需要實現Serializable,實現序列化。
好了,基本的出現的Java註解都已經講完了,並且,我們的Java註解一定要寫在get方法的上面,這是由相應的Java Annotation所規定的。
好的,實體Bean已經開發完了,那麼我們就應該寫一些業務方法了。所以我們先建立介面,程式碼給出來。
package org.adam.service;

import java.util.List;

import javax.ejb.Remote;

import org.adam.bean.UserBean;

@Remote
public interface UserService {

public void regist(UserBean user);

public void delete(Integer user_id);

public void update(UserBean user);

public UserBean getUser(Integer user_id);

public List getAllUser();
}

package org.adam.service;

import java.util.List;

import javax.ejb.Remote;

import org.adam.bean.ContextBean;

@Remote
public interface ContextService {

public void say(ContextBean con);

public void deletecontext(Integer context_id);

public void updatecontext(ContextBean con);

public ContextBean getSomething(Integer context_id);

public List getAllContext();
}

這是兩個很簡單的介面,沒有什麼好說的了,唯一稍有不同的在interface的上面有@Remote一句,這是表示一個遠端介面的意思。
好的,既然已經寫完了介面,那麼我們自然就要實現它,所以我們建立了兩個類實現了這兩個介面,先把程式碼給出來。

package org.adam.service.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.adam.bean.ContextBean;

import org.adam.service.ContextService;
@Stateless
public class ContextServiceBean implements ContextService{

@PersistenceContext(unitName="ejbdb")
EntityManager em;
public void deletecontext(Integer context_id) {
em.remove(em.find(ContextBean.class, context_id));
}

public List getAllContext() {
return em.createQuery("select c from ContextBean c").getResultList();
}

public ContextBean getSomething(Integer context_id) {
return em.find(ContextBean.class, context_id);
}

public void say(ContextBean con) {
em.persist(con);
}

public void updatecontext(ContextBean con) {
em.merge(con);
}

}

package org.adam.service.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.adam.bean.UserBean;
import org.adam.service.UserService;

@Stateless
public class UserServiceBean implements UserService{

@PersistenceContext(unitName="ejbdb")
EntityManager em;
public void delete(Integer user_id) {
em.remove(em.find(UserBean.class, user_id));
}

public List getAllUser() {
return em.createQuery("select u from UserBean u").getResultList();
}

public UserBean getUser(Integer user_id) {
return em.find(UserBean.class, user_id);
}

public void regist(UserBean user) {
em.persist(user);
}

public void update(UserBean user) {
em.merge(user);
}

}

這兩個類的結構和程式碼基本上都是一樣的,主要的不同點就是在與操縱的是不同的表,但是這不要緊,我們明白了一個,自然也就明白了另外一個。很簡單。
@Stateless:表示這是一個無狀態的會話Bean。
@PersistenceContext(unitName="ejbdb") :記得我們在最前面的那個xml檔案嗎,裡面有一項的內容就是ejbdb。在這裡用到了。
剩下的就沒有什麼特殊的了,就是5個很平常的Java程式碼。這樣子其實就完成了會話Bean的開發。

好了,絕大部分的內容都完成了,為了驗證我們的程式是否正確,我們使用了JUnit。實際上我也不很會這部分內容,但是簡單的使用還是可以的。我在前面的教程中也講到了如何使用,以及使用步驟,需要注意的地方等等。好了,把測試的程式碼給出來。
package org.adam.unittest;

import static org.junit.Assert.*;

import java.util.List;
import java.util.Properties;

import javax.naming.InitialContext;
import org.adam.bean.UserBean;
import org.adam.service.UserService;

import org.junit.BeforeClass;
import org.junit.Test;

public class UserServiceTest {

private static UserService user;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
InitialContext ctx=new InitialContext(props);
user=(UserService)ctx.lookup("UserServiceBean/remote");
}

@Test
public void testDelete() {
user.delete(2);
}

@Test
public void testGetAllUser() {
List list=user.getAllUser();
for(int i=0;i System.out.println(list.get(i).getUsername());
}
}

@Test
public void testGetUser() {
UserBean u=user.getUser(1);
System.out.println(u.getUsername()+" "+u.getPassword());
}

@Test
public void testRegist() {
UserBean u=new UserBean();
u.setUsername("Adam");
u.setPassword("43046721");
user.regist(u);
}

@Test
public void testUpdate() {
UserBean u=new UserBean();
u.setUser_id(1);
u.setUsername("張弘");
u.setPassword("123456");
user.update(u);
}

}

package org.adam.unittest;

import static org.junit.Assert.*;

import java.util.List;
import java.util.Properties;

import javax.naming.InitialContext;

import org.adam.bean.ContextBean;
import org.adam.service.ContextService;
import org.adam.service.UserService;
import org.junit.BeforeClass;
import org.junit.Test;

public class ContextServiceTest {

private static ContextService context;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
Properties props = new Properties();
props.setProperty("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
props.setProperty("java.naming.provider.url", "jnp://localhost:1099");
InitialContext ctx=new InitialContext(props);
context=(ContextService)ctx.lookup("ContextServiceBean/remote");
}

@Test
public void testSay() {
ContextBean c=new ContextBean();
c.setTitle("hello");
c.setContext("helloworld");
c.setEmail("xyx@163.com");
context.say(c);
}

@Test
public void testDeletecontext() {
context.deletecontext(1);
}

@Test
public void testUpdatecontext() {
ContextBean c=new ContextBean();
c.setContext_id(1);
c.setTitle("welcome");
c.setContext("helloworld");
c.setEmail("xyx@163.com");
context.updatecontext(c);
}

@Test
public void testGetSomething() {
ContextBean c=context.getSomething(1);
System.out.println(c.getTitle()+" "+c.getContext());
}

@Test
@SuppressWarnings("unchecked")
public void testGetAllContext() {
List list=context.getAllContext();
for(int i=0;i System.out.println(list.get(i).getTitle()+" "+list.get(i).getContext());
}
}

}

經過驗證,全部成功。那麼,我們使用EJB3.0對於資料庫的增刪改查的功能就全部完成了,我們現在要做的,就是把這個EJB Project打成一個jar包,使用Eclipse自帶的打包工具就可以,直接生成了一個jar包,拿來就可以使用了。
我們建立一個Web專案,把jar匯入,建立頁面和servlet就可以了,bean基本上就用不到了。
這個web專案我就寫了一點點,沒有什麼難度了,大家參考一下吧!
謝謝!

更多詳情

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29212814/viewspace-1131344/,如需轉載,請註明出處,否則將追究法律責任。

相關文章