jersey + spring + mybatis + redis項目搭建

Jersey + Spring + Mybatis + Redis項目搭建

2016年07月11日 15:36:29java

最近在公司作項目,想採用最近比較熱的restful來作,思前想後採用了jersey + spring + mybatis + redis來搭建項目,此處留下詳細的配置,方便之後查閱,jersey採用2.13版本,與1.x版本差異比較大,主要是容器的差異!spring採用3.2的版本!mysql

一、採用maven搭建項目

pom以下web

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <jersey.version>2.13</jersey.version>
        <spring.version>3.2.0.RELEASE</spring.version>
        <quartz.version>2.2.1</quartz.version>
        <shiro.version>1.2.2</shiro.version>
        <httpapi.version>4.1.3</httpapi.version>
        <dbcp.version>1.4</dbcp.version>
        <mybatis.version>3.2.6</mybatis.version>
        <junit.version>4.10</junit.version>
        <mybatis.version>3.2.6</mybatis.version>
        <junit.version>4.10</junit.version>
        <spring-mybatis.version>1.2.2</spring-mybatis.version>
        <mysql.version>5.1.30</mysql.version>
        <log4j.version>1.2.17</log4j.version>
        <fastjson.version>1.2.7</fastjson.version>
        <google.json>2.5</google.json>
        <slf4j.log4j12.version>1.6.4</slf4j.log4j12.version>
        <google.zing.version>3.2.0</google.zing.version>
        <javax.annotation.version>1.2</javax.annotation.version>
        <jstl.version>1.2</jstl.version>
        <servletapi.version>2.4</servletapi.version>
        <jspapi.version>2.2</jspapi.version>
        <sitemesh.version>3.0.0</sitemesh.version>
        <fileupload.version>1.3.1</fileupload.version>
        <dom4j.version>1.6.1</dom4j.version>
        <qiniu-java-sdk>7.0.0</qiniu-java-sdk>
    </properties>
    <dependencies>
            <dependency>
                <groupId>cglib</groupId>
                <artifactId>cglib</artifactId>
                <version>2.2.2</version>
            </dependency><!--用於aop實現,在下一篇博客裏面會寫到具體用法-->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>1.8.5</version>
            </dependency>
            <dependency>
                <groupId>aopalliance</groupId>
                <artifactId>aopalliance</artifactId>
                <version>1.0</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>1.8.5</version>
            </dependency>
            <!-- start jersey dependency -->
            <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
                <artifactId>jersey-media-multipart</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
                <artifactId>jersey-server</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.containers</groupId>
                <artifactId>jersey-container-servlet</artifactId>
                <version>${jersey.version}</version>
            </dependency>
            <dependency>
                <groupId>org.glassfish.jersey.ext</groupId>
                <artifactId>jersey-spring3</artifactId>
                <version>${jersey.version}</version>
                <exclusions>
                    <exclusion><!--排除jersey自帶的spring版本,便於本身控制-->
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aop</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-context</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <artifactId>spring-web</artifactId>
                        <groupId>org.springframework</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <!-- end jersey dependency -->
            <!-- start spring dependency -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>${spring.version}</version>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aspects</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
                <version>${spring.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-core</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-beans</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context-support</artifactId>
                <version>${spring.version}</version>
                <type>jar</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <!-- end spring dependency -->
            <!-- start quartz dependency -->
            <dependency>
                <groupId>org.quartz-scheduler</groupId>
                <artifactId>quartz</artifactId>
                <version>${quartz.version}</version>
            </dependency>
            <!-- end quartz dependency -->
            <!-- start http dependency -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpclient</artifactId>
                <version>${httpapi.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>${httpapi.version}</version>
            </dependency>
            <!-- end http dependency -->
            <!-- start common dependency -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>${dbcp.version}</version>
            </dependency>
            <dependency>
                <groupId>commons-fileupload</groupId>
                <artifactId>commons-fileupload</artifactId>
                <version>${fileupload.version}</version>
            </dependency>
            <!-- end common dependency -->
            <!-- start mybatis dependency -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>${spring-mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>${mybatis.version}</version>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
            <!-- end mybatis dependency -->
            <!-- start redis dependency -->
            <dependency>
                <groupId>org.springframework.data</groupId>
                <artifactId>spring-data-redis</artifactId>
                <version>1.4.0.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
                <version>2.5.0</version>
            </dependency>
            <!-- end redis dependency -->
            <!-- start json dependency -->
            <dependency>
                <groupId>org.apache.httpcomponents</groupId>
                <artifactId>httpcore</artifactId>
                <version>${httpapi.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>${fastjson.version}</version>
            </dependency>
            <dependency>
                <groupId>com.google.code.gson</groupId>
                <artifactId>gson</artifactId>
                <version>${google.json}</version>
            </dependency>
            <!-- end json dependency -->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
                <scope>test</scope>
            </dependency>
            <!-- -->
            <dependency>
                <groupId>servletapi</groupId>
                <artifactId>servletapi</artifactId>
                <version>${servletapi.version}</version>
                <scope>provided</scope><!-- 打包的時候不會打進去,由於tomcat帶的有 -->
            </dependency>
            <dependency>
                <groupId>javax.servlet.jsp</groupId>
                <artifactId>jsp-api</artifactId>
                <version>${jspapi.version}</version>
                <scope>provided</scope>
            </dependency>
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>${jstl.version}</version>
            </dependency>
            <!-- -->
            <!-- Shiro security -->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-core</artifactId>
                <version>${shiro.version}</version>
            </dependency><!--登陸框架-->
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-web</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-cas</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.shiro</groupId>
                <artifactId>shiro-spring</artifactId>
                <version>${shiro.version}</version>
            </dependency>
            <dependency>  
                <groupId>org.apache.shiro</groupId>  
                <artifactId>shiro-ehcache</artifactId>  
                <version>${shiro.version}</version>  
            </dependency>  
            <dependency>
                <groupId>org.sitemesh</groupId>
                <artifactId>sitemesh</artifactId>
                <version>${sitemesh.version}</version>
            </dependency>
            <!-- 日誌文件 -->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
                <version>${slf4j.log4j12.version}</version>
            </dependency>
            <!-- dom4j -->
            <dependency>
                <groupId>dom4j</groupId>
                <artifactId>dom4j</artifactId>
                <version>${dom4j.version}</version>
            </dependency>
            <!-- end -->
            <!-- start 七牛依賴包 -->
            <dependency>
                <groupId>com.qiniu</groupId>
                <artifactId>qiniu-java-sdk</artifactId>
                <version>${qiniu-java-sdk}</version>
            </dependency><!--圖片上傳用到,圖片採用的七牛服務器-->
            <!-- end 七牛依賴包 -->
        </dependencies>

二、web.xml配置

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <module-name>api</module-name>
   <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <filter>
        <description>字符集過濾器</description>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <description>字符集編碼</description>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring.xml,classpath:spring-mybatis.xml,classpath:redis.xml</param-value>
  </context-param>
  <servlet>
      <servlet-name>SpringApplication</servlet-name>
      <!--此處與jersey1.x區別較大-->
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.cn.the3ctv.api</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>SpringApplication</servlet-name>
      <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

三、spring 配置

<?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:task="http://www.springframework.org/schema/task"   
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd 
http://www.springframework.org/schema/task   
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop   
http://www.springframework.org/schema/aop/spring-aop.xsd
"><!--不寫具體的版本,他會自動按照spring的版原本-->
    <task:annotation-driven/>
    <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
    <context:annotation-config/>
    <context:property-placeholder location="file:/tomcat/config/jdbc.properties,file:/tomcat/config/redis.properties" />
    <context:component-scan base-package="com.cn.api" />
</beans>

四、spring-mybatis配置

<?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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 初始化鏈接大小 -->
        <property name="initialSize" value="${jdbc.initialSize}"></property>
        <!-- 鏈接池最大數量 -->
        <property name="maxActive" value="${jdbc.maxActive}"></property>
        <!-- 鏈接池最大空閒 -->
        <property name="maxIdle" value="${jdbc.maxIdle}"></property>
        <!-- 鏈接池最小空閒 -->
        <property name="minIdle" value="${jdbc.minIdle}"></property>
        <!-- 獲取鏈接最大等待時間 -->
        <property name="maxWait" value="${jdbc.maxWait}"></property>
        <property name="validationQuery" value="${jdbc.validationQuery}"></property>
        <property name="testOnBorrow" value="${jdbc.testOnBorrow}" /> 
        <!--removeAbandoned: 是否自動回收超時鏈接-->  
    </bean>

    <!-- spring和MyBatis完美整合,不須要mybatis的配置映射文件 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 自動掃描mapping.xml文件 -->
        <property name="mapperLocations" value="classpath*:com/cn/api/mapper/**/*.xml"></property>
    </bean>

    <!-- DAO接口所在包名,Spring會自動查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.cn.api.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- (事務管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
</beans>

五、redis配置

<?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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">
  <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">  

    <property name="maxTotal" value="${redis.pool.maxActive}" />
        <!--最大空閒鏈接數-->
    <property name="maxIdle" value="${redis.pool.maxIdle}" />
        <!--初始化鏈接數-->
    <property name="minIdle" value="${redis.pool.minIdle}"/>
        <!--最大等待時間-->
    <property name="maxWaitMillis" value="${redis.pool.maxWait}" />
        <!--對拿到的connection進行validateObject校驗-->
    <property name="testOnBorrow" value="${redis.pool.testOnBorrow}" />
        <!--在進行returnObject對返回的connection進行validateObject校驗-->
    <property name="testOnReturn" value="${redis.pool.testOnReturn}" />
        <!--定時對線程池中空閒的連接進行validateObject校驗-->
    <property name="testWhileIdle" value="false" />
  </bean>  
  <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"  
    p:host-name="${redis.host}" 
    p:port="${redis.port}" 
    p:password="${redis.pass}"  
    p:pool-config-ref="poolConfig"/> 
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg index="0" ref="poolConfig"/>
    <constructor-arg index="1" value="${redis.host}"/>
    <constructor-arg index="2" value="${redis.port}" type="int"/>
    <constructor-arg index="3" value="${redis.timeout}" type="int"/>
    <constructor-arg index="4" value="${redis.pass}"/>
  </bean> 
  <bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">  
    <property name="connectionFactory" ref="connectionFactory" />
  </bean>     
  <bean id="redisService" class="com.cn.api.utils.RedisService"></bean>
</beans>

七、jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.*.*:3306/vproject?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true
jdbc.username=root
jdbc.password=*
jdbc.initialSize=0
jdbc.maxActive=20
jdbc.maxIdle=20
jdbc.minIdle=3
jdbc.maxWait=60000
jdbc.validationQuery=select 1
jdbc.testOnBorrow=true

八、redis.preperties

redis.host=192.168.*.*
redis.port=6379
redis.pass=*
redis.database=0

redis.maxIdle=300
redis.maxActive=600
redis.maxWait=1000
redis.testOnBorrow=true
redis.timeout=1000

redis.pool.maxActive=200
redis.pool.maxIdle=50
redis.pool.minIdle=0
redis.pool.maxWait=15000
##向調用者輸出連接資源時,是否檢測是否有效,若是無效則從鏈接池中移除,嘗試繼續獲取,默認爲false,建議保留默認值
redis.pool.testOnBorrow=false
##向鏈接池「歸還」連接時,是否檢測「連接」對象的有效性。默認爲false。建議保持默認值.
redis.pool.testOnReturn=false
##maxActive: 連接池中最大鏈接數,默認爲8.
##maxIdle: 連接池中最大空閒的鏈接數,默認爲8.
##minIdle: 鏈接池中最少空閒的鏈接數,默認爲0.
##maxWait: 當鏈接池資源耗盡時,調用者最大阻塞的時間,超時將跑出異常。單位,毫秒數;默認爲-1.表示永不超時.
##minEvictableIdleTimeMillis: 鏈接空閒的最小時間,達到此值後空閒鏈接將可能會被移除。負值(-1)表示不移除。
##softMinEvictableIdleTimeMillis: 鏈接空閒的最小時間,達到此值後空閒連接將會被移除,且保留「minIdle」個空閒鏈接數。默認爲-1.
##numTestsPerEvictionRun: 對於「空閒連接」檢測線程而言,每次檢測的連接資源的個數。默認爲3.
##testOnBorrow: 向調用者輸出「連接」資源時,是否檢測是有有效,若是無效則從鏈接池中移除,並嘗試獲取繼續獲取。默認爲false。建議保持默認值.
##testOnReturn:  向鏈接池「歸還」連接時,是否檢測「連接」對象的有效性。默認爲false。建議保持默認值.
##testWhileIdle:  向調用者輸出「連接」對象時,是否檢測它的空閒超時;默認爲false。若是「連接」空閒超時,將會被移除。建議保持默認值.
##timeBetweenEvictionRunsMillis:  「空閒連接」檢測線程,檢測的週期,毫秒數。若是爲負值,表示不運行「檢測線程」。默認爲-1.
##whenExhaustedAction: 當「鏈接池」中active數量達到閥值時,即「連接」資源耗盡時,鏈接池須要採起的手段, 默認爲1:
## -> 0 : 拋出異常,
## -> 1 : 阻塞,直到有可用連接資源
## -> 2 : 強制建立新的連接資源

九、RedisService.java

見redis配置搭建博客
  • 1

這是本身開發過程當中的項目搭建,記錄一下,方便之後查閱!redis

版權聲明:本文爲博主原創文章,未經博主容許不得轉載。 https://blog.csdn.net/ishangxinyu/article/details/51881260spring

相關文章
相關標籤/搜索