Spring+SpringMvc+Mybatis整合注意事项

时间:2023-03-09 04:29:56
Spring+SpringMvc+Mybatis整合注意事项

1.web.xml代码如下

<?xml version="1.0" encoding="UTF-8"?>
<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"
version="3.0">
<display-name>Archetype Created Web Application</display-name>
<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param> <!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<async-supported>true</async-supported>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 防止Spring内存溢出监听器 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener> <!-- Spring MVC servlet -->
<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-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 这里代表拦截如下两种后缀的请求,如果只写 /,那么代表拦截所有资源 -->
<url-pattern>*.json</url-pattern>
<url-pattern>*.jhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list> </web-app> <!-- 读取spring和shiro配置文件 -->
<!-- <context-param> -->
<!-- <param-name>contextConfigLocation</param-name> -->
<!-- <param-value>classpath:spring-shiro.xml</param-value> -->
<!-- </context-param> -->
<!-- 设计路径变量值 -->
<!-- <context-param> -->
<!-- <param-name>webAppRootKey</param-name> -->
<!-- <param-value>springmvc.root</param-value> -->
<!-- </context-param> --> <!-- shiro过滤器 -->
<!-- <filter> -->
<!-- <filter-name>shiroFilter</filter-name> -->
<!-- <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> -->
<!-- <init-param> -->
<!-- <param-name>targetFilterLifecycle</param-name> -->
<!-- <param-value>true</param-value> -->
<!-- </init-param> -->
<!-- </filter> -->
<!-- <filter-mapping> -->
<!-- <filter-name>shiroFilter</filter-name> -->
<!-- <url-pattern>*.jhtml</url-pattern> -->
<!-- <url-pattern>*.json</url-pattern> -->
<!-- </filter-mapping> -->

2.当有两个以上的.xml文件加载时,错误范例

<!-- Spring和mybatis的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param> <!-- Spring-shiro的配置文件 --> <param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-shiro.xml</param-value>
</context-param>

正确写法

 <!-- Spring和mybatis的配置文件与 Spring-shiro的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml,classpath:spring-shiro.xml</param-value>
</context-param>