環境:jdk1.八、tomcat 7.0.68 、mysql5.6mysql
配置及風格說明:spring使用的是4.3.5版本,restful風格接口。web
實現功能:1.實現簡單的增刪查改spring
2.事務配置(以實現事務回滾)sql
3.日誌配置,日誌使用的是log4j,日誌級別控制到debug級別,會打印出所執行的sql語句apache
下載地址:連接:https://pan.baidu.com/s/1nbJHul5m34bKVKPwZhOSwA spring-mvc
密碼:dxyptomcat
問題記錄及心得分享:restful
jar地址下載,1.單個jar可在:https://mvnrepository.com/ 中查詢下載mvc
2.spring整套jar下載:http://repo.spring.io/simple/libs-release-local/org/springframework/spring/ 可選擇所須要版本進行下載app
事務配置①在applicationContext.xml中配置
<!-- (事務管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" />
②在service層添加註解
注意:若此時事務不生效多是spring-mvc.xml中掃描的包不對。
日誌配置:①下載log4j的jar
②在web.xml中配置
<!-- log4j 配置 --> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:resource/log4j.properties</param-value> <!-- <param-value>/WEB-INF/log4j.xml</param-value> --> </context-param> <listener> <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> </listener>
③在resource中添加 log4j.properties(注意修改本身的dao層路徑)
log4j.rootLogger=INFO,Console,File #定義日誌輸出目的地爲控制檯 log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.Target=System.out #能夠靈活地指定日誌輸出格式,下面一行是指定具體的格式 log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH\:mm\:ss} [%5p] - %c -%F(%L) %m%n[%t] #文件大小到達指定尺寸的時候產生一個新的文件 log4j.appender.File = org.apache.log4j.RollingFileAppender #指定輸出目錄 log4j.appender.File.File = logs/mscard.log #定義文件最大大小 log4j.appender.File.MaxFileSize = 10MB # 輸出因此日誌,若是換成DEBUG表示輸出DEBUG以上級別日誌 log4j.appender.File.Threshold = ALL log4j.appender.File.layout = org.apache.log4j.PatternLayout log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n %t%c # MyBatis logging configuration... log4j.logger.com.lll.dao=DEBUG
注意:若是spring的版本選擇在5.0以上的將不能使用這種方式引用log4j,由於spring-web中沒有Log4jConfigListener這個方法