部署Java在服务器端的EJB组件的方法
什么是EJB? create database student; //创建数据库 student 创建表: create table student( //创建表student 和 数据库同名 `id` integer(11) not null,`name` varchar2(20) default null,primary key (`id`) )ENGINE=InnoDB DEFAULT CHARSET=latin1 插入一条数据:
insert into student values(1,'easynoder'); commit; 给本地的root用户(生产环境请勿这样做)指定访问权限。
grant all privileges on *.* to root@localhost indentified by "1234" 通过以上步骤,需要的数据库表已建立好。可通过root用户访问所有数据。 2、编写实体Bean、用户操作接口和会话Bean 建立EJB工程,名为MyEJBProject。该工程META-INFO目录下包含一个文件persistence.xml文件。该文件用来配置数据源,稍后进行配置。 接着建立实体Bean @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; } } 建立操作接口: public interface BaSEOperation { public List<?> findAll(); } 该接口只有一个方法,获取所有的学生 @Stateless //这是一个无状态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、数据源配置 <?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 节点属性name的值,就是刚才咱们建立的文件夹的路径。resources表示mysql驱动的路径。意味着,需要将mysql的驱动放在main目录下。即main目录下包含两个文件,module.xml和数据库驱动文件。 <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的路径。 < 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服务。 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 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(); } } 运行这段代码,可以成功的查询到数据库的数据啦。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |