首页 > 代码库 > spring+springmvc+ibatis整合小结

spring+springmvc+ibatis整合小结

最近在整合Spring和ibatis时,不管applicationContext.xml放在哪儿,在web.xml中怎么配置,tomcat启动时始终报applicationContext.xml的错。后来查资料后才发现之前的web.xml配置文件没有在<context-param>中指定applicationContext.xml的路径。原来tomcat在加载web.xml时会优先加载<context-param>和<listener>,之后才加载<servlet>。
 
现将正常运行的web.xml贴出:
 
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>S</display-name>
 
<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>
<listener>
<description>请求监听器</description>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>  
      <param-name>contextConfigLocation</param-name>  
  <param-value>classpath:applicationContext.xml</param-value>
   </init-param> 
<load-on-startup>1</load-on-startup>
</servlet>
 
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
 
</web-app>
 
 
applicationContext.xml中可同时使用SqlMapClient和SqlMapClientTemplate,也可单独使用SqlMapClient。区别在于SqlMapClient中包含session的管理,SqlMapClientTemplate包含session的封装以及异常的捕捉,所以用SqlMapClient时需另外进行异常的处理。
 
applicationContext.xml的配置文件如下:
 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
 
<context:component-scan base-package="com.qs"/>
 
<mvc:annotation-driven /> 
 
 
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"/>
<property name="url" value="http://www.mamicode.com/jdbc:mysql://localhost:3306/test"/>
<property name="username" value="http://www.mamicode.com/root"/>
<property name="password" value="http://www.mamicode.com/qinsong"/>
</bean>
 
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="http://www.mamicode.com/classpath:com/qs/sqlmap/sqlMapConfig.xml"/>
<property name="dataSource" ref="myDataSource"/>
</bean>
<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    <property name="prefix" value="http://www.mamicode.com/"/>  
    <property name="suffix" value="http://www.mamicode.com/.jsp"/>
    <property name="viewClass" value="http://www.mamicode.com/org.springframework.web.servlet.view.JstlView" />  
</bean>  
 
</beans>
 
ibatis相应配置文件如下
sqlMapConfig.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"  "http://www.ibatis.com/dtd/sql-map-config-2.dtd" >
<sqlMapConfig>
 <settings useStatementNamespaces="true"/> 
<sqlMap resource="com/qs/sqlmap/sqlMap-User.xml"/>
</sqlMapConfig>
 
sqlMap-User.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >  
<sqlMap namespace="user">
 
<resultMap id="userMap" class="com.qs.entity.User">
<result property="id" column="id"/>
<result property="username" column="username" />
<result property="password" column="password" />
</resultMap>
 
<select id="getUserById"  parameterClass="java.lang.Integer" resultMap="userMap">
select id, username,password
from user
where id = #id#
</select>
</sqlMap>