spring+springmvc+ibatis整合小结
生活随笔
收集整理的這篇文章主要介紹了
spring+springmvc+ibatis整合小结
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近在整合Spring和ibatis時(shí),不管applicationContext.xml放在哪兒,在web.xml中怎么配置,tomcat啟動(dòng)時(shí)始終報(bào)applicationContext.xml的錯(cuò)。后來(lái)查資料后才發(fā)現(xiàn)之前的web.xml配置文件沒(méi)有在<context-param>中指定applicationContext.xml的路徑。原來(lái)tomcat在加載web.xml時(shí)會(huì)優(yōu)先加載<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>請(qǐng)求監(jiān)聽(tīng)器</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中可同時(shí)使用SqlMapClient和SqlMapClientTemplate,也可單獨(dú)使用SqlMapClient。區(qū)別在于SqlMapClient中包含session的管理,SqlMapClientTemplate包含session的封裝以及異常的捕捉,所以用SqlMapClient時(shí)需另外進(jìn)行異常的處理。 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="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="qinsong"/> </bean><bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation" value="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="/"/> <property name="suffix" value=".jsp"/><property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> </bean> </beans>
ibatis相應(yīng)配置文件如下 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>
轉(zhuǎn)載于:https://www.cnblogs.com/qs-spring/p/3811090.html
總結(jié)
以上是生活随笔為你收集整理的spring+springmvc+ibatis整合小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WPF 根据绑定值设置DataGrid行
- 下一篇: Erlang(起个中文名:易浪)不能错过