IT기술/Liferay Portal

Liferay Service Builder 완전 가이드: 엔터프라이즈급 데이터 레이어 자동 생성 도구

후스파 2025. 7. 21. 07:09
반응형

Liferay Service Builder는 개발자들이 복잡한 데이터베이스 관련 코드를 직접 작성하지 않고도 비즈니스 로직을 구현할 수 있도록 도와주는 강력한 코드 생성 도구입니다. 이 도구는 단일 XML 파일을 기반으로 모델, 퍼시스턴스, 서비스 레이어를 자동으로 생성하는 객체-관계형 매핑(ORM) 도구로 작동합니다.
Service Builder의 핵심 개념은 Model-Driven 코드 생성 방식으로, WEB-INF/service.xml 파일에 정의된 엔티티를 참조하여 필요한 모든 클래스들을 자동으로 생성해주는 것입니다. 이를 통해 개발자는 비즈니스 로직 구현에만 집중할 수 있게 됩니다.


Service Builder의 주요 기능과 장점

자동 생성되는 레이어 구조

Service Builder는 세 가지 핵심 레이어를 자동으로 생성합니다:

Model Layer엔티티 표현프로젝트의 엔티티를 나타내는 객체들을 정의
Persistence Layer데이터 저장/조회데이터베이스로부터 엔티티를 저장하고 검색하는 기능
Service Layer비즈니스 로직API와 비즈니스 로직을 구현할 수 있는 빈 레이어

핵심 장점들

Service Builder를 사용하면 다음과 같은 이점을 얻을 수 있습니다:

  • Liferay와의 완벽한 통합: 플랫폼 네이티브 기능과 seamless 연동
  • 자동 생성되는 로컬 및 원격 서비스: 분산 환경 지원
  • Hibernate와 Spring 설정 자동 구성: 복잡한 설정 작업 불필요
  • 엔티티 캐싱 지원 기능: 성능 최적화 자동 적용
  • 커스텀 SQL 쿼리와 동적 쿼리 지원: 복잡한 데이터 조회 가능
  • 개발 시간 대폭 단축: 반복적인 CRUD 코드 작성 불필요

Service Builder 실습 가이드

1단계: 프로젝트 생성 및 설정

먼저 Liferay 프로젝트를 생성하고 Service Builder를 위한 기본 구조를 설정해야 합니다. Eclipse IDE에서 File → New → Liferay Plugin Project를 선택하여 새 프로젝트를 만들어줍니다.

2단계: service.xml 파일 생성

Service Builder의 핵심은 service.xml 파일입니다. 이 파일에서 데이터 모델과 엔티티를 정의하게 됩니다. 프로젝트를 우클릭하고 New → Liferay Service Builder를 선택하여 service.xml 파일을 생성합니다.

3단계: 엔티티 정의 예시

다음은 간단한 직원 관리 시스템을 위한 service.xml 예시입니다:

<?xml version="1.0" encoding="UTF-8"?>
<service-builder package-path="com.example.service">
    <author>개발자명</author>
    <namespace>employee</namespace>
    
    <entity name="Employee" local-service="true" table="TBL_EMPLOYEE">
        <column name="employeeId" type="long" primary="true" id-type="increment"></column>
        <column name="name" type="String"></column>
        <column name="position" type="String"></column>
        <column name="department" type="String"></column>
        <column name="email" type="String"></column>
        <column name="phoneNumber" type="String"></column>
    </entity>
</service-builder>

4단계: Service Builder 실행

service.xml 파일 작성이 완료되면 Service Builder를 실행해야 합니다. Eclipse에서 프로젝트를 우클릭하고 Liferay → Build Services를 선택하거나, 명령줄에서 다음 명령어를 실행합니다:

# Gradle 사용 시
blade gw buildService

# Maven 사용 시
mvn service-builder:build

생성된 코드 구조 이해하기

자동 생성되는 클래스들

Service Builder가 실행되면 다음과 같은 클래스들이 자동으로 생성됩니다:

  • Entity Implementation (*Impl.java): 엔티티 커스터마이징 담당
  • Local Service Implementation (*LocalServiceImpl.java): 비즈니스 로직과 데이터 액세스 담당
  • Remote Service Implementation (*ServiceImpl.java): 원격 서비스 구현 (옵션)

서비스 레이어 구현

생성된 서비스 레이어에는 기본적인 CRUD 연산을 위한 메서드들이 포함되어 있습니다. 개발자는 *LocalServiceImpl.java 파일에서 추가적인 비즈니스 로직을 구현할 수 있습니다.

public class EmployeeLocalServiceImpl extends EmployeeLocalServiceBaseImpl {

    public Employee addEmployee(String name, String position, String department, 
                               String email, String phoneNumber) throws SystemException {

        long employeeId = counterLocalService.increment(Employee.class.getName());
        Employee employee = employeePersistence.create(employeeId);

        employee.setName(name);
        employee.setPosition(position);
        employee.setDepartment(department);
        employee.setEmail(email);
        employee.setPhoneNumber(phoneNumber);

        return employeePersistence.update(employee);
    }

    public List getEmployeesByDepartment(String department) 
            throws SystemException {
        return employeePersistence.findByDepartment(department);
    }
}

고급 기능 활용하기

Finder 메서드 구현

특정 조건으로 데이터를 검색하려면 Finder 메서드를 정의할 수 있습니다. service.xml에 다음과 같이 추가합니다:

<finder name="Department" return-type="Collection">
    <finder-column name="department"></finder-column>
</finder>

<finder name="PositionAndDepartment" return-type="Collection">
    <finder-column name="position"></finder-column>
    <finder-column name="department"></finder-column>
</finder>

이렇게 하면 findByDepartment()findByPositionAndDepartment() 메서드가 자동으로 생성됩니다.

캐싱 설정

성능 향상을 위해 엔티티 캐싱을 활성화할 수 있습니다. 엔티티 태그에 cache-enabled="true" 속성을 추가하면 됩니다:

<entity name="Employee" local-service="true" table="TBL_EMPLOYEE" cache-enabled="true">
    <!-- 컬럼 정의 -->
</entity>

커스텀 SQL 쿼리

복잡한 쿼리가 필요한 경우 커스텀 SQL을 사용할 수 있습니다. src/main/resources/META-INF/custom-sql/ 디렉토리에 SQL 파일을 생성하고 다음과 같이 구현합니다:

-- custom-sql/default.xml
<sql id="com.example.service.persistence.EmployeeFinder.findByDepartmentAndPosition">
    <![CDATA[
        SELECT * FROM TBL_EMPLOYEE 
        WHERE department = ? AND position = ?
        ORDER BY name ASC
    ]]>
</sql>

실제 개발 환경에서의 활용 팁

개발 환경 구성

Service Builder 개발을 위해서는 적절한 개발 환경 설정이 중요합니다. Liferay Workspace를 생성하고 모듈 기반 개발 방식을 채택하는 것이 좋습니다:

# Liferay Workspace 생성
blade init -v 7.4 my-workspace

# Service Builder 모듈 생성
blade create -t service-builder -p com.example.service employee-service

외부 데이터베이스 연동

기존의 외부 데이터베이스와 연동해야 하는 경우도 있습니다. 이 경우 ext-spring.xml 파일을 통해 데이터 소스를 설정하고, service.xml에서 해당 데이터 소스를 참조하도록 구성할 수 있습니다:

<!-- ext-spring.xml -->
<bean id="customDataSource" class="com.zaxxer.hikari.HikariDataSource">
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/custom_db"/>
    <property name="username" value="dbuser"/>
    <property name="password" value="dbpass"/>
</bean>

성능 최적화 전략

배치 처리 최적화:

public void updateEmployeesBatch(List employees) {
    for (Employee employee : employees) {
        employeeLocalService.updateEmployee(employee);
    }
    // 배치 처리 후 캐시 클리어
    employeePersistence.clearCache();
}

인덱스 최적화:

<entity name="Employee" local-service="true" table="TBL_EMPLOYEE">
    <!-- 컬럼 정의 -->
    
    <!-- 인덱스 정의 -->
    <index>
        <index-column name="department"/>
        <index-column name="position"/>
    </index>
</entity>

Service Builder vs 다른 ORM 도구 비교

학습 곡선낮음높음중간
Liferay 통합완벽수동 설정 필요수동 설정 필요
코드 생성자동수동수동
캐싱자동 지원수동 설정수동 설정
동적 쿼리지원지원제한적
성능최적화됨높음높음

마무리

Liferay Service Builder는 복잡한 데이터 액세스 코드 작성 없이도 강력한 비즈니스 로직을 구현할 수 있게 해주는 혁신적인 도구입니다. XML 기반의 간단한 설정만으로 완성도 높은 서비스 레이어를 자동 생성하며, 개발자는 핵심 비즈니스 로직 구현에만 집중할 수 있습니다.
특히 엔터프라이즈급 애플리케이션 개발에서 Service Builder의 진가가 발휘됩니다. 자동 생성되는 캐싱 기능, 동적 쿼리 지원, 그리고 Liferay 플랫폼과의 완벽한 통합은 대규모 시스템 개발에 있어 큰 장점이 됩니다.
앞으로는 Liferay Objects와 같은 로우코드/노코드 솔루션도 등장하고 있지만, 복잡한 비즈니스 요구사항을 만족시키려면 여전히 Service Builder가 최적의 선택입니다. 체계적인 학습과 실습을 통해 이 강력한 도구를 마스터해보시기 바랍니다.

반응형