SSM+shiro,所有配置文件,詳細注釋版,自用


spring配置文件applicationContext.xml,放在resources下

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

    <!--Spring配置文件的核心點( 1、數據源    2、與mybatis的整合   3、事務控制 )-->

    <!--業務邏輯組件掃描進來-->
    <context:component-scan base-package="club.iashe">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

    <!--==================================================================================================================-->
    <!--數據源的配置-->
    <!--引入外部的配置文件-->
    <context:property-placeholder ignore-unresolvable="true" location="classpath:dbconfig.properties"/>

    <!-- 數據庫連接池c3p0配置數據源 -->
    <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>
        <property name="driverClass" value="${jdbc.driverClass}"/>
        <property name="user" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--==================================================================================================================-->
    <!--配置和mybatis整合-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定mybatis全局配置文件的位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <!--指定數據源-->
        <property name="dataSource" ref="pooledDataSource"/>
        <!--指定mybatis映射文件的位置-->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
    </bean>

    <!--配置掃描器,將mybatis接口的實現,即DAO接口所在包名,加入到IOC容器中-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--掃描所有dao接口的實現,加入到IOC容器中-->
        <property name="basePackage" value="club.iashe.dao"/>
    </bean>

    <!--配置一個可以執行批量的sqlSession-->
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" >
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <!--<constructor-arg name="executorType" value="BATCH" />-->
    </bean>

    <!--==================================================================================================================-->
    <!-- 事務控制的配置,spring聲明式事務 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--控制住數據源-->
        <property name="dataSource" ref="pooledDataSource"/>
    </bean>

    <!--開啟基於注解的事務/使用xml配置形式的事務(一般比較重要的都使用xml形式)-->
    <aop:config>
        <!--切入點表達式-->
        <aop:pointcut expression="execution(* club.iashe.service..*(..))" id="txPoint"/>
        <!--<aop:pointcut id="txPoint" expression="execution(* cn.crud.service..*(..))"/>-->
        <!--配置事務增強-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>

    <!--配置事務增強,也就是事務如何切入-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--代表這個切入點的所有方法都是事務方法-->
            <tx:method name="*"/>
            <!--以get開始的所有方法,進行調優-->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    <!--==================================================================================================================-->
</beans>

springMVC配置文件,dispatcherServlet-servlet.xml,放在WEB-INF下

<?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.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!--兩個標准配置-->
    <!--將springMVC不能處理的請求交給tomcat-->
    <mvc:default-servlet-handler />
    <!--能支持springMVC一些更高級的功能,注釋替代XML配置,jrs303校驗,快捷的ajax,映射動態請求-->
    <!--<mvc:annotation-driven />-->
    <!-- 使用fastjson替換springMVC默認的Jackson -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 配置fastjson支持 -->
            <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="defaultCharset" value="UTF-8" />
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/html;charset=UTF-8</value>
                        <value>application/json</value>
                    </list>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!--
        springMVC的配置文件,包含網站跳轉邏輯的控制、配置
        注釋改掉默認掃描所有use-default-filters,設為false
    -->
    <context:component-scan base-package="club.iashe" use-default-filters="false">
        <!--只掃描控制器-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

    <!-- 配置視圖解析器,方便頁面返回信息 -->
         <!-- 配置 HTML 視圖解析器 -->
             <!-- html視圖解析器,必須先配置freemarkerConfig,html沒有prefix屬性 -->
    <!--<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="resourceLoader">
            <value>/html/</value>
        </property>
    </bean>
    <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value="html" />
        <property name="order" value="0"/>
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>-->
        <!-- 配置 jsp 視圖解析器 -->
    <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <!--<property name="prefix" value="WEB-INF/views/" />       &lt;!&ndash;前綴&ndash;&gt;-->
        <property name="prefix" value="WEB-INF/admin/views/" />       <!--前綴-->
        <property name="suffix" value=".jsp" />                 <!--后綴-->
    </bean>


</beans>

mybatis配置文件,mybatis-config.xml,resources下

<?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>
        <!-- Globally enables or disables any caches configured in any mapper under this configuration -->
        <setting name="cacheEnabled" value="false"/>
        <!-- Sets the number of seconds the driver will wait for a response from the database -->
        <setting name="defaultStatementTimeout" value="5"/>
        <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn -->
        <!-- 數據庫下划線轉為駝峰 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <!-- Allows JDBC support for generated keys. A compatible driver is required.
        This setting forces generated keys to be used if set to true,
         as some drivers deny compatibility but still work -->
        <setting name="useGeneratedKeys" value="true"/>
    </settings>

    <!-- Continue editing here -->
    <!--起別名-->
    <typeAliases>
        <package name="club.iashe.pojo" />
    </typeAliases>

    <!--配置PageHelper插件-->
    <plugins>
        <!-- com.github.pagehelper為PageHelper類所在包名 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!-- 使用下面的方式配置參數,后面會有所有的參數介紹 -->
            <!--<property name="param1" value="value1"/>-->
            <!-- 分頁參數合理化 -->
            <property name="reasonable" value="true" />
        </plugin>
    </plugins>

</configuration>

mybatis-generator-config配置文件

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

    <!-- 引入配置文件 -->
    <properties resource="dbconfig.properties" />

    <context id="DB2Tables" targetRuntime="MyBatis3">

        <!--配置生成的增刪改查方法不要注釋-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>

        <!--配置數據庫鏈接信息-->
        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.jdbcUrl}"
                        userId="${jdbc.user}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!--java模型生成,指定JavaBean生成的位置-->
        <javaModelGenerator targetPackage="club.iashe.pojo"
                            targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
            <property name="trimStrings" value="true" />
        </javaModelGenerator>

        <!--指定sql映射文件的位置-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject=".\src\main\resources">
            <property name="enableSubPackages" value="true" />
        </sqlMapGenerator>

        <!--指定DAO接口生成的位置-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="club.iashe.dao"
                             targetProject=".\src\main\java">
            <property name="enableSubPackages" value="true" />
        </javaClientGenerator>

        <!--指定每個表的生成策略-->
        <!--<table tableName="resident_tag" domainObjectName="ResidentTag" />-->
        <table tableName="resident_info" domainObjectName="ResidentInfo" />

    </context>
</generatorConfiguration>

數據庫配置文件dbconfig.properties

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/community?serverTimezone=UTC&zeroDateTimeBehavior=round
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=

最后,web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- web.xml文件的配置 -->

    <!-- 1.啟動spring容器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <!-- spring配置文件路徑 -->
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- spring監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 2、springMVC的前端控制器,攔截所有請求 -->
    <servlet>
        <!-- 配置springMVC的dispatcherServlet分發請求,實際上它是一個前端控制器 -->
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <!--攔截所有頁面請求-->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--3、字符編碼過濾器,第一位的過濾器-->
    <filter>
        <filter-name>CharacterEncodingFilter</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>
        <init-param>
            <param-name>forceRequestEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>forceResponseEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 4、使用rest風格的URI,將頁面普通的post請求轉為指定的delete或者put請求 -->
    <filter>
        <!-- hiddenHttpMethodFilter 可以把把post請求轉船成 put delete -->
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>HiddenHttpMethodFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM