Springmvc+phoenix+mybatis環境搭建css
1. 肯定phoenix服務器端的版本爲phoenix-4.4.0.2.4.2.0-258html
以及對應的jar包的版本java
phoenix-4.4.0.2.4.2.0-258-client.jarmysql
phoenix-core-4.4.0.2.4.2.0-258.jarweb
phoenix-server-4.4.0.2.4.2.0-258.jarspring
2. 在客戶端調用的時候,客戶端要與服務器端保持一致。sql
所以須要用phoenix-4.4.0.2.4.2.0-258-client.jar、phoenix-server-4.4.0.2.4.2.0-258.jarapache
可是因爲phoenix-core依賴了不少jar包,所以在調用的類的時候會出現報錯。api
如:driver class not found : org.apache.phoenix.jdbc.PhoenixDriverspring-mvc
解決辦法:不使用server端自帶的phoenix-core-4.4.0.2.4.2.0-258.jar
添加maven倉庫對應的版本依賴
<dependency>
<groupId>org.apache.phoenix</groupId>
<artifactId>phoenix-core</artifactId>
<version>4.4.0-HBase-1.1</version>
</dependency>
<dependency>
<groupId>sqlline</groupId>
<artifactId>sqlline</artifactId>
<version>1.1.9</version>
</dependency>
可是仍是要將phoenix-4.4.0.2.4.2.0-258-client.jar、phoenix-server-4.4.0.2.4.2.0-258.jar添加到classpath中。
備註:在配置dependency的過程當中可能會出現jar包衝突以至出現一堆莫名其妙的錯誤,我主要遇到了下邊的jar報衝突,在phoenix-core的dependency中加上如下內容:
<exclusions>
<exclusion>
<groupId>org.antlr</groupId>
<artifactId>antlr</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-util</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jsp-api-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.1</artifactId>
</exclusion>
<exclusion>
<groupId>org.mortbay.jetty</groupId>
<artifactId>servlet-api-2.5</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
</exclusion>
<exclusion>
<groupId>tomcat</groupId>
<artifactId>jasper-compiler</artifactId>
</exclusion>
<exclusion>
<groupId>tomcat</groupId>
<artifactId>jasper-runtime</artifactId>
</exclusion>
有時候即便加上了這些還會有jar包衝突的狀況,還得具體項目具體解決。
3. 總體配置
1)jdbc.properties
c3p0.url=jdbc:mysql://<host>:3306/db1?useUnicode=true&characterEncoding=utf8&mysqlEncoding=utf8
c3p0.user=root
c3p0.password=XXXXX
c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.acquireIncrement=1
c3p0.maxIdleTime=60
c3p0.maxPoolSize=10
c3p0.minPoolSize=5
c3p0.initialPoolSize=300
c3p02.url=jdbc:phoenix:host1,host2,host3:2181:/hbase-unsecure
c3p02.user=
c3p02.password=
c3p02.driverClass=org.apache.phoenix.jdbc.PhoenixDriver
c3p02.acquireIncrement=1
c3p02.maxIdleTime=60
c3p02.maxPoolSize=5
c3p02.minPoolSize=3
c3p02.initialPoolSize=3
2)spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd
">
<context:property-placeholder location="classpath:config/db.properties" ignore-unresolvable="true"/>
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${c3p0.driverClass}"></property>
<property name="jdbcUrl" value="${c3p0.url}"></property>
<property name="user" value="${c3p0.user}"></property>
<property name="password" value="${c3p0.password}"></property>
<property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:config/mybatis-config.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jyl.toeat.dao.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1" />
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<bean id="dataSource2"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${c3p02.driverClass}"></property>
<property name="jdbcUrl" value="${c3p02.url}"></property>
<property name="user" value="${c3p02.user}"></property>
<property name="password" value="${c3p02.password}"></property>
<property name="acquireIncrement" value="${c3p02.acquireIncrement}"></property>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="maxIdleTime" value="${c3p02.maxIdleTime}"></property>
<property name="maxPoolSize" value="${c3p02.maxPoolSize}"></property>
<property name="minPoolSize" value="${c3p02.minPoolSize}"></property>
<property name="acquireRetryDelay" value="1000"></property>
<property name="acquireRetryAttempts" value="60"></property>
<property name="breakAfterAcquireFailure" value="false"></property>
</bean>
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"></property>
<property name="configLocation" value="classpath:config/mybatis-config2.xml" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jyl.toeat.dao.mapper2"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2" />
</bean>
</beans>
3)SpringMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
<!-- 註解掃描包 -->
<context:component-scan base-package="com.jyl.toeat" />
<!-- 開啓註解 -->
<mvc:annotation-driven />
<!--
配置靜態資源,直接映射到對應的文件夾,不被DispatcherServlet處理,3.04新增功能,須要從新設置spring-mvc-3.0.xsd
-->
<mvc:resources mapping="/img/**" location="/img/" />
<mvc:resources mapping="/js/**" location="/js/" />
<mvc:resources mapping="/css/**" location="/css/" />
<mvc:resources mapping="/html/**" location="/html/" />
<mvc:resources mapping="/plugIn/**" location="/plugIn/" />
<!-- 定義跳轉的文件的先後綴 ,視圖模式配置-->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 這裏的配置個人理解是自動給後面action的方法return的字符串加上前綴和後綴,變成一個 可用的url地址 -->
<property name="prefix" value="/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
4)phoenix的mapper 、pojo也可使用mybatis generator生成
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry
location="C:\Users\jade\Desktop\lib\phoenix-4.4.0.2.4.2.0-258-client.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<jdbcConnection driverClass="org.apache.phoenix.jdbc.PhoenixDriver"
connectionURL="jdbc:phoenix:host1,host2,host3:2181:/hbase-unsecure"
userId=""
password=""/>
<javaModelGenerator targetPackage="com.XXXX.XX.pojo"
targetProject="test"
>
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<sqlMapGenerator targetPackage="com.XXXX.XX.dao.mapper"
targetProject="test">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<javaClientGenerator targetPackage="com.XXXX.XXX.dao.mapper"
targetProject="test" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<table tableName="TEST_LIUCUI" >
</table>
</context>
</generatorConfiguration>
5)建立兩個mybati的config文件
mybatis-config.xml、mybatis-config2 .xml
6)建立兩個mybatis的目錄mapper、mapper2 分別存放mysql和生成的phoenix的xml文件
注意: mybatis generator生成的insert語句 可是phoenix插入用的upsert,
所以須要將insert改成upsert
<insert id="insert" parameterType="com.jyl.toeat.dao.pojo.TestLiucui" >
upsert into TEST_LIUCUI (ID, ACCOUNT, PASSWD
)
values (#{id,jdbcType=VARCHAR}, #{account,jdbcType=VARCHAR}, #{passwd,jdbcType=VARCHAR}
)
</insert>