日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spring+springmvc+ibatis整合小结

發(fā)布時(shí)間:2025/7/25 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring+springmvc+ibatis整合小结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
最近在整合Spring和ibatis時(shí),不管applicationContext.xml放在哪兒,在web.xml中怎么配置,tomcat啟動(dòng)時(shí)始終報(bào)applicationContext.xml的錯(cuò)。后來查資料后才發(fā)現(xiàn)之前的web.xml配置文件沒有在<context-param>中指定applicationContext.xml的路徑。原來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)聽器</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ǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。