??? 最近想想,成為Javaeye的會員已經有年頭了,但是沒有在上面發表過一篇文章,感覺慚愧,做了這么多年的程序,一直沒有把自己的一些經驗和想法和大家一起分享。
???? 我在一家中小軟件企業里面負責公司的技術,腦子里面成天想的是如何建立一套符合自己公司業務的技術框架,雖然公司小,但是項目不小,我們在為一家煤炭企業做ERP,系統的部署環境比較復雜,數據庫是集群,web中間件也要集群,此時普通的技術方案和輕量級框架就不能滿足了,我就想到了EJB3.0
,我在網上找到了一篇講述兩者結合的文章,但是寫的不是很清楚,給出的代碼,我進行了加工和調整,才跑起來,不多說了,看看下面的代碼:
代碼1:
代碼2:
??? 此時我的ejb代碼已經完成,我是用M2來管理我的工程,進行打包編譯,不需要產生客戶端程序。我用的測試服務器是Jboss4.2.3的版本,下來我們要發布我們的ejb了,此時先要編寫oracle-ds.xml:
然后我們就要編寫JAP相應的持久化文件persistence.xml名稱應該是不能變動的,我沒有測試過,但是文檔描述文件名稱是不能變的
??? 此時,相應的persistence.xml文件一定要放在已經打好包的ejb里面,在打好的ejb包里,根目錄下有META-INF目錄,放在里面就可以了,今天先寫到這里
???? 我在一家中小軟件企業里面負責公司的技術,腦子里面成天想的是如何建立一套符合自己公司業務的技術框架,雖然公司小,但是項目不小,我們在為一家煤炭企業做ERP,系統的部署環境比較復雜,數據庫是集群,web中間件也要集群,此時普通的技術方案和輕量級框架就不能滿足了,我就想到了EJB3.0

代碼1:
package com.ejb3.service; import com.ejb3.domain.Customer; import java.util.Collection; import javax.ejb.Stateless; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; import javax.jws.WebMethod; @Stateless @Remote( { CustomerService.class }) public class CustomerServiceImpl implements CustomerService { //unitName是指你數據庫的用戶名,我用的是oracle10g,如果是別的數據庫就是數據庫名稱 @PersistenceContext(unitName="ERP") private EntityManager manager; public Customer create(Customer info) { this.manager.persist(info); return info; } public Customer update(Customer info) { return this.manager.merge(info); } public void remove(Long customerId) { this.manager.remove(this.manager.getReference(Customer.class, customerId)); } public Collection<Customer> findAll() { Query query = this.manager.createQuery("SELECT c FROM Customer c"); return query.getResultList(); } public Customer[] findAllAsArray() { Collection<Customer> collection = findAll(); return (Customer[]) collection.toArray(new Customer[collection.size()]); } public Customer findByPrimaryKey(Long customerId) { return (Customer) this.manager.find(Customer.class, customerId); }
代碼2:
package com.ejb3.service; import com.ejb.domain.Customer; import java.util.Collection; import javax.ejb.Local; import javax.ejb.Remote; public interface CustomerService { Customer create(Customer info); Customer update(Customer info); void remove(Long customerId); Collection<Customer> findAll(); Customer[] findAllAsArray(); Customer findByPrimaryKey(Long customerId); }
??? 此時我的ejb代碼已經完成,我是用M2來管理我的工程,進行打包編譯,不需要產生客戶端程序。我用的測試服務器是Jboss4.2.3的版本,下來我們要發布我們的ejb了,此時先要編寫oracle-ds.xml:
<?xml version="1.0" encoding="UTF-8"?> <datasources> <local-tx-datasource> <jndi-name>OracleDS</jndi-name> <connection-url>jdbc:oracle:thin:@192.168.1.222:1521:oracle</connection-url> <driver-class>oracle.jdbc.driver.OracleDriver</driver-class> <user-name>ERP</user-name> <password>ERP</password> <SetBigStringTryClob>true</SetBigStringTryClob> <exception-sorter-class-name> org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name> <metadata> <type-mapping>Oracle9i</type-mapping> </metadata> </local-tx-datasource> </datasources>
然后我們就要編寫JAP相應的持久化文件persistence.xml名稱應該是不能變動的,我沒有測試過,但是文檔描述文件名稱是不能變的
<?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="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"> <persistence-unit name="ERP" transaction-type="JTA"> <jta-data-source>java:/OracleDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence>
??? 此時,相應的persistence.xml文件一定要放在已經打好包的ejb里面,在打好的ejb包里,根目錄下有META-INF目錄,放在里面就可以了,今天先寫到這里
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
