目錄
一、Spring配置
1、包掃描器
2、配置資料源
3、整合MyBatis
4、總體配置:
二、SpringMVC配置
1、包掃描器
2、視圖解析器
3、兩個标準配置
三、MyBatis配置
四、web.xml配置
1、Spring監聽器
2、Spring MVC轉發器
3、字元編碼過濾器
4、rest風格過濾器:
5、總體配置
五、日志配置:
一、Spring配置
建立applicationContext.xml
1、包掃描器
需要配置不需要掃描的注解:
<!-- 包掃描器 -->
<context:component-scan base-package="com.starfall.ssm">
<!-- 不掃描的注解 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
2、配置資料源
首先需要建立資料源properties檔案:
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8
mysql.username=root
mysql.password=root
使用c3p0資料連接配接池
<!-- 引入資料庫配置檔案 -->
<context:property-placeholder location="classpath:dbConfig.properties" />
<!-- 配置資料源 -->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${mysql.url}"></property>
<property name="driverClass" value="${mysql.driver}"></property>
<property name="user" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
</bean>
3、整合MyBatis
(1)sqlSessionFactory交由Spring管理
<!-- 整合MyBatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置資料源 ,引用上方資料源 -->
<property name="dataSource" ref="pooledDataSource"></property>
<!-- 指定mybatis全局配置檔案 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 指定mapper檔案的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
(2)事務控制
transactionManager交由Spring管理
<!-- 事務控制 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--配置控制的資料源,引用上方資料源 -->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
額外的事務配置:
<!-- 啟用事務注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--開啟基于注解的事務,使用xml配置形式的事務(必要主要的都是使用配置式) -->
<aop:config>
<!-- 切入點表達式: com.starfall.ssm.service包及其子包 -->
<aop:pointcut id="txPoint"
expression="execution(* com.starfall.ssm.service..*(..))" />
<!-- 配置事務增強 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>
<!--配置事務增強,事務如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有方法都是事務方法 -->
<tx:method name="*" />
<!--配置隻讀方法-->
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
</tx:attributes>
</tx:advice>
(3)掃描mapper接口
<!-- 掃描所有的mapper接口的實作,讓這些mapper能夠自動注入 -->
<!-- <mybatis-spring:scan base-package="com.starfall.ssm.dao" /> -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--掃描所有dao接口的實作,加入到ioc容器中 -->
<property name="basePackage" value="com.starfall.ssm.dao"></property>
</bean>
(4)配置一個可以進行批量執行的sqlSession
<!--配置一個可以進行批量執行的sqlSession-->
<bean id="sqlSessionBatch" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
4、總體配置:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 包掃描器 -->
<context:component-scan base-package="com.starfall.ssm">
<!-- 不掃描的注解 -->
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 引入資料庫配置檔案 -->
<context:property-placeholder location="classpath:dbConfig.properties" />
<!-- 配置資料源 -->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="${mysql.url}"></property>
<property name="driverClass" value="${mysql.driver}"></property>
<property name="user" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
</bean>
<!-- 整合MyBatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置資料源,引用上方資料源 -->
<property name="dataSource" ref="pooledDataSource"></property>
<!-- 指定mybatis全局配置檔案 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 指定mapper檔案的位置 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 事務控制 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--配置控制的資料源,引用上方資料源 -->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!-- 啟用事務注解 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!--開啟基于注解的事務,使用xml配置形式的事務(必要主要的都是使用配置式) -->
<aop:config>
<!-- 切入點表達式: com.starfall.ssm.service包及其子包 -->
<aop:pointcut id="txPoint"
expression="execution(* com.starfall.ssm.service..*(..))" />
<!-- 配置事務增強 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint" />
</aop:config>
<!--配置事務增強,事務如何切入 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 所有方法都是事務方法 -->
<tx:method name="*" />
<!--配置隻讀方法 -->
<tx:method name="get*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="select*" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 掃描所有的mapper接口的實作,讓這些mapper能夠自動注入 -->
<!-- <mybatis-spring:scan base-package="com.starfall.ssm.dao" /> -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--掃描所有dao接口的實作,加入到ioc容器中 -->
<property name="basePackage" value="com.starfall.ssm.dao"></property>
</bean>
<!--配置一個可以進行批量執行的sqlSession-->
<bean id="sqlSessionBatch" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
<constructor-arg name="executorType" value="BATCH"></constructor-arg>
</bean>
</beans>
二、SpringMVC配置
applicationContext-servlet.xml
1、包掃描器
隻配置掃描的注解
<!-- 包掃描器 -->
<context:component-scan base-package="com.starfall.ssm"
use-default-filters="false">
<!--隻掃描控制器。 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
2、視圖解析器
<!--配置視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
3、兩個标準配置
<!--兩個标準配置 -->
<!-- 将springmvc不能處理的請求交給tomcat -->
<mvc:default-servlet-handler />
<!-- 能支援springmvc更進階的一些功能,JSR303校驗,快捷的ajax...映射動态請求 -->
<mvc:annotation-driven />
4、總體配置
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 包掃描器 -->
<context:component-scan base-package="com.starfall.ssm"
use-default-filters="false">
<!--隻掃描控制器。 -->
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--配置視圖解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--兩個标準配置 -->
<!-- 将springmvc不能處理的請求交給tomcat -->
<mvc:default-servlet-handler />
<!-- 能支援springmvc更進階的一些功能,JSR303校驗,快捷的ajax...映射動态請求 -->
<mvc:annotation-driven />
</beans>
三、MyBatis配置
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>
<!-- 開啟自動駝峰命名規則(camel case)映射 -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 懶加載開啟 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 開啟任何方法的調用都會加載該對象的所有屬性。否則,每個屬性會按需加載 -->
<setting name="aggressiveLazyLoading" value="false" />
</settings>
</configuration>
四、web.xml配置
web.xml
1、Spring監聽器
<!-- Spring監聽器,啟動Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
2、Spring MVC轉發器
<!-- Spring MVC -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-servlet.xml</param-value>
</init-param>
<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>
<!-- request 編碼 -->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!-- response 編碼 -->
<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風格過濾器:
<!-- restful風格 -->
<filter>
<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>
5、總體配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<!-- Spring監聽器,啟動Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring MVC -->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字元編碼過濾器,一定要放在所有過濾器之前 -->
<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>
<!-- request 編碼 -->
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<!-- response 編碼 -->
<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>
<!-- restful風格 -->
<filter>
<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>
五、日志配置:
log4j.properties
配置詳解:https://blog.csdn.net/shaohe18362202126/article/details/84667631
# 配置根Logger
## level:DEBUG,appenderName:console和rollingFile
log4j.rootLogger=DEBUG,console,rollingFile
log4j.logger.com.starfall.ssm.dao=TRACE
# 配置Appender
## 配置Appender中的console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.rollingFile.Threshold=DEBUG
log4j.appender.rollingFile.ImmediateFlush=true
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %p [%c] - %m %n
## 配置Appender中的rollingFile
log4j.appender.rollingFile=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.Threshold=DEBUG
log4j.appender.rollingFile.ImmediateFlush=true
log4j.appender.rollingFile.Append=true
log4j.appender.rollingFile.File=D:/logs/ssm_log.log
log4j.appender.rollingFile.MaxFileSize=100MB
log4j.appender.rollingFile.MaxBackupIndex=1024
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.ConversionPattern=%d{yyyy/MM/dd HH:mm:ss} %p [%c] - %m %n