Spring+SpringMVC+mybatis+redis(一)

SSM框架加redis整合韭邓,

我也是一個碼農(nóng)措近,都是搬代碼,這搬一點女淑,那搬一點瞭郑。。鸭你。這回分享分享凰浮,一個挺好的架構(gòu)我抠。。
這是一個maven項目,不廢話袜茧,直接上

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.tyro.ssm</groupId>
  <artifactId>mavenssm</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenssm Maven Webapp</name>
  <url>http://maven.apache.org</url>
  
    <properties>
        <!-- spring版本號 -->
        <spring.version>4.2.2.RELEASE</spring.version>
        <!-- mybatis版本號 -->
        <mybatis.version>3.3.0</mybatis.version>
        <!-- log4j日志文件管理包版本 -->
        <slf4j.version>1.7.7</slf4j.version>
        <log4j.version>1.2.17</log4j.version>
        <json.version>2.6.3</json.version>
        <shiro.version>1.2.3</shiro.version>
        <twoCode.version>2.0</twoCode.version>
    </properties>
 
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <!-- 當沒有添加Tomcat 時,jsp文件會有紅叉 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
        <!-- 只在編譯時和測試時運行 -->
        <scope>provided</scope>
    </dependency>
    <!-- 導入java ee jar 包 -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>
    <!-- spring核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
 
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!-- mybatis核心包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!-- 和spring aop有關的-->
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.6</version>
        </dependency>
        <!-- mybatis/spring包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.2</version>
        </dependency>
         <!-- 導入Mysql數(shù)據(jù)庫鏈接jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.30</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/druid
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.26</version>
        </dependency>
          --> 
        <!-- 導入dbcp的jar包瓣窄,用來在applicationContext.xml中配置數(shù)據(jù)庫  -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
       
        <!-- JSTL標簽類 -->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- 日志文件管理包 -->
        <!-- log start -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
         <!-- 格式化對象笛厦,方便輸出日志 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.1.41</version>
        </dependency>
        <!-- 上傳組件包 -->
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>
        <!--  連接池  -->
        <dependency>
          <groupId>commons-pool</groupId>
          <artifactId>commons-pool</artifactId>
          <version>1.6</version>
        </dependency>
      
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.2</version>
        </dependency>
        <!-- json -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>${json.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>${json.version}</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>${json.version}</version>
        </dependency>
        
        <!-- 二維碼 -->
        <dependency>
            <groupId>com.google.zxing</groupId>
            <artifactId>core</artifactId>
            <version>${twoCode.version}</version>
        </dependency>
        
        <!--  
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-mapper-asl</artifactId>
            <version>1.7.1</version>
        </dependency>
        <dependency>
            <groupId>org.codehaus.jackson</groupId>
            <artifactId>jackson-core-asl</artifactId>
            <version>1.7.1</version>
        </dependency>
        -->
        <!-- shiro -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-all</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>5.2.2.Final</version>
        </dependency>   
        <!-- redis begin
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>1.6.2.RELEASE</version>
        </dependency>
        -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.4.2</version>
        </dependency>
        <!-- redis over -->
  </dependencies>
  
  <build>
    <finalName>mavenssm</finalName>
    <plugins>  
    <plugin>  
        <groupId>org.apache.maven.plugins</groupId>  
        <artifactId>maven-compiler-plugin</artifactId>  
        <version>2.3.2</version>  
        <configuration>  
            <source>1.8</source>  
            <target>1.8</target>  
        </configuration>  
    </plugin>  
</plugins>  
  </build>
</project>

上面是我的一個demo,可能有些包俺夕,如不必要可以自己去掉

2.sping-mvc.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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aopz
http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 啟用spring mvc 注解 
    <context:annotation-config />
    -->
    <!-- 啟動@controll注解 -->
    <mvc:annotation-driven />
    <!-- 將靜態(tài)資源的處理經(jīng)由Spring MVC框架交回Web應用服務器處理裳凸。 -->
    <mvc:default-servlet-handler/>
    <!-- 自動掃描 -->
    <context:component-scan base-package="com.lrs.controller"/>
    
    <!-- 訪問靜態(tài)資源 -->
    <mvc:resources mapping="/static/**" location="/,/static/" />
    
    <!-- 請求攔截
 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**/**"/>
             <bean class="com.lrs.interceptor.MyHandlerInterceptor">
                <property name="needLoginUrl" value=".*/((web)|(toHome)|(page)).*"></property>
                <property name="resourceUrl" value=".*/((resources)|(static)).*"></property>
             </bean>
        </mvc:interceptor>
    </mvc:interceptors>
    
    <!-- json 配置,避免IE執(zhí)行AJAX時,返回JSON出現(xiàn)下載文件
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="false">
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>      
    </mvc:annotation-driven>
     -->
 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

3.spring-mybatis.xml

<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:p="http://www.springframework.org/schema/p"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  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/aop 
    http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context.xsd">

     <!-- 啟用注解 -->
    <context:annotation-config/>
    <context:component-scan base-package="com.lrs"/>
    
    <context:property-placeholder location="classpath:config/config-*.properties" />
    <!-- redis 配置  是不需要spring-data-redis的  -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">  
        <property name="maxTotal" value="${redis.maxTotal}" />  
        <property name="maxIdle" value="${redis.maxIdle}" />  
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />  
        <property name="testOnReturn" value="${redis.testOnReturn}" />
        <property name="numTestsPerEvictionRun" value="1024" />  
        <property name="timeBetweenEvictionRunsMillis" value="30000" />  
        <property name="minEvictableIdleTimeMillis" value="-1" />  
        <property name="softMinEvictableIdleTimeMillis" value="10000" />  
        <property name="maxWaitMillis" value="10000" />  
        <property name="testWhileIdle" value="true" />  
        <property name="jmxEnabled" value="true" />  
        <property name="jmxNamePrefix" value="youyuan" />  
        <property name="blockWhenExhausted" value="false" />   
    </bean>  
      <!--  -->
    <bean id="shardedJedisPool" class="redis.clients.jedis.ShardedJedisPool" scope="singleton">
        <constructor-arg index="0" ref="jedisPoolConfig"/>
        <constructor-arg index="1">
            <list>
                <bean class="redis.clients.jedis.JedisShardInfo">
                    <constructor-arg name="host" value="${redis.host}"></constructor-arg>
                    <constructor-arg name="port" value="${redis.port}"></constructor-arg>
                    <constructor-arg name="timeout" value="${redis.timeout}"></constructor-arg>
                    <constructor-arg name="weight" value="1"></constructor-arg>
                </bean>
            </list>
        </constructor-arg>
    </bean> 
  
    <!-- 配置數(shù)據(jù)源 秘蛇,dbcp  -->
    <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="maxActive" value="30" />
        <property name="maxIdle" value="5" />
    </bean>
    
     
    <!-- 阿里 druid數(shù)據(jù)庫連接池 
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">  
         <property name="url" value="${jdbc.url}" />  
         <property name="username" value="${jdbc.username}" />  
         <property name="password" value="${jdbc.password}" />  
         <property name="driverClassName" value="jdbc.driver" />  
         <property name="filters" value="${jdbc.filters}" />  
         <property name="maxActive" value="${jdbc.maxActive}" />
         <property name="initialSize" value="${jdbc.initialSize}" />
         <property name="maxWait" value="${jdbc.maxWait}" />
         <property name="minIdle" value="${jdbc.minIdle}" />  
         <property name="timeBetweenEvictionRunsMillis" value="${jdbc.timeBetweenEvictionRunsMillis}" />
         <property name="minEvictableIdleTimeMillis" value="${jdbc.minEvictableIdleTimeMillis}" />  
         <property name="validationQuery" value="${jdbc.validationQuery}" />  
         <property name="testWhileIdle" value="${jdbc.testWhileIdle}" />  
         <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />  
         <property name="testOnReturn" value="${jdbc.testOnReturn}" />  
         <property name="maxOpenPreparedStatements" value="${jdbc.maxOpenPreparedStatements}" />
         <property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
         <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
         <property name="logAbandoned" value="${jdbc.logAbandoned}" />
    </bean>
    -->  
    
    <!-- DAO接口所在包名兴革,Spring會自動查找其下的類 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.lrs.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    
    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數(shù)據(jù)庫連接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加載mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:config/mybatis/mybatis-config.xml" />
        <!-- mapper掃描 -->
        <property name="mapperLocations" value="classpath:com/lrs/mapper/*.xml"></property>
    </bean>
    
    <!-- 通過Spring進行事務的管理氧枣,我們需要增加Spring注解的事務管理機制 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>   
    
    <!--配置事務通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 
                傳播行為 
                    * propagation   :事務的傳播行為
                    * isolation     :事務的隔離級別
                    * read-only     :只讀
                    * rollback-for  :發(fā)生哪些異沉耄回滾事務
                    * no-rollback-for   :發(fā)生哪些異常不回滾事務
            -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <!-- 配置事務切面
     --> 
    <aop:config proxy-target-class="true">
        <!-- 配置切入點 -->
        <aop:pointcut id="serviceOperation" expression="execution(* com.tyro.service..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>
    
    <!-- shiro -->
       
    <!-- Shiro默認會使用Servlet容器的Session,可通過sessionMode屬性來指定使用Shiro原生Session -->  
    <!-- 即<property name="sessionMode" value="native"/>,詳細說明見官方文檔 -->  
    <!-- 這里主要是設置自定義的單Realm應用,若有多個Realm,可使用'realms'屬性代替 -->  
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">  
        <property name="realm" ref="myRealm"/>  
    </bean>
    
    <!-- 繼承自AuthorizingRealm的自定義Realm,即指定Shiro驗證用戶登錄的類為自定義的ShiroDbRealm.java -->  
    <bean id="myRealm" class="com.lrs.interceptor.MyShiroRealm"/>  
       
    <!-- Shiro主過濾器本身功能十分強大,其強大之處就在于它支持任何基于URL路徑表達式的罚渐、自定義的過濾器的執(zhí)行 -->  
    <!-- Web應用中,Shiro可控制的Web請求必須經(jīng)過Shiro主過濾器的攔截,Shiro對基于Spring的Web應用提供了完美的支持 -->  
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">  
        <!-- Shiro的核心安全接口,這個屬性是必須的 -->  
        <property name="securityManager" ref="securityManager"/>  
        <!-- 要求登錄時的鏈接(可根據(jù)項目的URL進行替換),非必須的屬性,默認會自動尋找Web工程根目錄下的"/login.jsp"頁面 -->  
        <property name="loginUrl" value="/"/>  
        <!-- 登錄成功后要跳轉(zhuǎn)的連接(本例中此屬性用不到,因為登錄成功后的處理邏輯在LoginController里硬編碼為main.jsp了) -->  
        <!-- <property name="successUrl" value="/system/main"/> -->  
        <!-- 用戶訪問未對其授權的資源時,所顯示的連接 -->  
        <!-- 若想更明顯的測試此屬性可以修改它的值,如unauthor.jsp,然后用[玄玉]登錄后訪問/admin/listUser.jsp就看見瀏覽器會顯示unauthor.jsp -->  
        <property name="unauthorizedUrl" value="/"/>  
        <!-- Shiro連接約束配置,即過濾鏈的定義 -->  
        <!-- 此處可配合我的這篇文章來理解各個過濾連的作用http://blog.csdn.net/jadyer/article/details/12172839 -->  
        <!-- 下面value值的第一個'/'代表的路徑是相對于HttpServletRequest.getContextPath()的值來的 -->  
        <!-- anon:它對應的過濾器里面是空的,什么都沒做,這里.do和.jsp后面的*表示參數(shù),比方說login.jsp?main這種 -->  
        <!-- authc:該過濾器下的頁面必須驗證后才能訪問,它是Shiro內(nèi)置的一個攔截器org.apache.shiro.web.filter.authc.FormAuthenticationFilter --> 
        <!-- 
             /admins/**=anon             無參,表示可匿名使用,可以理解為匿名用戶或游客 
             /admins/user/**=authc       無參,表示需認證才能使用 
             /admins/user/**=authcBasic  無參,表示httpBasic認證 
             /admins/user/**=user        無參,表示必須存在用戶,當?shù)侨氩僮鲿r不做檢查 
             /admins/user/**=ssl         無參,表示安全的URL請求,協(xié)議為https 
         --> 
        <property name="filterChainDefinitions">  
            <value>  
                 /static/**         =anon  
                 /login             =anon  
                 /code              =anon  
                 /toTwoCode         =anon  
                 /twoCode           =anon  
                 /test              =anon  
                 /app**/**          =anon 
                 /web/**            =anon  
                 /**                =authc
            </value>  
        </property>
    </bean>  
    
</beans>

4.config-db.properties 和config-redis.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useUnicode=true
jdbc.username=tyro
jdbc.password=tyro

jdbc.filters=stat
jdbc.maxActive=20
jdbc.initialSize=1
jdbc.maxWait=60000
jdbc.minIdle=10
jdbc.maxIdle=15
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.minEvictableIdleTimeMillis=30000  
jdbc.validationQuery=SELECT 'x'
jdbc.testWhileIdle=true
jdbc.testOnBorrow=false
jdbc.testOnReturn=false
jdbc.maxOpenPreparedStatements=20
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=1800
jdbc.logAbandoned=true


#redis
# Redis settings  
redis.host=127.0.0.1
redis.port=6379  
redis.pass=redis 
redis.timeout=2000
  
redis.maxTotal=50  
redis.maxIdle=7 
redis.maxWait=1000  
redis.testOnBorrow=true  
redis.testOnReturn=true  

5.mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    
    <settings> 
        <setting name="cacheEnabled" value="true" /><!-- 全局映射器啟用緩存 -->   
        <setting name="useGeneratedKeys" value="true" /> 
        <setting name="defaultExecutorType" value="REUSE" />
        <setting name="callSettersOnNulls" value="true"/>
    </settings>

    <typeAliases>
        <typeAlias type="com.lrs.util.ParameterMap" alias="pm"/>
        <!-- 分頁 -->
        <typeAlias type="com.lrs.plugin.Page" alias="Page"/>
    </typeAliases>
    
    <plugins>
        <plugin interceptor="com.lrs.plugin.PagePlugin">
            <property name="dialect" value="mysql"/>
            <property name="pageSqlId" value=".*Page.*"/>
        </plugin>
    </plugins>

</configuration>

6.web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>SSMDemo</display-name>
  
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>ssm.root</param-value>
    </context-param>
  <!-- 加載spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:config/spring/spring-mybatis.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:config/log4j.properties</param-value>
    </context-param>
    <!-- 加載log4j配置 -->
    <listener>
        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
    </listener>
    
    <filter>
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <!-- 配置Shiro過濾器,先讓Shiro過濾系統(tǒng)接收到的請求 -->  
    <!-- 這里filter-name必須對應applicationContext.xml中定義的<bean id="shiroFilter"/> -->  
    <!-- 使用[/*]匹配所有請求,保證所有的可控請求都經(jīng)過Shiro的過濾 -->  
    <!-- 通常會將此filter-mapping放置到最前面(即其他filter-mapping前面),以保證它是過濾器鏈中第一個起作用的 -->  
    <filter>  
        <filter-name>shiroFilter</filter-name>  
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
        <init-param>  
        <!-- 該值缺省為false,表示生命周期由SpringApplicationContext管理,設置為true則表示由ServletContainer管理 -->  
        <param-name>targetFilterLifecycle</param-name>  
        <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
            <filter-name>shiroFilter</filter-name>  
            <url-pattern>/*</url-pattern>  
    </filter-mapping>
    
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:config/spring/spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
  
  <error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/jsp/404.jsp</location>
  </error-page>
</web-app>

7.RedisDataSource類

package com.lrs.redis;

import redis.clients.jedis.ShardedJedis;

public interface RedisDataSource {
    public abstract ShardedJedis getRedisClient();
    public void returnResource(ShardedJedis shardedJedis);
    public void returnResource(ShardedJedis shardedJedis,boolean broken);
}

8.RedisDataSourceImpl類

package com.lrs.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.tyro.util.MyLogger;

import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

@Repository("redisDataSource")
public class RedisDataSourceImpl implements RedisDataSource {
    protected MyLogger log = MyLogger.getLogger(this.getClass());
    
    @Autowired
    private ShardedJedisPool  shardedJedisPool;

    public ShardedJedis getRedisClient() {
        try {
            System.out.println("shardedJedisPool="+shardedJedisPool);
            ShardedJedis shardJedis = shardedJedisPool.getResource();
            log.info("============獲取Redis===========");
            return shardJedis;
        } catch (Exception e) {
            log.error("getRedisClent error", e);
        }
        return null;
    }

    public void returnResource(ShardedJedis shardedJedis) {
        log.info("============釋放資源===========");
        shardedJedisPool.returnResource(shardedJedis);
    }

    public void returnResource(ShardedJedis shardedJedis, boolean broken) {
        if (broken) {
            shardedJedisPool.returnBrokenResource(shardedJedis);
        } else {
            shardedJedisPool.returnResource(shardedJedis);
        }
    }
}

不夠?qū)懥耍?a href="http://www.reibang.com/p/e9de4a22a1ab" target="_blank">下一章繼續(xù)

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末迅耘,一起剝皮案震驚了整個濱河市税娜,隨后出現(xiàn)的幾起案子月洛,更是在濱河造成了極大的恐慌件甥,老刑警劉巖捌议,帶你破解...
    沈念sama閱讀 206,214評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異引有,居然都是意外死亡瓣颅,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,307評論 2 382
  • 文/潘曉璐 我一進店門譬正,熙熙樓的掌柜王于貴愁眉苦臉地迎上來宫补,“玉大人,你說我怎么就攤上這事曾我》叟拢” “怎么了?”我有些...
    開封第一講書人閱讀 152,543評論 0 341
  • 文/不壞的土叔 我叫張陵您单,是天一觀的道長斋荞。 經(jīng)常有香客問我,道長虐秦,這世上最難降的妖魔是什么平酿? 我笑而不...
    開封第一講書人閱讀 55,221評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮悦陋,結(jié)果婚禮上蜈彼,老公的妹妹穿的比我還像新娘。我一直安慰自己俺驶,他們只是感情好幸逆,可當我...
    茶點故事閱讀 64,224評論 5 371
  • 文/花漫 我一把揭開白布棍辕。 她就那樣靜靜地躺著,像睡著了一般还绘。 火紅的嫁衣襯著肌膚如雪楚昭。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,007評論 1 284
  • 那天拍顷,我揣著相機與錄音抚太,去河邊找鬼。 笑死尿贫,一個胖子當著我的面吹牛庆亡,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播捞稿,決...
    沈念sama閱讀 38,313評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼括享!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,956評論 0 259
  • 序言:老撾萬榮一對情侶失蹤娇斩,失蹤者是張志新(化名)和其女友劉穎仁卷,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體犬第,經(jīng)...
    沈念sama閱讀 43,441評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡锦积,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,925評論 2 323
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了歉嗓。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丰介。...
    茶點故事閱讀 38,018評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖鉴分,靈堂內(nèi)的尸體忽然破棺而出哮幢,到底是詐尸還是另有隱情,我是刑警寧澤志珍,帶...
    沈念sama閱讀 33,685評論 4 322
  • 正文 年R本政府宣布橙垢,位于F島的核電站,受9級特大地震影響伦糯,放射性物質(zhì)發(fā)生泄漏柜某。R本人自食惡果不足惜嗽元,卻給世界環(huán)境...
    茶點故事閱讀 39,234評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望喂击。 院中可真熱鬧剂癌,春花似錦、人聲如沸惭等。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,240評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽辞做。三九已至,卻和暖如春寡具,著一層夾襖步出監(jiān)牢的瞬間秤茅,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,464評論 1 261
  • 我被黑心中介騙來泰國打工童叠, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留框喳,地道東北人。 一個月前我還...
    沈念sama閱讀 45,467評論 2 352
  • 正文 我出身青樓厦坛,卻偏偏與公主長得像五垮,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子杜秸,可洞房花燭夜當晚...
    茶點故事閱讀 42,762評論 2 345

推薦閱讀更多精彩內(nèi)容