ofbiz连接mysql并创建独立数据库

时间:2022-05-03
本文章向大家介绍ofbiz连接mysql并创建独立数据库,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

         Ofbiz:数据库移植mysql并创建自己的mysql

         Ofbiz原生数据库是derby,而作为开发使用,其就不能满足我们需求,ofbiz支持多种数据库,我们就可以将数据移植到mysql.

         第一步:找到frameworkentityconfigentityengine.xml这个文件,找到之后进行下面相关操作.

         1、添加或者修改datasource,因为该文件本身存在这些资料,只是被注释掉了.

<datasourcename="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_ci"> 

   <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"/> 

   <inline-jdbc 

           jdbc-driver="com.mysql.jdbc.Driver" 

           jdbc-uri="jdbc:mysql://localhost:3306/ofbiz?autoReconnect=true&characterEncoding=UTF-8" 

           jdbc-username="root" 

           jdbc-password="" 

           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_ci "> 

   <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"/> 

   <inline-jdbc 

           jdbc-driver="com.mysql.jdbc.Driver" 

           jdbc-uri="jdbc:mysql://localhost:3306/ofbizolap?autoReconnect=true&characterEncoding=UTF-8" 

           jdbc-username="root" 

           jdbc-password="" 

           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_ci"> 

   <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"/> 

   <inline-jdbc 

           jdbc-driver="com.mysql.jdbc.Driver" 

           jdbc-uri="jdbc:mysql://localhost:3306/ofbiztenant?autoReconnect=true&characterEncoding=UTF-8" 

           jdbc-username="root" 

           jdbc-password="" 

           isolation-level="ReadCommitted" 

           pool-minsize="2" 

           pool-maxsize="250" 

           time-between-eviction-runs-millis="600000"/> 

</datasource> 

          2.还是在该文件添加一些内容,注意上面的datasource name=""与下面的datasource-name是对应的.

<delegator name="default"entity-model-reader="main" entity-group-reader="main"entity-eca-reader="main"distributed-cache-clear-enabled="false">

      <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>

   

   <delegator name="default-no-eca"entity-model-reader="main" entity-group-reader="main"entity-eca-reader="main" entity-eca-enabled="false"distributed-cache-clear-enabled="false">

             <group-mapgroup-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>

 

   <!-- be sure that your default delegator (or the one you use) usesthe same datasource for test. You must run "ant load-demo" beforerunning "ant run-tests" -->

   <delegator name="test" entity-model-reader="main"entity-group-reader="main" entity-eca-reader="main">

        <group-mapgroup-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>

        datasource-name:就是配置1中对应的数据库名

        group-name: :是用来进行分组辨识的,即可以将数据资料移植到不同的数据库

         问题:可以一直为什么要创建三个数据库,一个不行?带着这个问题我们进行下面操作.

第二步:找到frameworkentityentitydefentitygroup.xml文件,内容如下

<span style="font-size:14px;"><?xml version="1.0"encoding="UTF-8"?>

<entitygroup xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitygroup.xsd">

 

   <!-- ========================================================= -->

   <!-- org.ofbiz.entity.tenant -->

   <!-- ========================================================= -->

   <entity-group group="org.ofbiz.tenant"entity="Tenant"/>

   <entity-group group="org.ofbiz.tenant"entity="TenantDataSource"/>

   <entity-group group="org.ofbiz.tenant"entity="Component"/>

   <entity-group group="org.ofbiz.tenant"entity="TenantComponent"/>

   <entity-group group="org.ofbiz.tenant"entity="TenantDomainName"/>

</entitygroup></span>

        就是这个配置将Tenant等一些实体(数据库中的表,至于创建方式我就不在这里讲述了)分到org.ofbiz.tenant这个组名的数据库中即上面中第三个数据库,数据库名叫localmysqltenant.当然若没有这种配置,那么数据资料就会到默认数据库中,但是这里是项目自带,所以我们就需要这样一个数据库,同样的道理还有到另一个数据库中的资料,加上默认的,所以我们需要三个数据库.

至于这些文件的加载在当前目下ofbiz-component.xml文件下,如下:

   <entity-resource type="model"reader-name="main" loader="main"location="entitydef/entitymodel.xml"/>

   <entity-resourcetype="group" reader-name="main" loader="main"location="entitydef/entitygroup.xml"/>

          注意:framework/base/lib下需要导入mysql的包

          上面就配置完成,然后我们在数据库创建对应的三个数据库名会跟上文中一一对应(注意编码一致),在启动的时候带参数load-data(如何代参运行,不知道的话,详情百度,嘻嘻)

下面我们独立创建一个数据库,来放我们自己的资料.

相应文件的位置:

        第一步:在上文配置文件frameworkentityconfigentityengine.xml下加入相应的配置,我的资料如下:

 <datasourcename="wuliys" 
<pre name="code" class="html">
       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_ci"> 

   <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"/> 

   <inline-jdbc 

           jdbc-driver="com.mysql.jdbc.Driver" 

           jdbc-uri="jdbc:mysql://localhost:3306/wuliys?autoReconnect=true&characterEncoding=UTF-8" 

           jdbc-username="root" 

           jdbc-password="" 

           isolation-level="ReadCommitted" 

           pool-minsize="2" 

           pool-maxsize="250" 

           time-between-eviction-runs-millis="600000"/> 

</datasource>
<pre name="code" class="html"><span style="font-size:18px;"><group-mapgroup-name="org.ofbiz.wuliys" datasource-name="wuliys"/></span>

至于这些资料所放位置与上文一一对应

        第二步:创建对应的数据库wuliys,并设置其编码为utf-8,字符集utf8_general_ci

       第三步:创建一个实体,文件路径mypartyentitydefentitymodel.xml,注意其可以是hot-deploy下的一个模块,也可以是你分离出来的一个模块.内容如下:

<pre name="code" class="html"><span style="font-size:18px;"><entitymodelxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

   xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitymodel.xsd">

    <title>Entity of anApache OFBiz Component</title>

   <description>None</description>

   <version>1.0</version>

    <entityentity-name="Test" package-name="wuliys">

         <field name="myId" type="id-ne"></field>

         <field name="myName"type="id-ne"></field>

         <prim-key field="myId"/>

    </entity>  

</entitymodel></span>

     第四步:将创建的实体引入到自己所想到的分组,文件路径: mypartyentitydefentitygroup.xml,内容如下:

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>

<entitygroupxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitygroup.xsd">

<entity-group entity="Test"group="org.ofbiz.wuliys"/>

</entitygroup></span><span style="font-size:18px;"> 
</span>

    第五步:在该实体下创建一条数据,文件路径mypartydatatestdata.xml,内容如下:

<span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>

   <entity-engine-xml>

       <Test myName="jack"></Test>

       <Test myName="zheng"></Test>

  </entity-engine-xml> 
</span>

          第六步:将这写配置文件在该模块的ofbiz-component.xml下加载,内容如下

<span style="font-size:18px;"><?xmlversion="1.0" encoding="UTF-8"?>

<ofbiz-componentname="myparty"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">

      <resource-loader name="main"type="component"/>

      <entity-resource type="model"reader-name="main" loader="main"location="entitydef/entitymodel.xml"/>

       <entity-resource type="group"reader-name="main" loader="main"location="entitydef/entitygroup.xml"/>

            <entity-resourcetype="data" reader-name="seed" loader="main"location="data/testdata.xml"/>

         <webappname="myparty"

               server="default-server"

               location="webapp/myparty"

               mount-point="/myparty"></webapp>

</ofbiz-component></span><span style="font-size:18px;"> 
</span>

          而后带参load-data启动一下,就在数据库相应位置创建好了自己的数据资料.

                  备注:新手写blog,查阅网上的相关资料,加上自己的一些想法