独立使用OFBiz实体引擎

实体引擎(Entity Engine)在OFBiz中是不可或缺的核心组件。本文并不是一篇实体引擎的使用帮助,只是想说明如何独立地去使用实体引擎,可以方便自己阅读源码。笔者将建立一个gradle项目,用来管理依赖。

 

首先,需要下载OFBiz,可以下载发布包,也可以下载SVN源码。发布包下载地址:http://ofbiz.apache.org/download.html,SVN源码下载:

svn co http://svn.apache.org/repos/asf/ofbiz/trunk ofbiz
 
然后,我们来建gradle项目,创建一个settings.gradle和build.gradle文件:

 

rootProject.name = 'ofbiz-test-entityengine'
 
apply plugin: 'java'
apply plugin: 'maven'

project.group = 'com.zf'
project.version = '1.0.0'
project.archivesBaseName = 'ofbiz-test-entityengine'

sourceCompatibility = '1.6'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.zf.ofbiz.entity.Main'
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    
    compile (
        'commons-codec:commons-codec:1.6'
        ,'commons-dbcp:commons-dbcp:1.4'
        ,'commons-lang:commons-lang:2.6'
        ,'commons-logging:commons-logging:1.1.2'
        ,'commons-pool:commons-pool:1.3'
        ,'mysql:mysql-connector-java:5.1.20'
        ,'log4j:log4j:1.2.17'
        ,'javolution:javolution:5.4.3'
    )
    
    testCompile (
        'junit:junit:4.10'
    )
}

task sourcesJar(type: Jar, dependsOn: classes, description: 'Creates a jar from the source files.') {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives jar
    archives sourcesJar
}
 
可以看到依赖的包有由compile指定的几个,这是在maven仓库里有的;还有一部分是compile fileTree指定的项目libs目录下的所有jar包,主要有:
  1. antisamy-bin.1.2.jar
  2. geronimo-transaction-2.1.1.jar
  3. ofbiz-base.jar
  4. servlet-api-3.0.jar
  5. clhm-release-1.0-lru.jar
  6. icu4j-49_1.jar
  7. ofbiz-entity.jar
  8. xercesImpl-2.9.1.jar
  9. geronimo-j2ee-connector_1.5_spec-1.0.jar
  10. jakarta-oro-2.0.8.jar
  11. ofbiz-geronimo.jar
  12. xstream-1.3.1.jar
  13. geronimo-jta_1.1_spec-1.1.1.jar
  14. jdbm-1.0-SNAPSHOT.jar
  15. owasp-esapi-full-java-1.4.jar
这此依赖包都是必须的。在OFBIZ_HOME/framework/下的各目录里去搜索都能找到。ofbiz-*.jar是编译OFBiz生成出来的,所以需要先执行一下OFBIZ_HOME/ant build。
 
最后,我们来测试一下OFBiz的实体引擎吧。案例将会设计三个实体(对应两张表):User、Party、PartyAndUser。这三个实体的定义在entitymodel.xml文件中。
<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitymodel.xsd">
    
    <title>Entity of an Open For Business Project Component</title>
    <description>None</description>
    <version>1.0</version>

    <entity package-name="org.zf.entity" entity-name="Party" title="Party Entity" no-auto-stamp="true">
        <field name="partyId" type="id-int"></field>
        <field name="partyName" type="name"></field>
        <prim-key field="partyId"/>
    </entity>

    <entity package-name="org.zf.entity" entity-name="User" title="User Entity" no-auto-stamp="true">
        <field name="userId" type="id-int"></field>
        <field name="partyId" type="id-int"></field>
        <field name="loginName" type="name"></field>
        <prim-key field="userId"/>
    </entity>
    
    <view-entity package-name="org.zf.entity.view" entity-name="PartyAndUser" title="Party and User Entity">
        <member-entity entity-alias="PARTY" entity-name="Party"/>
        <member-entity entity-alias="USER" entity-name="User"/>
        <alias-all entity-alias="PARTY"/>
        <alias-all entity-alias="USER"/>
        <view-link entity-alias="PARTY" rel-entity-alias="USER">
            <key-map field-name="partyId" rel-field-name="partyId"/>
        </view-link>
    </view-entity>
    
</entitymodel>
 
除了我们定义的实体外,我们还要配置一下实体引擎,主要配置的是数据库连接、实体配置目录以及数据字段类型。通过文件entityengine.xml定义。
<?xml version="1.0" encoding="UTF-8" ?>
<entity-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entity-config.xsd">
    
    <resource-loader name="fieldTypeLoader" class="org.ofbiz.base.config.ClasspathLoader" prefix="fieldtype/"/>
    
    <resource-loader name="entityLoader" class="org.ofbiz.base.config.ClasspathLoader" prefix="entity/"/>

    <transaction-factory class="org.ofbiz.geronimo.GeronimoTransactionFactory"/>

    <connection-factory class="org.ofbiz.entity.connection.DBCPConnectionFactory"/>

    <debug-xa-resources value="false" />
    
    <delegator name="default" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main">
        <group-map group-name="org.ofbiz" datasource-name="localmysql"/>
        <group-map group-name="org.ofbiz.olap" datasource-name="localmysqlolap"/>
        <group-map group-name="org.ofbiz.tenant" datasource-name="localmysqltenant"/>
    </delegator>
    
    <!-- 实体配置 -->
    <entity-model-reader name="main">
        <resource loader="entityLoader" location="entitymodel.xml" />
    </entity-model-reader>

    <entity-group-reader name="main"/>

    <entity-eca-reader name="main"/>

    <entity-data-reader name="tenant"/>
    <entity-data-reader name="seed"/>
    <entity-data-reader name="seed-initial"/>
    <entity-data-reader name="demo"/>
    <entity-data-reader name="ext"/>
    <entity-data-reader name="ext-test"/>
    <entity-data-reader name="ext-demo"/>

    <!-- 数据类型配置,可以拷贝OFBIZ_HOME/framework/entity/fieldtype/fieldtypemysql.xml文件到项目 -->
    <field-type name="mysql" loader="fieldTypeLoader" location="fieldtypemysql.xml"/>
    
    <!-- 数据源配置 -->
    <datasource name="localmysql"
                helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
                field-type-name="mysql"
                check-on-start="true"
                add-missing-on-start="true"
                check-pks-on-start="false"
                use-foreign-keys="true"
                join-style="ansi-no-parenthesis"
                alias-view-columns="false"
                drop-fk-use-foreign-key-keyword="true"
                table-type="InnoDB"
                character-set="utf8"
                collate="utf8_general_cs">
        <read-data reader-name="tenant"/>
        <read-data reader-name="seed"/>
        <read-data reader-name="seed-initial"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <read-data reader-name="ext-test"/>
        <read-data reader-name="ext-demo"/>
        <inline-jdbc
            jdbc-driver="com.mysql.jdbc.Driver"
            jdbc-uri="jdbc:mysql://localhost/ofbiz?characterEncoding=UTF8"
            jdbc-username="ofbiz"
            jdbc-password="ofbiz"
            isolation-level="ReadCommitted"
            pool-minsize="2"
            pool-maxsize="250"
            time-between-eviction-runs-millis="600000"/>
    </datasource>
    <datasource name="localmysqlolap"
                helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
                field-type-name="mysql"
                check-on-start="true"
                add-missing-on-start="true"
                check-pks-on-start="false"
                use-foreign-keys="true"
                join-style="ansi-no-parenthesis"
                alias-view-columns="false"
                drop-fk-use-foreign-key-keyword="true"
                table-type="InnoDB"
                character-set="utf8"
                collate="utf8_general_cs">
        <read-data reader-name="tenant"/>
        <read-data reader-name="seed"/>
        <read-data reader-name="seed-initial"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <read-data reader-name="ext-test"/>
        <read-data reader-name="ext-demo"/>
        <inline-jdbc
            jdbc-driver="com.mysql.jdbc.Driver"
            jdbc-uri="jdbc:mysql://127.0.0.1/ofbizolap?autoReconnect=true"
            jdbc-username="ofbiz"
            jdbc-password="ofbiz"
            isolation-level="ReadCommitted"
            pool-minsize="2"
            pool-maxsize="250"
            time-between-eviction-runs-millis="600000"/>
    </datasource>
    <datasource name="localmysqltenant"
                helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
                field-type-name="mysql"
                check-on-start="true"
                add-missing-on-start="true"
                check-pks-on-start="false"
                use-foreign-keys="true"
                join-style="ansi-no-parenthesis"
                alias-view-columns="false"
                drop-fk-use-foreign-key-keyword="true"
                table-type="InnoDB"
                character-set="utf8"
                collate="utf8_general_cs">
        <read-data reader-name="tenant"/>
        <read-data reader-name="seed"/>
        <read-data reader-name="seed-initial"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <read-data reader-name="ext-test"/>
        <read-data reader-name="ext-demo"/>
        <inline-jdbc
            jdbc-driver="com.mysql.jdbc.Driver"
            jdbc-uri="jdbc:mysql://127.0.0.1/ofbiztenant?autoReconnect=true"
            jdbc-username="ofbiz"
            jdbc-password="ofbiz"
            isolation-level="ReadCommitted"
            pool-minsize="2"
            pool-maxsize="250"
            time-between-eviction-runs-millis="600000"/>
    </datasource>
</entity-config>
 
这样主要的配置文件都已经有了。对了,还要把OFBIZ_HOME/framework/base/config/cache.properties文件拷贝到项目,也可以再OFBIZ_HOME/framework/base/config/log4j.xml文件到项目目录。下面就是我们的测试代码了:
package com.zf.ofbiz.entity;

import java.util.List;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;

public class Main {    
    public static void main(String[] args) throws Exception {        
        Delegator delegator = DelegatorFactory.getDelegator("default");
       
        List<GenericValue> valueList = delegator.findList("PartyAndUser", EntityCondition.makeCondition("partyName", "TEST"), null, null, null, true);
        System.out.println(valueList.size());
    }
}
 
这样我们就可以独立使用实体引擎了,可以更方便地去测试、学习OFBiz的实体引擎。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值