We can create service builder module to perform various CRUD operation in liferay. This post includes creating and building service module in liferay 7.
In service module we just need to specify table structure in service.xml file and all methods for CRUD operation will generate automatically.
Today we will create basic service module with only one table i.e one entity using Liferay IDE
Goto File→New→Liferay Module Project
Give ProjectName→product-registration-service
Select ServiceBuilder as project template and Click Next
Give Package Name →com.liferay.product.service and click finish
This will create product-registration-service module as shown here.
product-registration-service will include
- product-registration-service-api
- product-registration-service-service
Now open service.xml file located in product-registration-service-service and click on Overview.
For this tutorial change namespace to pr.
Click on Entities,here you can add any number of entities you want using + symbol shown in figure,each entity will be store as a separate table in you database.
Now add one Entity→ Product.
Click on Product here their are various properties available which you can set.
Enter product_details in table property and click on local service as shown below
Click on columns here you can add various columns for your entity with datatype.
Now Add 4 fields
- productId
- companyId
- productName
- productPrice
Set primary =true and id type ="increment" for productId filed
Now click on source this will generate following code.
<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 7.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_7_0_0.dtd">
<service-builder package-path="com.liferay.product.service">
<namespace>pr</namespace>
<entity local-service="true" name="Product" table="product_details" remote-service="false" uuid="true">
<column name="productId" primary="true" type="long" id-type="increment"/>
<column name="companyId" type="long" />
<column name="productName" type="String" />
<column name="productPrice" type="long" />
</entity>
</service-builder>
Now its all done, just we require to build service and deploy our service module and this will create product_details table in our mysql database and it will also generate required CRUD methods for Product Entity.
For Building Service and deploying service module we will be using gradle task
Note we need to build and deploy both
- product-registration-service-api
- product-registration-service-service
First go to Gradle Task→product-registration-service-service and perform
- build service
- build
- deploy
Then go to Gradle Task→product-registration-service-api and perform
- build
- deploy
Note if you have not set liferay.workspace.home.dir and liferay.workspace.modules.default.repository.enabled in your gradle.properties visit my previous blog Deploying liferay module to tomcat
Congratulations you have successfully created service module with entity Product ,now check your module it has generated lots of packages and classes.Also check your database here you will find product_details is generated.
Final Project Structure
Also we can create various finder methods in service builder.My future post on service builder will include creating finder and writing custom sql in liferay 7.
Also please refer by below post to learn how to handle success and error message in liferay DXP.
thank for helping us..but we are little struggling for database connections..can you please update here
ReplyDelete