`

liferay持久化代码的自动生成

阅读更多
liferay对数据库的操作他有自已的一套写法,我们可以用service.xml来自动生成这些代码, 这样就省了不少力气.在ext-impl包的classes下面service.xml写上数据库表字段如下:
<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 4.3.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_4_3_0.dtd">

<service-builder root-dir=".." package-path="com.pccw.portlet">

	<!-- ecp.ecpErp.portlet-->
	
	<portlet name="ecpErp" short-name="ecpErp" />
	
	<entity name="ecpErpEmployee" local-service="true" remote-service="true" table="Ecp_Erp_Employee"><!-- 员工表  -->
	
		<!-- PK fields -->
		
		<column name="ecpEmployeeId" type="Long" primary="true" db-name="ecpEmployeeId"/>
		
	    <!-- Audit fields -->
        <!-- column name这个值是要和表中的字段名一样,会自动全变成大写.  -->
	    <column name="employeeNumber" type="String"/>	
	    <column name="employeeName" type="String"/> 
	    <column name="companyCode" type="String"/>
	    <column name="departmentCode" type="String"/>
	    <column name="email" type="String"/>
	    <column name="tel" type="String"/>
	    <column name="supplierId" type="Long"/>
	    <column name="bank" type="String"/>
	    <column name="account" type="String"/>
	    <column name="orgId" type="String"/>
	    <column name="userId" type="Long"/> 
	    <column name="creater" type="String"/>
        <column name="creatTime" type="Long"/>
	    <column name="updater" type="String"/>
        <column name="updateTime" type="Long"/>
	    <column name="attribute1" type="String"/>		
	    <column name="attribute2" type="String"/>	
	    <column name="attribute3" type="String"/>	
	    <column name="attribute4" type="String"/>	
	    <column name="attribute5" type="String"/>		
			
		<!-- Order -->

		<order by="asc">
			<order-column name="employeeNumber" />
		</order>

		<!-- Finder methods -->	
		
		<finder name="userId" return-type="ecpErpEmployee" >
			<finder-column name="userId"/>	
		</finder>
		<!--增加的查找,删除的方法,默认情况下liferay会自动生成一些常用的方法,如:主键查找(findById(long id)),查找所有(findAll()),删除(delete***(long id)).你可以根据自已的需要来增加一些方法,包括多个字段的组合查找. -->
		<finder name="employeeName" return-type="Collection" >
			<finder-column name="employeeName"/>	
		</finder>

	</entity>	


这个配置文件写好了以后,build-service这个文件,然后你就会发现在你的ext-service包中com.test.portlet.ecperp.model包下面生成三个java文件:
ecpErpCostCenter.java:
public interface ecpErpCostCenter extends ecpErpCostCenterModel {
}


ecpErpCostCenterModel.java:

import com.liferay.portal.model.BaseModel;


public interface ecpErpCostCenterModel extends BaseModel {
    public Long getPrimaryKey();

    public void setPrimaryKey(Long pk);

    public Long getCostCenterId();

    public void setCostCenterId(Long costCenterId);

    public String getErpCodeId();

    public void setErpCodeId(String erpCodeId);

    public String getCostCenterCode();

    public void setCostCenterCode(String costCenterCode);

    public String getCostCenterName();

    public void setCostCenterName(String costCenterName);

    public String getDescription();

    public void setDescription(String description);

    public String getCreater();

    public void setCreater(String creater);

    public Long getCreatTime();

    public void setCreatTime(Long creatTime);

    public String getUpdater();

    public void setUpdater(String updater);

    public Long getUpdateTime();

    public void setUpdateTime(Long updateTime);

    public String getAttribute1();

    public void setAttribute1(String attribute1);

    public String getAttribute2();

    public void setAttribute2(String attribute2);

    public String getAttribute3();

    public void setAttribute3(String attribute3);

    public String getAttribute4();

    public void setAttribute4(String attribute4);

    public String getAttribute5();

    public void setAttribute5(String attribute5);
}


ecpErpCostCenterSoap.java:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
public class ecpErpCostCenterSoap implements Serializable {
    private Long _costCenterId;
    private String _erpCodeId;
    private String _costCenterCode;
    private String _costCenterName;
    private String _description;
    private String _creater;
    private Long _creatTime;
    private String _updater;
    private Long _updateTime;
    private String _attribute1;
    private String _attribute2;
    private String _attribute3;
    private String _attribute4;
    private String _attribute5;

    public ecpErpCostCenterSoap() {
    }

    public static ecpErpCostCenterSoap toSoapModel(ecpErpCostCenter model) {
        ecpErpCostCenterSoap soapModel = new ecpErpCostCenterSoap();
        soapModel.setCostCenterId(model.getCostCenterId());
        soapModel.setErpCodeId(model.getErpCodeId());
        soapModel.setCostCenterCode(model.getCostCenterCode());
        soapModel.setCostCenterName(model.getCostCenterName());
        soapModel.setDescription(model.getDescription());
        soapModel.setCreater(model.getCreater());
        soapModel.setCreatTime(model.getCreatTime());
        soapModel.setUpdater(model.getUpdater());
        soapModel.setUpdateTime(model.getUpdateTime());
        soapModel.setAttribute1(model.getAttribute1());
        soapModel.setAttribute2(model.getAttribute2());
        soapModel.setAttribute3(model.getAttribute3());
        soapModel.setAttribute4(model.getAttribute4());
        soapModel.setAttribute5(model.getAttribute5());

        return soapModel;
    }

    public static ecpErpCostCenterSoap[] toSoapModels(List models) {
        List soapModels = new ArrayList(models.size());

        for (int i = 0; i < models.size(); i++) {
            ecpErpCostCenter model = (ecpErpCostCenter) models.get(i);
            soapModels.add(toSoapModel(model));
        }

        return (ecpErpCostCenterSoap[]) soapModels.toArray(new ecpErpCostCenterSoap[0]);
    }

    public Long getPrimaryKey() {
        return _costCenterId;
    }

    public void setPrimaryKey(Long pk) {
        setCostCenterId(pk);
    }

    public Long getCostCenterId() {
        return _costCenterId;
    }

    public void setCostCenterId(Long costCenterId) {
        _costCenterId = costCenterId;
    }

    public String getErpCodeId() {
        return _erpCodeId;
    }

    public void setErpCodeId(String erpCodeId) {
        _erpCodeId = erpCodeId;
    }

    public String getCostCenterCode() {
        return _costCenterCode;
    }

    public void setCostCenterCode(String costCenterCode) {
        _costCenterCode = costCenterCode;
    }

    public String getCostCenterName() {
        return _costCenterName;
    }

    public void setCostCenterName(String costCenterName) {
        _costCenterName = costCenterName;
    }

    public String getDescription() {
        return _description;
    }

    public void setDescription(String description) {
        _description = description;
    }

    public String getCreater() {
        return _creater;
    }

    public void setCreater(String creater) {
        _creater = creater;
    }

    public Long getCreatTime() {
        return _creatTime;
    }

    public void setCreatTime(Long creatTime) {
        _creatTime = creatTime;
    }

    public String getUpdater() {
        return _updater;
    }

    public void setUpdater(String updater) {
        _updater = updater;
    }

    public Long getUpdateTime() {
        return _updateTime;
    }

    public void setUpdateTime(Long updateTime) {
        _updateTime = updateTime;
    }

    public String getAttribute1() {
        return _attribute1;
    }

    public void setAttribute1(String attribute1) {
        _attribute1 = attribute1;
    }

    public String getAttribute2() {
        return _attribute2;
    }

    public void setAttribute2(String attribute2) {
        _attribute2 = attribute2;
    }

    public String getAttribute3() {
        return _attribute3;
    }

    public void setAttribute3(String attribute3) {
        _attribute3 = attribute3;
    }

    public String getAttribute4() {
        return _attribute4;
    }

    public void setAttribute4(String attribute4) {
        _attribute4 = attribute4;
    }

    public String getAttribute5() {
        return _attribute5;
    }

    public void setAttribute5(String attribute5) {
        _attribute5 = attribute5;
    }
}


在com.test.portlet.ecperp.service包中生成几个java文件。
ecpErpCostCenterLocalService.java:

public interface ecpErpCostCenterLocalService {
    public java.util.List dynamicQuery(
        com.liferay.portal.kernel.dao.DynamicQueryInitializer queryInitializer)
        throws com.liferay.portal.SystemException;

    public java.util.List dynamicQuery(
        com.liferay.portal.kernel.dao.DynamicQueryInitializer queryInitializer,
        int begin, int end) throws com.liferay.portal.SystemException;
    // 增加
    public com.pccw.portlet.ecperp.model.ecpErpCostCenter createecpErpCostCenter(
        com.pccw.portlet.ecperp.model.ecpErpCostCenter account);
    // 根据主键删除	
    public com.pccw.portlet.ecperp.model.ecpErpCostCenter deleteecpErpCostCenter(
        java.lang.Long ecpAccountId) throws com.liferay.portal.SystemException;
    // 更新
    public com.pccw.portlet.ecperp.model.ecpErpCostCenter updateecpErpCostCenter(
        com.pccw.portlet.ecperp.model.ecpErpCostCenter center)
        throws com.liferay.portal.SystemException;
    // 主键查找
    public com.pccw.portlet.ecperp.model.ecpErpCostCenter findByCenterId(
        java.lang.Long centerId)
        throws com.liferay.portal.SystemException, 
            com.pccw.portlet.ecperp.NoSuchAccountException;
    // 查找所有
    public java.util.List findAll() throws com.liferay.portal.SystemException;
}


在ext-impl/classes/META-INF/ext-spring.xml配置文件中自动生成信息:
	<bean id="com.pccw.portlet.ecperp.service.ecpErpCostCenterLocalService.professional" class="com.pccw.portlet.ecperp.service.impl.ecpErpCostCenterLocalServiceImpl" lazy-init="true" />
	<bean id="com.pccw.portlet.ecperp.service.ecpErpCostCenterLocalService.transaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
		<property name="transactionManager">
			<ref bean="liferayTransactionManager" />
		</property>
		<property name="target">
			<ref bean="com.pccw.portlet.ecperp.service.ecpErpCostCenterLocalService.professional" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="check*">PROPAGATION_REQUIRED</prop>
				<prop key="clear*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="set*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
			</props>
		</property>
	</bean>
	<bean id="com.pccw.portlet.ecperp.service.ecpErpCostCenterLocalServiceFactory" class="com.pccw.portlet.ecperp.service.ecpErpCostCenterLocalServiceFactory" lazy-init="true">
		<property name="service">
			<ref bean="com.pccw.portlet.ecperp.service.ecpErpCostCenterLocalService.transaction" />
		</property>
	</bean>
	<bean id="com.pccw.portlet.ecperp.service.ecpErpCostCenterService.professional" class="com.pccw.portlet.ecperp.service.impl.ecpErpCostCenterServiceImpl" lazy-init="true" />
	<bean id="com.pccw.portlet.ecperp.service.ecpErpCostCenterService.transaction" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true">
		<property name="transactionManager">
			<ref bean="liferayTransactionManager" />
		</property>
		<property name="target">
			<ref bean="com.pccw.portlet.ecperp.service.ecpErpCostCenterService.professional" />
		</property>
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED</prop>
				<prop key="check*">PROPAGATION_REQUIRED</prop>
				<prop key="clear*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="set*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
			</props>
		</property>
	</bean>
	<bean id="com.pccw.portlet.ecperp.service.ecpErpCostCenterServiceFactory" class="com.pccw.portlet.ecperp.service.ecpErpCostCenterServiceFactory" lazy-init="true">
		<property name="service">
			<ref bean="com.pccw.portlet.ecperp.service.ecpErpCostCenterService.transaction" />
		</property>
	</bean>
	<bean id="com.pccw.portlet.ecperp.service.persistence.ecpErpCostCenterPersistenceImpl" class="com.pccw.portlet.ecperp.service.persistence.ecpErpCostCenterPersistenceImpl" lazy-init="true">
		<property name="dataSource">
			<ref bean="liferayDataSource" />
		</property>
		<property name="sessionFactory">
			<ref bean="liferaySessionFactory" />
		</property>
	</bean>
	<bean id="com.pccw.portlet.ecperp.service.persistence.ecpErpCostCenterUtil" class="com.pccw.portlet.ecperp.service.persistence.ecpErpCostCenterUtil" lazy-init="true">
		<property name="persistence">
			<ref bean="com.pccw.portlet.ecperp.service.persistence.ecpErpCostCenterPersistenceImpl" />
		</property>
	</bean>


然后要修改其中的三个文件,ext-service/新建包名/service/***EntryService.java和***EntryServiceUtil.java,***EntryService.java这个是个接口,你可以把你要用到的一些持久化的方法(ext-impl/新建包名/service/***Entrypersistence.java中有所有的方法)写到这里,***EntryServiceUtil.java这里写上这些静态方法(在action中操作的时候就是调用这个类),最后一个要修改的是ext-impl/新建包名/service/impl/***EntryServiceImpl.java因为他实现了,***EntryService这个接口所以把他的实现方法写这里就好了.

以上我说的可能比较粗糙,其实自动生成这些文件后,修改那三个文件时,如果您不知道怎么改可以参照liferay的源码,他的目录层次和这个是一样的,在blog和journalArtlice里面相对应的三个文件就可以了,基本上是一看就明白了.最后一点自动生成spring.xml中bean id="com.ext.portlet.包名.service.persistence.***EntryPersistenceImpl 对应的class值,这个值每次总是个错的,不晓得为什么,要手动的改一下.
分享到:
评论

相关推荐

    Liferay开发持久化层和服务层演示Demo代码

    通过分析“Liferay开发持久化层和服务层演示Demo代码”这一主题,我们将理解Liferay Service Builder如何帮助开发者高效地管理数据库操作,并创建自定义服务。 首先,Liferay是一个开放源代码的企业级门户平台,它...

    liferay 超级学习文档

    Liferay的Service Builder工具也可以与Hibernate结合,自动生成服务层代码,方便数据操作。 四、Liferay 学习路径 学习Liferay,首先要熟悉Java EE基础,包括Servlet、JSP、JPA等。接着,了解portlet开发,掌握...

    liferay架构

    例如,通过解析service.xml文件,可以自动生成EJB、HBM(Hibernate Mapping)和Model组件,这一过程主要依赖于Xdoclet引擎。这种自动化机制大大提升了开发效率,同时也确保了代码的一致性和可维护性。 ### 总结 ...

    Liferay教程

    在Liferay开发中,服务构建器是一个非常重要的工具,它能够自动生成与数据库交互的服务层代码。通过定义`service.xml`文件,开发者可以轻松地创建实体类、DAO层、本地服务接口等,极大地提高了开发效率。 #### `...

    liferay sdk

    开发者可以通过服务建设者生成持久化模型、DAO层、服务接口和实现,以及相应的XML配置文件。 5. **Liferay Theme**:Liferay主题允许开发者自定义门户的外观和样式。SDK包含创建和修改主题的工具,包括预设的模板和...

    Liferay技术文档

    每个 Portlet 的持久性数据都有单独的 ejb.xml 文件,通过 Ant 构建工具中的 build-ejb 命令生成,XDoclet 用于自动代码生成。 - **安全机制**:Liferay 采用 JAAS(Java Authentication and Authorization Service...

    Liferay_SSH_开发案例

    2. **自动生成持久化层和服务层的类** - **编写SERVICE.XML**:Service Builder是Liferay提供的一种工具,用于自动生成服务层代码。在`service.xml`中,定义实体类的字段、关联关系以及自动生成的服务接口和实现。 ...

    liferay portlet开发参考手册

    Service Builder允许开发者自动生成服务层代码,简化数据库操作,而DAO(数据访问对象)模式则提供了与数据库交互的标准接口。 书中还会涵盖主题和布局的主题,这是Liferay Portal的一大特色。通过创建和应用自定义...

    liferay-demo:Liferay示范

    通过Service Builder,开发者可以自动生成DAO、Service和API,从而减少手动编写SQL和持久层代码的工作量。 6. **Liferay Theme 开发** 主题决定了Liferay门户的外观。开发者可以通过修改或创建新的主题,改变颜色...

    LiferayPortal二次开发指南(read)

    同时,服务层(Service Layer)提供了一组基于JPA的持久化服务,方便操作数据库。 - 服务构建器:生成服务层代码,提供对数据模型的操作API。 - Hook开发:通过hook扩展或覆盖Liferay的默认行为,无需修改源码。 - ...

    GenericMatrixService:Java服务使用Spring数据和Hibernate来访问使用Liferay Service Builder创建的矩阵数据

    一旦定义完成,Service Builder会自动生成必要的服务接口、实现类以及DAO(数据访问对象),简化了数据库操作。 接下来,我们引入Spring Data,这是一个强大的框架,它简化了JPA(Java Persistence API)的使用,...

    J2EE开源框架

    PropertySet 则是一个基于 XML 的持久化方案。 - **AOP 和 IoC 框架**:Spring 是一个全能型的 AOP 和 IoC 容器,尽管配置较为复杂,但提供了强大的功能;AspectJ 和 AspectWerkz 分别是 AOP 方面的框架;...

    J2EE程序员需掌握的技术

    18. **项目构建和安装包制作**:Ant和Maven负责构建自动化,Izpack等工具用于创建安装程序。 19. **软件过程和建模设计**:CMMI、RUP、XP等软件过程模型,以及Rose、PowerDesigner等建模工具,对大型项目的管理至关...

Global site tag (gtag.js) - Google Analytics