2017-02-17 27 views
3

出於某種原因,我們需要運行數據庫原生查詢而不是靈活的查詢。爲了運行這些查詢,我們需要數據庫連接,因此我們如何從Hybris獲取jdbcTemplate或DataSource對象。Hybris數據庫連接

+1

對於到底是什麼在更多的細節?您還可以在靈活搜索中使用數據庫相關條件。如果代碼未被識別爲靈活搜索,則它將按照在數據庫中的原樣傳遞。 –

回答

7

這是一個腳本常規,可以實現這一點的例子:

import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import de.hybris.platform.util.Utilities; 
import de.hybris.platform.core.Registry; 

Connection conn = null; 
PreparedStatement pstmt = null; 

try 
{ 
    conn = Registry.getCurrentTenant().getDataSource().getConnection(); 

    pstmt = conn.prepareStatement("your sql query here..."); 

    pstmt.execute(); 

} 
catch (final SQLException e) 
{ 
    LOG.error("Error!!"); 
} 
finally 
{ 
    Utilities.tryToCloseJDBC(conn, pstmt, null); 
} 

return "Groovy Rocks!" 


編輯:發現本文中的 https://www.stackextend.com/hybris/run-native-sql-query-hybris/

+0

很高興知道.. – HybrisFreelance