下载jar.jar包上传不了只能看截图 所需的所有的jar如下
手动搭建ssm框架手动搭建ssm框架创建mybaits主配置文件:mybatis.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>
		<!--执行时显示sql  -->
		<setting name="logImpl" value="LOG4J"/>
	</settings>
    
    <!--注册映射文件  -->
    <mappers>
    	<mapper resource="com/ysd/dao/UserInfoMapper.xml"/>
    </mappers>
</configuration>

log4j.properties文件


# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %C\: - %m%n

配置数据库连接文件applicationContext.xml

<?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"
	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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 加载db.properties文件中的内容 -->
	<context:property-placeholder location="classpath:db.properties" />

	<!-- 配置数据源dbcp -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driver}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="maxActive" value="10" />
		<property name="maxIdle" value="5" />
		
		<property name="initialSize" value="10"/>
		<property name="minIdle" value="5"/>
		<property name="maxWait" value="10000"/>
	</bean>

	<!-- 配置sqlSessionFactory value值为配置文件的路径-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:mybatis-config.xml" />
	</bean>

	<!-- 配置mapper扫描器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 扫描包的路径,手动修改,如果需要扫描多个包,中间使用半角 逗号隔开 -->
		<property name="basePackage" value="com.ysd.mapper" /> 
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	</bean>
	
</beans>

db.properties属性文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/test2
jdbc.username=root
jdbc.password=admin

加载applicationContext-transaction.xml

<?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"
	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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
	<!-- 事务管理器 -->
	<!-- 对mybatis操作数据事务控制,spring使用jdbc的事务控制类 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 数据源dataSource在applicationContex-dao.xml中配置了 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!--声明式事务 对以下的相同方法前缀添加事务支持 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />
			<tx:method name="delete*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="list*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="select*" propagation="SUPPORTS" read-only="true" />
		</tx:attributes>
	</tx:advice>

 	<!--声明式事务管理  -->
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ysd.service.impl.*.*(..))" />
	</aop:config>
	
</beans>

springmvc.xml配置文件

<?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"
	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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.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-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

	<!-- 一个配置节解决映射器和适配器的配置注解配置 -->
	<mvc:annotation-driven>
		<mvc:message-converters>
			<bean class="org.springframework.http.converter.json.GsonHttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/html;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</mvc:message-converters>
	</mvc:annotation-driven>

	<!-- 扫描所有的Controller -->
	<context:component-scan base-package="com.ysd"></context:component-scan>
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" /><!-- 注意WEB-INF前面带个/,否则restful不能实现 -->
		<property name="suffix" value=".jsp" />
	</bean>

	<!-- 静态资源解析,包括js,css,img... -->
	<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
	<mvc:resources location="/css/" mapping="/css/**"></mvc:resources>
	<mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
	
	<!-- 拦截器 -->
	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 拦截所有请求 -->
			<mvc:mapping path="/**"/>
			<!-- 除了这些 -->
			<mvc:exclude-mapping path="/toJsp/toLogin"/>
			<mvc:exclude-mapping path="/toJsp/toError"/>
			<mvc:exclude-mapping path="/toJsp/toForgetPwd2"/>
			<mvc:exclude-mapping path="/toJsp/toForgetPwd3"/>
			<mvc:exclude-mapping path="/toJsp/toForgetPwd4"/>
			<mvc:exclude-mapping path="/user/login"/>
			<mvc:exclude-mapping path="/forgetPassword/*"/>
			<mvc:exclude-mapping path="/code/*"/>
			<!-- <mvc:exclude-mapping path="/js/jquery-easyui-1.4.3"/> -->
			<!-- <mvc:exclude-mapping path="/js/global.js"/> -->
			<mvc:exclude-mapping path="/**/fonts/*"/>
			<mvc:exclude-mapping path="/**/*.css"/>
			<mvc:exclude-mapping path="/**/*.js"/>
			<mvc:exclude-mapping path="/images/*.png"/>
			<mvc:exclude-mapping path="/**/*.gif"/>
			<mvc:exclude-mapping path="/**/*.jpg"/>
			<mvc:exclude-mapping path="/**/*.jpeg"/>
			<!-- <mvc:exclude-mapping path="/sound/message.wav"/> -->
			<bean class="com.ysd.interceptor.UserInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	
</beans>

web.xml文件

<!-------初始化springmvc配置文件------>
<servlet>
		<servlet-name>springmvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
	</servlet>

<!-------请求后端路径------>
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
<!-------加载application配置文件------>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext-*.xml</param-value>
	</context-param>
	
	<!--404页面 -->
	<error-page>
	   <error-code>404</error-code>
	  <location>/toJsp/toError</location>
	</error-page>
	
	<!----事件监听----->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

springmvc原理
1.用户发送request时到前端控制器DispatcherServlet
2.DispacterServet 收到请求调用HandlerMapper处理器映射器
3.处理映射器找到具体的处理器(可以根据xml配置,注解进行查找),生成处理对象及处理器拦截器一并返回给DispacterServlet
4、 DispatcherServlet调用HandlerAdapter处理器适配器。
5、 HandlerAdapter经过适配调用具体的处理器(Controller,也叫后端控制器)。
6、 Controller执行完成返回ModelAndView。
7、 HandlerAdapter将controller执行结果ModelAndView返回给DispatcherServlet。
8、 DispatcherServlet将ModelAndView传给ViewReslover视图解析器。
9、 ViewReslover解析后返回具体View。
10、DispatcherServlet根据View进行渲染视图(将模型数据填充至视图中)。
11、 DispatcherServlet响应用户。

分类:

技术点:

相关文章: