Springmvc+phoenix+mybatis环境搭建

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>