SSM三大框架整合配置(Spring+SpringMVC+MyBatis)
阿新 • • 發佈:2018-02-26
lean source reat ati quest req 繼續 時間 per
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"> <!-- 定義Spring MVC的前端控制器 --> <servlet> <servlet-name>UserManage</servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/UserManage-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <!-- 讓Spring MVC的前端控制器攔截所有請求 --> <servlet-mapping> <servlet-name>UserManage</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- ================Spring配置開始================ --> <!-- 設置Spring容器加載配置文件的路徑 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/config/spring-config.xml</param-value> <!-- <param-value>classpath:/spring-*.xml</param-value> --> </context-param> <!-- 配置Spring監聽器,可以在容器啟動時,加載contextConfigLocation的context-param節點的配置文件 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- ================Spring配置結束================ --> <!-- 防止Spring內存溢出監聽器 --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- 指定首頁 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置SESSION超時,單位是分鐘 --> <session-config> <session-timeout>10</session-timeout> </session-config> <!--如下此段編碼設置必須在所有filter的前面,否則過濾器有可能不起作用。 設置編碼方式為UTF-8,解決中文亂碼問題。--> <filter> <filter-name>CharacterEncoding</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>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
UserManage-servlet.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 9 http://www.springframework.org/schema/mvc 10 http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 11 http://www.springframework.org/schema/context 12 http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 13 14 <!-- spring可以自動去掃描base-pack下面的包或者子包下面的java文件, 15 如果掃描到有Spring的相關註解的類,則把這些類註冊為Spring的bean --> 16 <context:component-scan base-package="org.usermanage.controller"/> 17 18 <!-- 配置處理映射器 --> 19 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> 20 21 <!-- 配置處理器適配器--> 22 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> 23 24 <!-- 視圖解析器 --> 25 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 26 <!--指定默認前綴,controller中直接寫文件名即可--> 27 <property name="prefix" value="/WEB-INF/content/jsp/"></property> 28 </bean> 29 30 <!--指定單獨處理的資源,不經過DispatcherServlet--> 31 <mvc:annotation-driven/> 32 33 <!--所有以html結尾的請求全部到指定目錄下查詢--> 34 <mvc:resources mapping="/*.html" location="WEB-INF/content/html/"></mvc:resources> 35 <mvc:resources mapping="/*.js" location="WEB-INF/content/js/"></mvc:resources> 36 <mvc:resources mapping="/*.css" location="WEB-INF/content/css/"></mvc:resources> 37 <mvc:resources mapping="/login/*.css" location="WEB-INF/content/css/login/"></mvc:resources> 38 39 40 </beans>
spring-config.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: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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 加載配置jdbc屬性文件 --> <!--MapperScannerConigurer實際是在解析加載bean定義階段的,這個時候要是設置sqlSessionFactory的話, PropertyPlaceholderConfigurer還沒來得及替換定義中的變量,導致把表達式當作字符串復制了 修改如下:--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>WEB-INF/config/jdbc.properties</value> <!--要是有多個配置文件,只需在這裏繼續添加即可 --> </list> </property> </bean> <!--配置c3p0連接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--註入屬性--> <property name="driverClass" value="${driver}"></property> <property name="jdbcUrl" value="${url}"></property> <property name="user" value="${username}"></property> <property name="password" value="${password}"></property> <!-- c3p0連接池的私有屬性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 關閉連接後不自動commit --> <property name="autoCommitOnClose" value="false"/> <!-- 獲取連接超時時間 --> <property name="checkoutTimeout" value="10000"/> <!-- 當獲取連接失敗重試次數 --> <property name="acquireRetryAttempts" value="2"/> </bean> <!--SqlSessionFactoryBean配置--> <bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!--Mybatis配置文件,非必須--> <property name="configLocation" value="WEB-INF/config/mybatis-config.xml"/> <property name="dataSource" ref="dataSource"/> <!--掃描mapper.xml文件的位置--> <property name="mapperLocations" value="classpath:org/usermanage/mapper/*.xml"/> </bean> <!-- 配置掃描Dao接口包,動態實現Dao接口,註入到spring容器中 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 註入sqlSessionFactory,單個dataSource時可省略 --> <!--<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"/>--> <!-- 給出需要掃描Dao接口包 --> <property name="basePackage" value="org.usermanage.mapper"/> </bean> <!--掃描所有使用註解的類型,使用Annotation自動註冊Bean--> <context:component-scan base-package="org.usermanage"/> <!-- 事務配置 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 使用annotation註解方式配置事務 --> <!--<tx:annotation-driven transaction-manager="transactionManager"/>--> </beans>
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> <!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值 --> <setting name="useGeneratedKeys" value="true"/> <!-- 使用列別名替換列名 默認:true --> <setting name="useColumnLabel" value="true"/> <!-- 開啟駝峰命名轉換:Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--從外部配置文件導入jdbc信息--> <!--<properties resource="config/jdbc.properties"></properties>--> <!--<environments default="development">--> <!--<environment id="development">--> <!--<transactionManager type="JDBC"/>--> <!--<dataSource type="POOLED">--> <!--<property name="driver" value="${driver}"/>--> <!--<property name="url" value="${url}"/>--> <!--<property name="username" value="${username}"/>--> <!--<property name="password" value="${password}"/>--> <!--</dataSource>--> <!--</environment>--> <!--</environments>--> <!--指定映射資源文件--> <!--<mappers>--> <!--<mapper resource="classpath:org/usermanage/mapper/UserMapper.xml"/>--> <!--</mappers>--> </configuration>
jdbc.properties
# jdbc連接信息 driver=com.mysql.jdbc.Driver #url=jdbc:mysql://192.168.184.130:3306/gxrdb url=jdbc:mysql://10.15.1.200:3306/gxrdb?useUnicode=true&characterEncoding=utf8 username=root password=root
SSM三大框架整合配置(Spring+SpringMVC+MyBatis)