spring mvc、spring、mybatis框架整合

来源:互联网 发布:pdf排版软件 编辑:程序博客网 时间:2024/05/21 17:26

总结

在整合中,自认为总重要的就是配置文件,但本身框架都是自学,基础还是差了点,以至于大部分的时间都在配置文件上,从难到易反而加深了理解。

首先,配置web.xml文件时要注意:

<context-param>    <param-name>contextConfigLocation</param-name>    <param-value>classpath:beans.xml;classpath:mvc-servlet.xml</param-value>    </context-param>

配置需要加载的上下文;

<servlet>    <description>spring mvc servlet</description>    <servlet-name>springMvc</servlet-name>    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    <init-param>      <description>spring mvc 配置文件</description>     <param-name>contextConfigLocation</param-name>      <param-value>classpath:mvc-servlet.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>
在这部分配置中要注意url-patter,每个人的配置都可能不同,可根据自己方便来写;

接着,在beans.xml中要注意:

   <!-- mybatis自动扫描加载sql映射文件:MapperScannerConfigurer    basePackage;指定SQL映射文件/接口所在的包(自动扫描)     -->    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    <property name="basePackage" value="com.mapper"></property>    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>    </bean>                <!-- 事务管理:DataSourceTransactionManager -->    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="ds"></property>    </bean>

     <aop:config>         <!-- 配置一个切入点 -->         <aop:pointcut id="serviceMethods" expression="execution(* com.service.*ServiceImpl.*(..))" />         <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />     </aop:config>

最后是mvc-servlet.xml主要配置如下

     <bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>         <property name="prefix" value="/"></property> <!-- 我这里的视图直接放在WebRoot下-->         <property name="suffix" value=".jsp"></property>     </bean>
在搭建过程中主要碰到的问题

1.忘记加载配置文件

2.web.xml中的url-patter和请求的路径没有匹配



0 0
原创粉丝点击