1创建web动态项目,项目结构截图
2.配置日志文件
1 #\u5B9A\u4E49LOG\u8F93\u51FA\u7EA7\u522B 2 log4j.rootLogger=INFO,Console,File 3 #\u5B9A\u4E49\u65E5\u5FD7\u8F93\u51FA\u76EE\u7684\u5730\u4E3A\u63A7\u5236\u53F0 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender 5 log4j.appender.Console.Target=System.out 6 #\u53EF\u4EE5\u7075\u6D3B\u5730\u6307\u5B9A\u65E5\u5FD7\u8F93\u51FA\u683C\u5F0F\uFF0C\u4E0B\u9762\u4E00\u884C\u662F\u6307\u5B9A\u5177\u4F53\u7684\u683C\u5F0F 7 log4j.appender.Console.layout = org.apache.log4j.PatternLayout 8 log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n 9 10 #\u6587\u4EF6\u5927\u5C0F\u5230\u8FBE\u6307\u5B9A\u5C3A\u5BF8\u7684\u65F6\u5019\u4EA7\u751F\u4E00\u4E2A\u65B0\u7684\u6587\u4EF6 11 log4j.appender.File = org.apache.log4j.RollingFileAppender 12 #\u6307\u5B9A\u8F93\u51FA\u76EE\u5F55 13 log4j.appender.File.File = logs/ssm.log 14 #\u5B9A\u4E49\u6587\u4EF6\u6700\u5927\u5927\u5C0F 15 log4j.appender.File.MaxFileSize = 10MB 16 # \u8F93\u51FA\u6240\u4EE5\u65E5\u5FD7\uFF0C\u5982\u679C\u6362\u6210DEBUG\u8868\u793A\u8F93\u51FADEBUG\u4EE5\u4E0A\u7EA7\u522B\u65E5\u5FD7 17 log4j.appender.File.Threshold = ALL 18 log4j.appender.File.layout = org.apache.log4j.PatternLayout 19 log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
3.spring-common.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" xmlns:aop="http://www.springframework.org/schema/aop" 4 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:task="http://www.springframework.org/schema/task" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/aop 10 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 11 http://www.springframework.org/schema/tx 12 http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 13 http://www.springframework.org/schema/context 14 http://www.springframework.org/schema/context/spring-context.xsd 15 http://www.springframework.org/schema/task 16 http://www.springframework.org/schema/task/spring-task-3.0.xsd"> 17 <!-- 1. 数据源 : DriverManagerDataSource --> 18 <bean id="dataSource" 19 class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 20 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 21 <property name="url" value="jdbc:mysql://localhost:3306/teacher" /> 22 <property name="username" value="root" /> 23 <property name="password" value="123456" /> 24 </bean> 25 26 27 <!-- 2. mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 --> 28 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 29 <property name="dataSource" ref="dataSource"></property> 30 <property name="typeAliasesPackage" value="com.xtc.bean" /> 31 </bean> 32 33 34 <!-- 3. 自动扫描mybatis映射文件和接口的包 --> 35 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 36 <property name="basePackage" value="com.xtc.dao"></property> 37 </bean> 38 39 <!-- 4.配置事务管理 --> 40 <bean id="txManager" 41 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 42 <property name="dataSource" ref="dataSource"></property> 43 </bean> 44 45 <!-- 5. 使用声明式事务 transaction-manager:引用上面定义的事务管理器 --> 46 <tx:annotation-driven transaction-manager="txManager" /> 47 48 </beans>
4.spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/mvc 6 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 7 http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd"> 11 <!-- 注解扫描包 --> 12 <context:component-scan base-package="com.xtc" /> 13 14 <!-- 开启注解 --> 15 <mvc:annotation-driven /> 16 17 <!-- 配置静态资源,直接映射到对应的文件夹,不被DispatcherServlet处理 --> 18 <mvc:resources mapping="/img/**" location="/img/" /> 19 <mvc:resources mapping="/js/**" location="/js/" /> 20 <mvc:resources mapping="/css/**" location="/css/" /> 21 <mvc:resources mapping="/html/**" location="/html/" /> 22 23 24 25 <!-- 配置视图解析器 --> 26 <bean id="viewResolver" 27 class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 28 <property name="prefix" value="/WEB-INF/jsp/" /> 29 <property name="suffix" value=".jsp" /> 30 </bean> 31 32 33 </beans>
5.web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <display-name>springMVCMybatis</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <listener> 13 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 14 </listener> 15 <context-param> 16 <param-name>contextConfigLocation</param-name> 17 <param-value>classpath:spring-common.xml</param-value> 18 </context-param> 19 <servlet> 20 <servlet-name>springMVC</servlet-name> 21 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 22 <init-param> 23 <param-name>contextConfigLocation</param-name> 24 <param-value>classpath:spring-mvc.xml</param-value> 25 </init-param> 26 <load-on-startup>1</load-on-startup> 27 </servlet> 28 <servlet-mapping> 29 <servlet-name>springMVC</servlet-name> 30 <url-pattern>/</url-pattern> 31 </servlet-mapping> 32 <listener> 33 <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> 34 </listener> 35 <filter> 36 <filter-name>encodingFilter</filter-name> 37 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 38 <init-param> 39 <param-name>encoding</param-name> 40 <param-value>UTF-8</param-value> 41 </init-param> 42 <init-param> 43 <param-name>forceEncoding</param-name> 44 <param-value>true</param-value> 45 </init-param> 46 </filter> 47 <filter-mapping> 48 <filter-name>encodingFilter</filter-name> 49 <url-pattern>/*</url-pattern> 50 </filter-mapping> 51 </web-app>
剩下就是写bean,controller,server,dao层的代码了,还有jsp页面,整个项目的源代码在这里下载
http://download.youkuaiyun.com/detail/qq_32780741/9601377