Sping,SpringMVC,Mybatis 三大框架整合环境搭建详解

来源:互联网 发布:韩服数据库 编辑:程序博客网 时间:2024/05/21 17:46

SpringMVC:springMVC是一个表现层的框架,类似struts2.主要用于jsp页面与后台数据交互

  • SpringMVC的三大组件:
    • 第一个组件:处理器映射器
    • 第二个组件:处理器适配器
    • 第三个组件:视图解析器

这里写图片描述

这里写图片描述

Springmvc执行流程

  • strutsPrepareAndExcuteFilter拦截请求(控制层),拦截请求,转发请求
  • 寻找Action执行
  • ActionProxy:strutsActionProxy extends defaultActionProxy
  • ActionMapping去寻找执行类Action
    根据mvc设计模式:

这里写图片描述

Sping,SpringMVC,Mybatis 三大框架整合环境搭建详解:

第一步:导入jar包

这里写图片描述

第二步:配置spring的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:c="http://www.springframework.org/schema/c"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"><!-- 包扫描 --> <context:component-scan base-package="cn.peaceliu.springmvc.service"/> <!-- 读取配置文件 --> <context:property-placeholder location="classpath:*.properties"/> <!-- 数据源 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">    <property name="driverClassName" value="${jdbc.driver}"></property>    <property name="url" value="${jdbc.url}"/>    <property name="username" value="${jdbc.username}"></property>    <property name="password" value="${jdbc.password}"></property> </bean> <!-- sqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">    <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>    <property name="dataSource" ref="dataSource"></property>    <property name="mapperLocations" value="classpath:mappers/*.xml"></property> </bean> <!-- 配置我们所有的接口扫描包 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <property name="basePackage" value="cn.peaceliu.springmvc.dao"></property> </bean><!-- 事物管理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="dataSource"></property> </bean> <!-- 开启事物 --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>  

第二步:配置mybatis的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:aop="http://www.springframework.org/schema/aop"       xmlns:tx="http://www.springframework.org/schema/tx"       xmlns:p="http://www.springframework.org/schema/p"       xmlns:c="http://www.springframework.org/schema/c"       xmlns:mvc="http://www.springframework.org/schema/mvc"       xsi:schemaLocation="            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd            http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">        <!-- 包扫描路径一定要配置到controller层,不要放大包扫描路径 -->        <context:component-scan base-package="cn.peaceliu.springmvc.controller"></context:component-scan>        <!-- 配置我们的处理器映射器和处理器适配器 -->        <mvc:annotation-driven/>        <!-- 配置我们的资源视图解析器 -->        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">            <property name="prefix" value="/WEB-INF/jsp/"></property>            <property name="suffix" value=".jsp"></property>        </bean>    </beans>

第四步:配置web.xml

<!-- needed for ContextLoaderListener --><context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:applicationContext.xml</param-value></context-param><!-- Bootstraps the root web application context before servlet initialization --><listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- The front controller of this Spring Web application, responsible for handling all application requests --><servlet>    <servlet-name>springDispatcherServlet</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><!-- Map all requests to the DispatcherServlet for handling --><servlet-mapping>    <servlet-name>springDispatcherServlet</servlet-name>    <url-pattern>*.action</url-pattern></servlet-mapping>
阅读全文
0 0
原创粉丝点击