Vous êtes sur la page 1sur 8

Getting started XFire Web Service With Spring and Hibernate Using netbeans and tomcat.

Content : 1. Creating Database at MySQL. 2. Creating Netbeans Project 3. Generated Entity Object From Database 4. Insert Depedency Package 5. Creating Hibernate Dao (interface Dao and implementation). 6. Creating Spring XML Configuration for Dao 7. Creating Service For transaction (Interface and implementation) 8. Creating Spring XML Configuration for service 9. Creating Test Unit For Service 10. Creating Web service (Interface And Implementation) with util class. 11. Creating Spring XML Configuration for Web Service CREATING DATABASE AT MYSQL For the first step please create database and it tables. Like SQL Script below :
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0; SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL'; CREATE SCHEMA IF NOT EXISTS `TutorialCSL` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ; USE `TutorialCSL`; DROP TABLE IF EXISTS `TutorialCSL`.`MasterSex` ; CREATE TABLE IF NOT EXISTS `TutorialCSL`.`MasterSex` ( `SexID` TINYINT NOT NULL AUTO_INCREMENT , `Description` VARCHAR(50) NOT NULL , PRIMARY KEY (`SexID`) , UNIQUE INDEX `MastrerSexDescriptionUnique` (`Description` ASC) ) ENGINE = InnoDB; DROP TABLE IF EXISTS `TutorialCSL`.`MasterEmployee` ; CREATE TABLE IF NOT EXISTS `TutorialCSL`.`MasterEmployee` ( `EmployeeID` BIGINT NOT NULL , `NickName` VARCHAR(45) NOT NULL , `FullName` VARCHAR(255) NOT NULL , `Email` VARCHAR(45) NOT NULL , `Address` TEXT NOT NULL , `Phone` VARCHAR(15) NOT NULL , `SexID` TINYINT NOT NULL , PRIMARY KEY (`EmployeeID`) , INDEX `MasterEmployeeMasterSexID` (`SexID` ASC) , CONSTRAINT `MasterEmployeeMasterSexID` FOREIGN KEY (`SexID` ) REFERENCES `TutorialCSL`.`MasterSex` (`SexID` ) ON DELETE RESTRICT ON UPDATE RESTRICT) ENGINE = InnoDB;

DROP TABLE IF EXISTS `TutorialCSL`.`ModuleDepartmentMember` ; CREATE TABLE IF NOT EXISTS `TutorialCSL`.`ModuleDepartmentMember` ( `ModuleDepartmentMemberID` BIGINT NOT NULL AUTO_INCREMENT , `EmployeeID` BIGINT NOT NULL , `DepartmentID` INT NOT NULL , PRIMARY KEY (`ModuleDepartmentMemberID`) , INDEX `ModuleDepartmentMasterEmployeeID` (`EmployeeID` ASC) , UNIQUE INDEX `ModulDepartmentEmployeeID` (`EmployeeID` ASC) , INDEX `ModuleDepartmentMasterDepartment` (`DepartmentID` ASC) , CONSTRAINT `ModuleDepartmentMasterEmployeeID` FOREIGN KEY (`EmployeeID` ) REFERENCES `TutorialCSL`.`MasterEmployee` (`EmployeeID` ) ON DELETE CASCADE ON UPDATE RESTRICT, CONSTRAINT `ModuleDepartmentMasterDepartment` FOREIGN KEY (`DepartmentID` ) REFERENCES `TutorialCSL`.`MasterDepartment` (`DepartmentID` ) ON DELETE CASCADE ON UPDATE RESTRICT) ENGINE = InnoDB; SET SQL_MODE=@OLD_SQL_MODE; SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS; SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

CREATING NETBEANS PROJECT 1. Choose File > New Project (Ctrl-Shift-N). Select Web Application from the Java Web category. 2. Name the project Tutorial. Click Next. 3. Select Tomcat As Server, select Java EE 5 As Java EE Version, And Set Context Path /Tutorial 4. Click Finish You can see the result below :

GENERATED ENTITY OBJECT FROM DATABASE To Generated Entity Object from database is very easy. As easy as when we make a netbeans project. 1. Right-click the project node icon Tutorial which had pictures of the earth > choose New > Other > select entity classes from database from persistence category 2. Choose new database connection as Database Connection. 3. Select Direct URL Entry as data input mode, Select MySQL As Driver Name, insert your mysql username and password, and set jdbc:mysql://localhost:3306/tutorialcsl at JDBC URL, Click OK

4. Click Add All >> and then click next. 5. Change Class name from table Class Names column Class Name. Like The Picture Below :

6. Set the package com.ilhami.tutorial.model and than click next.

7. Choose default for association fetch, and than choose java.util.List as Collection type. Finally check Fully Qualified database Table names, and check Attribute for regenerating Tables. See the picture below :

8. Click Finish. See the result Below.

INSERT DEPENDENCY PACKAGE The Following dependency that use to built this project are : 1. 2. 3. 4. 5. 6. 7. 8. Activation.jar antlr-2.7.6.jar asm.jar asm-attrs.jar aspectjrt.jar aspectjweaver.jar cglib-2.1.3.jar commons-collections-2.1.1.jar

9. commons-logging.jar 10. commons-logging-1.1.jar 11. dom4j-1.6.1.jar 12. ehcache-1.2.3.jar 13. ejb3-persistence.jar 14. emma.jar 15. emma_ant.jar 16. hibernate3.jar 17. hibernate-annotations.jar 18. hibernate-commons-annotations.jar 19. hibernate-entitymanager.jar 20. hibernate-tools.jar 21. javassist.jar 22. jdbc2_0-stdext.jar 23. jdom.jar 24. jta.jar 25. log4j-1.2.15.jar 26. mysql-connector-java-5.1.6-bin.jar 27. qname.jar 28. sax2.jar 29. spring.jar 30. spring-aop.jar 31. spring-beans.jar 32. spring-context.jar 33. spring-context-support.jar 34. spring-core.jar 35. spring-jdbc.jar 36. spring-orm.jar 37. spring-tx.jar 38. spring-web.jar 39. spring-webmvc.jar 40. stax-api-1.0.1.jar 41. staxtest_1.1.2.jar 42. wsdl4j.jar 43. wstx-asl-3.2.9.jar 44. xbean-spring-2.6.jar 45. xfire-all-1.2.6.jar 46. xfire-jsr181-api-1.0-M1.jar To insert Dependency into project Library. Right-click the project node icon Tutorial which had pictures of the earth > choose Properties.

Click add jar from libraries category. Browse all category dependency. And Click OK.

CREATING HIBERNATE DAO (INTERFACE DAO AND IMPLEMENTATION). Before Creating Dao Interface for each entity, to make easier and reduce time developing this this application, we should make helping interface for dao interface that called GenericDao interface. Create new interface : 1. Right-click the project node icon Tutorial which had pictures of the earth > choose New > Other > select Java interface from java category. 2. Set GenericDao for class name. 3. Set com.ilhami.tutorial.dao for package 4. Click finish. View the souce code of GenericDao below.

import java.io.Serializable; import java.util.List; public interface GenericDao<T, ID extends Serializable> { public List<T> findAllWhereAttributeIsNull(String[] attributeName); public List<T> findAll(); public List<T> findAllLimit(int startIndex, int limit); public List<T> findByAttributeNameWhereLike(String attributeName, String value, int startIndex, int limit); public List<T> findByAttributeNameEquivalent(String[] attributeName, Object [] value); public List<T> findByAttributeNameEquivalentLimit(String[] attributeName, Object [] value, int startIndex, int limit); public List<T> findByAttributeNameEquivalentOr(String[] attributeName, Object [] value); public List<T> findByAttributeNameEquivalentOrLimit(String[] attributeName, Object [] value , int startIndex, int limit); public List<T> findByAttributeNameBetween(String attributeName, Object firstValue, Object secondValue); public List<T> findByAttributeNameBetweenAndOtherAttributeEquivalent(String attributeName, Object firstValue, Object secondValue, String[] attributeOther, Object [] valueOther); public List<T> findByAttributeNameBetweenAndOtherAttributeLike(String attributeName, Object firstValue, Object secondValue, String attributeOther, String valueOther); public List<T> findByAttributeNameBetweenLimit(String attributeName, Object firstValue, Object secondValue,int startIndex, int limit); public List<T> findAllWhereNullAttributeOrAttributeEquivalent(String AttributeName, Object value); public List<T> findAllWhereNullAttributeOrAttributeNotEquivalent(String AttributeName, Object value); public List<T> findAllWhereAttributeNameEquivalendAndOtherAttributeLike(String[] attributeEquivalentName, Object[] valueEquivalent, String attributeLike, String valueLike, int startIndex, int limit);

public Long count(); public T findByID(final ID id); public void save(final T domain) throws RuntimeException; public void save(final List<T> domainList) throws RuntimeException; public void update(final T domain) throws RuntimeException; public void update(final List<T> domainList) throws RuntimeException; public void delete(final T domain) throws RuntimeException; public void delete(final List<T> domainList) throws RuntimeException; public List<T> findByAttributeNameEquivalent_test(String[] attributeName, Object [] value); }

Code Form : GenericDao interface

Vous aimerez peut-être aussi