使用会话Bean+实体bean+数据源

下面接着总结Ejb使用会话Bean+实体bean+数据源开发流程:

先新建一个Java项目导入\jboss-4.2.3.GA\client

第一步:配置数据源
在\jboss-4.2.3.GA\docs\examples\jca目录下mssql-ds.xml(数据源配置文件)

配置内容如下:







liyongMySql
jdbc:mysql://localhost:3306/entitydatabse?useUnicode=true&characterEncoding=UTF-8
com.mysql.jdbc.Driver
root
liyong
3
100
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter






mySQL



然后将这个数据源配置文件copy到jboss-4.2.3.GA\server\default\deploy进行发布

第二步:
在src目录下新建META-INF再新建一个persistence.xml内容如下:

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">



java:liyongMySql










第三步:编写实体bean Persion.java
package com.liyong.bean;

import java.io.Serializable;

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

@Entity
public class Persion implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
private Integer id;
private String name;

public Persion(){}
public Persion(String name) {
this.name=name;
}
@Id @GeneratedValue//默认为auto
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Column(length=20)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Persion other = (Persion) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}

}

第四步:编写一个会话bean PersionService.java和接口IPersionService.java

接口:
public interface IPersionService {
public void save(Persion persion);
public Persion getPersion(Integer persionid);
public List getAllPersions();
public void update(Persion persion);
public void delete(Persion persion);

}
实现类:

package com.liyong.service;

import java.util.List;

import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import com.liyong.bean.Persion;

@Stateless
@Remote(IPersionService.class)

public class PersionService implements IPersionService{

@PersistenceContext(unitName="person") EntityManager manager;

public void delete(Persion persion) {
manager.remove(persion);
}

@SuppressWarnings("unchecked")
public List getAllPersions() {

return manager.createQuery("select o from Persion o").getResultList();
}

public Persion getPersion(Integer persionid) {

return manager.find(Persion.class, persionid);
}

public void save(Persion persion) {
manager.persist(persion);

}

public void update(Persion persion) {

manager.merge(persion);

}

}


第五步:编写一个单元测试PersionTest.java

package junit.test;


import java.util.List;

import javax.naming.InitialContext;
import org.junit.BeforeClass;
import org.junit.Test;

import com.liyong.bean.Persion;
import com.liyong.service.IPersionService;

public class PersionTest {

private static IPersionService persionService;

@BeforeClass
public static void setUpBeforeClass() throws Exception {
try {
InitialContext ctx = new InitialContext();
persionService = (IPersionService) ctx.lookup("PersionService/remote");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

@Test
public void testSave(){
try {
Persion persion=new Persion("ly");
persionService.save(persion);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testGetPersoin(){

System.out.println(persionService.getPersion(1).getName());
}
@Test
public void testUpdate(){
Persion persion=persionService.getPersion(1);
persion.setName("xxxx");
persionService.update(persion);
}
@Test
public void testGetAllPersions(){
List persions=persionService.getAllPersions();
for(Persion persion:persions){
System.out.println(persion.getName());
}
}
}

对了还需要一个jndi.properties(这个可以参考前面HelloWord)

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.provider.url=localhost\:1099

我们使用build.xml来打包编译

































































你可能感兴趣的:(EJB学习笔记)