1. 程式人生 > >maven下整合SSM的檔案配置,包含mybatis外掛使用,詳細註釋版,拿來即用

maven下整合SSM的檔案配置,包含mybatis外掛使用,詳細註釋版,拿來即用

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>