Monday, October 12, 2015

Service Builder in Liferay


Service builder in Liferay provide a easy way to created services. We just need to provide the table structure in a xml file and all the CRUD methods generate automatically. For custom methods you may create Custom Sql  and Finder MethodsToday we create a basic services with just single table and see how to enter data in the table.

So lets start this step by step:-

Step 1:-Create Liferay Project
Inside eclipse IDE Click on File->New->Liferay plugin project and give the project name as service_demo and click finish.





Step 2:-Create MVC Portlet inside project
Right Click on your project ie service_demo and click on New than click on Liferay Portlet. Provide class and package name and select MVCPortlet and click finish.

Note:-You can check the snapshots in my  blog on Portlet Configuration Page in Liferay.

Step 3:-Create Service.xml
Right Click on your project ie service_demo and click on New than click on Liferay Service Builder. Provide the package path, namespaces and author



and Click Finish. This will create a service.xml inside WEB-INF.





Step 4:-Create Entity in Service.xml
Open Service.xml and Click overview and then click on entity.By using the "+" button you may enter as many entities as you want each entity represent one table in database.



Here we use Employee as name of the entity so all services is created with Employee as prefix. 

But we want the table name in database is employee_data .So Click on Employee on Left and fill Table as employee_data



So our table is created with name employee_data and our entity name is Employee.





Step 5:-Create columns in table
Click on columns on Left and by using the "+" button add columns



Here we check eid Primary checkbox so that eid become primary key for the table.

Now we want the primary key is auto incremented. So select eid from left, this will open property page for eid enter Id type to increment .



We have other options for Id type like sequence, identity, class but i am using Mysql so use increment.

Now all the columns in database will created with eid,name,phone ,age but i want that salary is created in database as EMP_SALARY so from Left click on salary and change Db name as:-



When you click on source you will see the service.xml as:-


We can directly create service.xml without using GUI. So you can directly copy paste service.xml and skip step 4 and 5.

Step 6:-Build Services
Click on overview in service.xml and click on Build service icon as:-



After build service is completed you can check all class are created in src folder.


Step 7:-Call the services 
Open your controller of the portlet ie java class and paste this:-



Explanation:-
Here in doView method we use CounterLocalServiceUtil to generate primary key ie eid because we take eid as autoincremented.

Step 8:-Check Output
Open your database you can see table as:-




Check data for that table




Project Structure



Hope this will Help....

You can Download Source code from  Service Builder in Liferay.

In the next blog Service Builder in Detail we will see each and every tag of service.xml in detail.

Related Post:-

No comments:

Post a Comment

Total Pageviews

Number Of Unique Visitor

Free counters!