ejb 3 初探

ejb3.0开发web,ejb作为M(模型层),推荐使用eclipse+myeclipse开发(虽然jbuilderejb结合的更紧密)


1.依然将ejb所用的jar包导入工程


下面的jar文件是必需的:


[jboss安装目录]\client\commons-logging.jar


[jboss安装目录]\client\concurrent.jar


[jboss安装目录]\client\ejb3-persistence.jar


[jboss安装目录]\client\hibernate-annotations.jar


[jboss安装目录]\client\hibernate-client.jar


[jboss安装目录]\client\javassist.jar


[jboss安装目录]\client\jboss-annotations-ejb3.jar


[jboss安装目录]\client\jboss-aop-jdk50-client.jar


[jboss安装目录]\client\jboss-aspect-jdk50-client.jar


[jboss安装目录]\client\jboss-common-client.jar


[jboss安装目录]\client\jboss-ejb3-client.jar


[jboss安装目录]\client\jboss-ejb3x.jar


[jboss安装目录]\client\jboss-j2ee.jar


[jboss安装目录]\client\jboss-remoting.jar


[jboss安装目录]\client\jbosssx-client.jar


[jboss安装目录]\client\jboss-transaction-client.jar


[jboss安装目录]\client\jnp-client.jar


[jboss安装目录]\client\trove.jar


[jboss安装目录]\client\jbossws-client.j


[jboss安装目录]\client\jboss-jaxws.jar


[jboss安装目录]\client\xmlsec.jar


相当的多!


现在写第一stateless session bean(无状态会话bean)


ejb3的标注式声明比较快捷!


2.写业务规范包(接口包)


:


com.sk.DAO //包名


package com.sk.DAO;


Login接口


import com.sk.domain.User;

public interface Login {


public User login(String username,String password);


}


3.写业务实现类(实现相应接口的类)


:


com.sk.DAOImp //包名


写类LoginBean 并实现 Login接口(ejb3约定:实现接口的类的名称一律用接口名+Bean来标识)


package com.sk.DAOImp;

import java.sql.CallableStatement;


import java.sql.Connection;


import java.sql.ResultSet;


import java.sql.SQLException;

import javax.ejb.Remote;


import javax.ejb.Stateless;

import com.sk.DAO.Login;


import com.sk.domain.User;


import com.sk.tools.DBOper;

@Stateless //表示此Bean为无状态Bean(服务端不存储Bean的状态,可节省Server的内存)


@Remote({Login.class}) //表示遥控(@remote)的接口,本地(@local)必须保证与EJB相关业务逻辑操作者与调用者在同一ApplicationServer


public class LoginBean implements Login {

public User login(String username, String password) {


// TODO Auto-generated method stub


try {


Connection conn= DBOper.getConnection();


CallableStatement cs=conn.prepareCall("{call up_users_login(?,?)}");


cs.setString(1, username);


cs.setString(2, password);


ResultSet rs= cs.executeQuery();


while(rs.next()){


User user=new User(rs.getString("username"),rs.getString("password"),rs.getString("description"),rs.getBoolean("isadmin"));


return user;


//User对象必须实现序列化,(用于web传播)


}


} catch (SQLException e) {


// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}
至此,ejb3登陆功能,已经实现
最后可通过以下方法得的想要的对象
public class EJBFactory {
 public static Object getEJB(String jndipath) {
 try {
 Properties props = new Properties();
//
 props.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
 props.setProperty("java.naming.provider.url", "localhost:1099");
 props.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

 InitialContext ctx = new InitialContext(props);
 return ctx.lookup(jndipath);
 } catch (NamingException e) {
 e.printStackTrace();
 }
 return null;
 }
}
调用此方法需要注意:
Login loginAdmin= (Login)EJBFactory.getEJB("LoginBean/remote");

你可能感兴趣的:(sql,bean,Hibernate,jboss,ejb)