淘先锋技术网

首页 1 2 3 4 5 6 7
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文件:
[b]ecpErpCostCenter.java:[/b]

public interface ecpErpCostCenter extends ecpErpCostCenterModel {
}


[b]ecpErpCostCenterModel.java:[/b]

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);
}


[b]ecpErpCostCenterSoap.java:[/b]

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文件。
[b]ecpErpCostCenterLocalService.java:[/b]

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值,这个值每次总是个错的,不晓得为什么,要手动的改一下.