Phoenix:Phoenix將SQL查詢語句轉換成多個scan操作,並編排執行最終生成標准的JDBC結果集。
Spring將數據庫訪問的樣式代碼提取到JDBC模板類中,JDBC模板還承擔了資源管理和異常處理的工作,Phoenix作為JDBC驅動同樣可以將其與Spring集成,提高開發效率。
具體操作如下:
1.配置applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:hdp="http://www.springframework.org/schema/hadoop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd">
//配置Phoenix數據源 <bean id="phoenixDataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="org.apache.phoenix.jdbc.PhoenixDriver"/> <property name="url" value="jdbc:phoenix:master"/> <property name="initialSize" value="15"/> <property name="maxActive" value="0"/> </bean> //選擇JDBC模板 <bean id="phoenixJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg ref="phoenixDataSource"/> <qualifier value="phoenixJdbcTemplate"/> </bean> <bean id="travelDao" class="com.mobin.dao.impl.TravelDaoImpl"> <property name="jdbcTemplate" ref="phoenixJdbcTemplate"/> </bean> </beans>
2.JDBC代碼模板
public class TravelDaoImpl implements TravelDao{ private JdbcTemplate jdbcTemplate; jdbcTemplate.xxx()//通過調用jdbcTemplate封裝的方法訪問Phoenix並進行相關的操作如查詢,更新等 public JdbcTemplate getJdbcTemplate() { return jdbcTemplate; } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } }