在Ofbiz中使用源生sql

在Ofbiz中我們使用delegator 這個類對數據庫進行操作,

這個類在絕大多數方面都可以滿足我們的需求,

但是在一些特殊的地方,我們還是很懷念源生的sql。

下面介紹的方法就是在ofbiz中使用源生sql:

//使用源生的JDBC方式
String sql = "update product t set t.whether_sale='Y' "
		+"where t.product_store_id="+productStoreId;
//獲得gropuHelperName
//這個org.ofbiz是寫在entityengine.xml的文件中的
String groupHelperName = delegator.getGroupHelperName("org.ofbiz");
//獲得數據庫的連接
Connection conn = ConnectionFactory.getConnection(groupHelperName);
//獲得Statement
Statement stmt = conn.createStatement();
//執行sql 將商品是否可購買狀態設置爲N
tmt.executeUpdate(sql);
//end

我們來看下entityengine.xml文件。

<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="localderby"/>
        <group-map group-name="org.ofbiz.olap" datasource-name="localderbyolap"/>
        <group-map group-name="org.ofbiz.tenant" datasource-name="localderbytenant"/> -->
        
        <!-- <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"/> -->
        
        <group-map group-name="org.ofbiz" 			datasource-name="localoracle"/>
        <group-map group-name="org.ofbiz.olap" 		datasource-name="localoracle"/>
        <group-map group-name="org.ofbiz.tenant" 	datasource-name="localoracle"/>
        
        <!-- <group-map group-name="org.ofbiz" 		datasource-name="localpostnew"/>
        <group-map group-name="org.ofbiz.olap" 		datasource-name="localpostolap"/>
        <group-map group-name="org.ofbiz.tenant" 	datasource-name="localposttenant"/> -->
</delegator>

這個xml文件中的group-name就可以幫助我們獲得到groupHelperName。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章