框架SSM
简单记录整合Spring+SpringMVC+Mybatis
简述三个关键配置文件
web.xml
<!--上下文参数--> <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> <!--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:springmvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--字符编码过滤器--> <filter> <filter-name>encoding</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> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
applicationContext.xml
<!--扫描注解--> <context:component-scan base-package="com.cgq.service.impl"></context:component-scan> <!--加载数据库配置文件 即可以在本配置文件存放连接数据库所需的数据--> <context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!--数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.root}"></property> <property name="password" value="${jdbc.pw}"></property> </bean> <!--配置SqlSessionFactory--> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--起别名--> <property name="typeAliasesPackage" value="com.cgq.pojo"></property> </bean> <!--扫描器 扫描Mapper层--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cgq.mapper"></property> <property name="sqlSessionFactoryBeanName" value="factory"></property> </bean> <!--事物管理 即用配置文件管理mapper中的函数对数据库的操作--> <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--声明事物--> <tx:advice id="txAdvice" transaction-manager="txManage"> <tx:attributes> <!--除了ins*、del*、upd*其他的类只能读不能进行对数据库操作--> <tx:method name="ins*"/> <tx:method name="del*"/> <tx:method name="upd*"/> <tx:method name="*" read-only="true"></tx:method> </tx:attributes> </tx:advice> <!--配置aop--> <aop:config> <!--* com.cgq.service.impl.*.*(..)) *: 通配符 impl.*.*: impl中的任意类的任意方法 (..): 不确定参数 --> <aop:pointcut id="mypoint" expression="execution(* com.cgq.service.impl.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor> </aop:config>
springmvc.xml
<!--扫描控制器--> <context:component-scan base-package="com.cgq.controller"></context:component-scan> <!--加载驱动--> <mvc:annotation-driven></mvc:annotation-driven> <!--静态资源--> <mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <mvc:resources mapping="/files/**" location="/files/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources> <!--配置Multipart--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"></bean>
SpringMVC运行原理:
如果在web.xml中设置DispatcherServlet的<url-pattern>为/时
当用户发起请求,请求一个控制器
首先会执行DispatcherServlet
由DispatcherServlet调用HandlerMapping的DefaultAnnotationHandlerMapping解析URL
解析后调用HandlerAdapter组件的AnnotationMethodHandlerAdapter去调用Controller中的HandlerMethod
当HandlerMethod执行完会返回View
会被ViewResolver进行视图解析
解析后调用jsp对应的.class文件并运行
最终把运行.class文件的结果响应给客户端