Sunday, February 7, 2016

Liferay Service Builder Basic Concept

Environment :
Liferay 6.2
Liferay Tomcat 7

Liferay Service Builder is a tool built by liferay that allows developers to define entities. It  generates necessary model , persistence and  service layers for our applications. It uses spring and hibernate internally to generate set of  classes, sql scripts and configuration files to interact with database. It takes service.xml file as input and generate the set of classes and interfaces to interact with Database. Using this service layers we can interact with database and can perform CRUD operation( operation like ->Add,update,delete,retrieve). The service builder tool takes xml file (its called service.xml) as an input and generate code based on the input provided in service.xml.

STEPS:
Step1: Create one liferay mvc project of portlet plugin type and one portlet inside that project.

Step 2: Create service.xml file using liferay ide,


While creating service.xml select plugin project for which we are creating service.xml , in this example project : Sampleproject, package path:com.slayer(path contain service related files), Namespace : X, follow below screen. Click finish.

Step 3: Define required entities/tables in service.xml, Created service.xml contain sample entity definition, we will edit/add as per requirement, in this example below is service.xml content.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.2.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_6_2_0.dtd">
<service-builder package-path="com.slayer">
       <author>samsun</author>
       <namespace>X</namespace>

       <entity name="EmployeeManage" local-service="true" remote-service="false">

              <!-- PK fields -->
              <column name="employeeId" type="long" primary="true" />

              <column name="empFirstName" type="String" />
              <column name="empLastName" type="String" />
              <column name="empAge" type="int" />
              <column name="empNumber" type="String" />
       </entity>
</service-builder>

Step 4: Run the service builder: Once we created service.xml and defined entity detail, now we need to  run service builder. We can run the service builder in eclipse from ANT view. We need to click on build-service target from the ANT view.
Click on Sampleproject-portlet in which we have created service.xml and defined entityàLiferayàBuild Service
Follow below screen:


After successful building service, we will get below successful message:

Below screen shows service builder generated service layers file:

After deployment of project we will notice a new table X_EmployeeManage  got automatically created by liferay when our service layer got deployed to the server. The table name is prefixed with the namespace: X , followed by the actual entity name : EmployeeManage.
Complete entity name: X_EmployeeManage
We can optionally disable this auto-prefix  by specifying   auto-namespace-tables=”false” attribute for “<service-builder>”  tag of the service.xml after package path attribute.
Below screen shows entity/table generated in database:

2 comments:

  1. Facile à comprendre avec des captures d'écran et merci pour le partage des étapes... Nous proposons également le développement Liferay Angular... contactez-nous pour obtenir plus de détails .

    ReplyDelete