SpringMvc+Spring+mybatis配置檔案

率性修道我心光明發表於2019-03-22

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">

    <!-- http://localhost:8080/shopping_ssm -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
  <display-name>Archetype Created Web Application</display-name>
  <!-- 1.自定義spring核心配置檔案 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>
  				 classpath:applicationContext.xml
  	</param-value>
  </context-param>
  <servlet> 
       <servlet-name>springMVC</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
       
       <!-- 自定義SpringMVC核心配置檔案  區域性 -->
       <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>
	             classpath:springMVC.xml
             </param-value>
       </init-param>
       
       <!-- 讓tomcat在啟動時就初始化DispatcherServlet例項 -->
       <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
       <servlet-name>springMVC</servlet-name>
       <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  
  
  <!--  5. 作為 Shiro整合的Spring的入口 -->
  <!--<filter>
        &lt;!&ndash; 這個名字不要隨便改, 後面要跟IoC容器中的bean對相應 &ndash;&gt;
        <filter-name>shiroFilter</filter-name>
        
        &lt;!&ndash; 預設這個filter的生命週期由IoC容器管理 (false), 現在我們需要將DelegatingFilterProxy的生命週期交由web容器tomcat管理 &ndash;&gt;
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        
        <init-param>
             <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>-->
  
  
  
  <!--3. spring 的 listener -->
   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
 <!-- 4. Post請求字元編碼過濾器 -->
  <filter>
        <filter-name>character</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
      <init-param>
          <!-- encoding是CharacterEncodingFilter類中的屬性,不能寫錯 -->
          <param-name>encoding</param-name>
          <param-value>utf-8</param-value>
      </init-param>
  </filter>
  <filter-mapping>
        <filter-name>character</filter-name>
        <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  
</web-app>
複製程式碼

* applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
                       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
                       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                       ">



     <context:property-placeholder location="classpath:*.properties"/>

     <context:component-scan base-package="com.zte.shopping.service.impl"/>
    <!-- 3. 配置DataSource -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}"  />

        <property name="initialSize" value="5" />
        <property name="maxActive" value="10" />
    </bean>


  <!--mybatis-->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="typeAliasesPackage" value="com.zte.shopping.entity"/>
      <property name="mapperLocations">
           <list>
                <value>classpath:mapper/*Mapper.xml</value>
           </list>
      </property>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
  </bean>
  
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     <property name="basePackage" value="com.zte.shopping.dao"/>
   <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
  </bean>

`   <!--配置通知-->`
    <!--配置事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>


    <tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="regist" propagation="REQUIRED" rollback-for="java.lang.Exception" />
            <tx:method name="*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <aop:pointcut expression="execution(* com.zte.shopping.service.impl.*ServiceImpl.*(..))" id="pc"/>
        <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="pc" />
    </aop:config>
</beans>

複製程式碼

* springMVC.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans 
     xmlns="http://www.springframework.org/schema/beans" 
     xmlns:mvc="http://www.springframework.org/schema/mvc"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="
                         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
                         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                        ">
     
     
     <!-- Spring工作流程的配置   掃描到包名 -->

     <context:component-scan base-package="com.zte.shopping.controller" />
    <!--<context:component-scan base-package="java.com.zte.shopping.global" />-->
     
     <mvc:annotation-driven />


     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"  />
          <property name="prefix" value="/WEB-INF/view/" />
          <property name="suffix" value=".jsp" />
     </bean>
    
    <!-- 訪問靜態資源  default  or   WebRoot/js/jquery-1.9.1.min.js -->
    <!--<mvc:resources location="" mapping="js/**" />-->
    <mvc:default-servlet-handler/>
    <!--<mvc:resources mapping="/images/**" location="/images/**"/>-->
   <!-- <mvc:resources mapping="/js/**" location="/js/**" />
    <mvc:resources mapping="/css/**" location="/css/**" />-->



    <!--<mvc:view-controller path="/showMain" view-name="backend/main" />-->

    <!-- http://localhost:8080/shopping_ssm/showLogin  跳轉到 /WEB-INF/pages/backend/login.jsp 後臺登入首頁-->
    <!--<mvc:view-controller path="/showLogin" view-name="backend/login" />-->
    <mvc:view-controller path="/showMain" view-name="main" />
</beans>
複製程式碼

* applicationContext_shiro.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans 
    xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    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-4.2.xsd
                       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
                       http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd
                       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
                       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
                       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
                       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
                       ">
  
   
   <!-- Shiro的配置 -->
	<!-- Shiro的配置 -->

	<!--<context:component-scan base-package="service.impl" />-->
    <!--????掃描-->
    
    <!-- 1.配置Shiro核心 SecurityManager -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
          <!-- 注入多個jdbcRealm -->
          <property name="realms">
                <list>
                      <ref bean="usernameRealm" />
                      <ref bean="phoneRealm" />
                      <ref bean="emailRealm" />
                </list>
          </property>
    </bean>
    
    <!-- 2. 用於處理Shiro中登入驗證 角色驗證  許可權驗證:ShiroFilterFactoryBean -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    
        <!-- 2.1 注入securityManager -->
        <property name="securityManager" ref="securityManager" />
        
        <!-- <property name="loginUrl" value="index.jsp" /> -->
        
        <!-- 沒有驗證統一進入/showLogin指定的頁面 -->
        <property name="loginUrl" value="/showLogin" />
        
        <!-- 未授權 跳轉的頁面 -->
        <property name="unauthorizedUrl" value="/unauthorizedUrl.jsp" />
        
     
        
        <!-- 2.2 注入驗證規則(做 登入驗證 or 角色驗證 or 許可權驗證) -->
        <property name="filterChainDefinitions">
             <value>
	             <!-- 配置登入驗證  登入頁面 和 登入功能 不需要驗證      首頁/main需要驗證-->
	             /showLogin=anon
	             /login=anon
	             /main=authc
	              
	             /student/view=authc,roles[student],perms[student:view]
	             /student/delete=authc,roles[student],perms[student:delete]
	            
	             /student/**=authc,roles[student]
	             /teacher/**=authc,roles[teacher]
	             
             </value>
        </property>
    </bean>
    
    <!--  3. 配置jdbcRealm -->
    <!-- 支援  使用者名稱 登入 -->
    <bean id="usernameRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
	     <property name="dataSource" ref="dataSource" />
	     
	     <!-- 登入驗證 -->
	     <property name="authenticationQuery">
	            <value>select password from t_user where username = ?</value>
	     </property>
	     
	     <!--  角色驗證 -->
	     <property name="userRolesQuery">
	          <value>
			    select
				   r.rolename
				from  
				   t_user        u
				left join 
				   t_user_role   ur
				on
				   u.id = ur.user_id
				left join
				   t_roles   r
				on
				   r.id = ur.role_id
				where 
				   username = ?
			 </value>
	     </property>
	     
	     <!-- 許可權驗證 -->
	     <!-- 開啟許可權驗證 -->
	     <property name="permissionsLookupEnabled" value="true" />
	     <!-- 自定義許可權驗證 -->
	     <property name="permissionsQuery">
	          <value>
		            select
					   *
					from
					   t_roles r
					left join
					   t_role_permission rp
					on
					   r.id = rp.role_id
					left join
					   t_permissions  p
					on
					   p.id = rp.permisson_id
					where
					   r.rolename = ?
	          </value>
	     </property>
    </bean>
    
    <!-- 支援 手機號 登入 -->
    <bean id="phoneRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
	    <!-- <property name="dataSource" ref="dataSource" />-->
	     <property name="authenticationQuery">
	            <value>select password from t_user where phone = ?</value>
	     </property>
	     
	     <!--  角色驗證 -->
	     <property name="userRolesQuery">
	          <value>
			    select
				   r.rolename
				from  
				   t_user        u
				left join 
				   t_user_role   ur
				on
				   u.id = ur.user_id
				left join
				   t_roles   r
				on
				   r.id = ur.role_id
				where 
				   username = ?
			 </value>
	     </property>
	     
	     <!-- 許可權驗證 -->
	     <!-- 開啟許可權驗證 -->
	     <property name="permissionsLookupEnabled" value="true" />
	     <!-- 自定義許可權驗證 -->
	     <property name="permissionsQuery">
	          <value>
		            select
					   *
					from
					   t_roles r
					left join
					   t_role_permission rp
					on
					   r.id = rp.role_id
					left join
					   t_permissions  p
					on
					   p.id = rp.permisson_id
					where
					   r.rolename = ?
	          </value>
	     </property>
    </bean>
    
    <!-- 支援 郵箱 登入 -->
    <bean id="emailRealm" class="org.apache.shiro.realm.jdbc.JdbcRealm">
	     <property name="dataSource" ref="dataSource" />
	     <property name="authenticationQuery">
	            <value>select password from t_user where email = ?</value>
	     </property>
	     
	     <!--  角色驗證 -->
	     <property name="userRolesQuery">
	          <value>
			    select
				   r.rolename
				from  
				   t_user        u
				left join 
				   t_user_role   ur
				on
				   u.id = ur.user_id
				left join
				   t_roles   r
				on
				   r.id = ur.role_id
				where 
				   username = ?
			 </value>
	     </property>
	     
	     <!-- 許可權驗證 -->
	     <!-- 開啟許可權驗證 -->
	     <property name="permissionsLookupEnabled" value="true" />
	     <!-- 自定義許可權驗證 -->
	     <property name="permissionsQuery">
	          <value>
		            select
					   *
					from
					   t_roles r
					left join
					   t_role_permission rp
					on
					   r.id = rp.role_id
					left join
					   t_permissions  p
					on
					   p.id = rp.permisson_id
					where
					   r.rolename = ?
	          </value>
	     </property>
    </bean>
</beans>
複製程式碼

* dataSource.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/shoppingssm?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root
複製程式碼

* log4j.properties

log4j.appender.myconsole=org.apache.log4j.ConsoleAppender


#log4j.appender.myconsole.layout=org.apache.log4j.SimpleLayout
log4j.appender.myconsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myconsole.layout.ConversionPattern=%-5p %m%n

log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.PreparedStatemant=DEBUG

log4j.rootLogger=debug,myconsole
複製程式碼

* 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>
	
	<plugins>
		<!-- 配置mybatis分頁外掛 -->
	    <!-- com.github.pagehelper為PageHelper類所在包名  只能是4.0版本???-->
	    <plugin interceptor="com.github.pagehelper.PageHelper">
	    
	        <!-- 4.0.0以後版本可以不設定該引數 -->
	        <property name="dialect" value="mysql"/>
	        
	        <!-- 該引數預設為false -->
	        <!-- 設定為true時,會將RowBounds第一個引數offset當成pageNum頁碼使用 -->
	        <!-- 和startPage中的pageNum效果一樣-->
	        <property name="offsetAsPageNum" value="true"/>
	        
	        
	        <!-- 該引數預設為false -->
	        <!-- 設定為true時,使用RowBounds分頁會進行count查詢 -->
	        <property name="rowBoundsWithCount" value="true"/>
	        
	        
	        <!-- 設定為true時,如果pageSize=0或者RowBounds.limit = 0就會查詢出全部的結果 -->
	        <!-- (相當於沒有執行分頁查詢,但是返回結果仍然是Page型別)-->
	        <property name="pageSizeZero" value="true"/>
	        
	        <!-- 3.3.0版本可用 - 分頁引數合理化,預設false禁用 -->
	        <!-- 啟用合理化時,如果pageNum<1會查詢第一頁,如果pageNum>pages會查詢最後一頁 -->
	        <!-- 禁用合理化時,如果pageNum<1或pageNum>pages會返回空資料 -->
	        <property name="reasonable" value="true"/>
	        
	        <!-- 3.5.0版本可用 - 為了支援startPage(Object params)方法 -->
	        <!-- 增加了一個params引數來配置引數對映,用於從Map或ServletRequest中取值 -->
	        <!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,orderBy,不配置對映的用預設值 -->
	        <!-- 不理解該含義的前提下,不要隨便複製該配置 -->
	        <property name="params" value="pageNum=pageHelperStart;pageSize=pageHelperRows;"/>
	        
	        <!-- 支援通過Mapper介面引數來傳遞分頁引數 -->
	        <property name="supportMethodsArguments" value="true"/>
	        
	        <!-- 總是返回PageInfo型別-->
	        <property name="returnPageInfo" value="true"/>
	    </plugin>
	</plugins>
	
</configuration>

複製程式碼

相關文章