SpringMVC+Spring+Ibatis整合

更多文章請進入:我的開源網

1.所需要jar包:


2.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">
	
	  <!-- 配置DispatcherServlet -->
	  <servlet>
	  	<servlet-name>commServlet</servlet-name>
	  	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	  	<!-- 手動配置加載的配置文件 -->
	  	<init-param>
	  		<param-name>contextConfigLocation</param-name>
	  		<param-value>/WEB-INF/classes/spring/*.xml</param-value>
	  	</init-param>
	  	<!-- 項目啓動時加載 -->
	  	<load-on-startup>1</load-on-startup>
	  </servlet>
	
	  <!-- servlet映射 -->
	  <servlet-mapping>
	  	<!-- 所有.aspx結尾的請求都由commServlet管理  -->
	  	<servlet-name>commServlet</servlet-name>
	  	<url-pattern>*.aspx</url-pattern>
	  </servlet-mapping>
	
	  <!-- 配置編碼過濾器 -->
	  <filter>
	  	<filter-name>encodingFilter</filter-name>
	  	<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
	  	<!-- 初始化參數,編碼是UTF-8 -->
	  	<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>
	
	  <!-- 登錄過濾 -->
	  <filter>
	    <filter-name>loginFilter</filter-name>
	    <filter-class>com.cjh.filter.LoginFilter</filter-class>
	  </filter>
	  
	  <!-- 登錄過濾映射 -->
	  <filter-mapping>
	    <filter-name>loginFilter</filter-name>
	    <url-pattern>*.jsp</url-pattern>
	  </filter-mapping>
	
	  <!-- 錯誤頁面 -->
	  <error-page>
      <error-code>404</error-code>
      <location>/error_page/404.jsp</location>
      </error-page>
	
	  <welcome-file-list>
	  	<welcome-file>index.jsp</welcome-file>
	  </welcome-file-list>
	  
	  
	  
</web-app>

3.Spring MVC配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:util="http://www.springframework.org/schema/util"
	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   
          http://www.springframework.org/schema/util    
          http://www.springframework.org/schema/util/spring-util-3.0.xsd">


	<!-- ==================主鍵掃描,配置要掃描的Java類============================ -->
	<context:component-scan base-package="com.cjh" />
	
	
	<!-- ==================啓動Spring MVC的註解功能,完成請求和註解POJO的映射,配置一個基於註解的定製的WebBindingInitializer,
	解決日期轉換問題,方法級別的處理器映射============================ -->
	<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
	    <property name="cacheSeconds" value="0" />
	    <!-- 配置一下對json數據的轉換 -->
	    <property name="messageConverters">
	    	<list>
	    		<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
	    	</list>
	    </property>
	</bean>
	
	
	<!-- ==================定義一個視圖解析類,基於ResourceView的解析器 ,此解析器是在url解析器上,
	加入了對jstl的支持============================ -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 前綴 -->
		<!-- <property name="prefix" value="/jsp/" /> -->
		<!-- 後綴 -->
		<property name="suffix" value=".jsp" />

	</bean>
	

	<!-- ================== 處理文件上傳 ============================ -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 默認編碼 (ISO-8859-1) -->
		<property name="defaultEncoding" value="utf-8" />
		<!-- 最大內存大小 (10240)-->
		<property name="maxInMemorySize" value="10240" />
		<!-- 上傳後的臨時目錄名 (WebUtils#TEMP_DIR_CONTEXT_ATTRIBUTE) -->
		<property name="uploadTempDir" value="/upload_temp/" />
		<!-- 最大文件大小,-1爲無限止(-1),注意maxUploadSize屬性的限制不是針對單個文件,而是所有文件的容量之和  -->
		<property name="maxUploadSize" value="-1" />
		
	</bean>


	<!-- ================== SpringMVC在超出上傳文件限制時,會拋出org.springframework.web.multipart.MaxUploadSizeExceededException
	該異常是SpringMVC在檢查上傳的文件信息時拋出來的,而且此時還沒有進入到Controller方法中 ============================ -->
	<bean id="exceptionResolver"
		class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<!-- 遇到MaxUploadSizeExceededException異常時,自動跳轉到fileError.jsp頁面 -->
				<prop
					key="org.springframework.web.multipart.MaxUploadSizeExceededException">
					error_page/fileUploadError
				</prop>
			</props>
		</property>
	</bean>

</beans>

4.Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">


	<!-- ===================使用 annotation============================== -->
	<context:annotation-config />
	
	<!-- ===================強制使用CGLIB代理============================== -->
	<aop:aspectj-autoproxy proxy-target-class="true"/>

	<!-- ===================C3P0 數據源配置=============================== -->
	<bean id="dataSource"
		class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 驅動 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<!-- 數據庫連接url -->
		<property name="jdbcUrl"
			value="jdbc:mysql://127.0.0.1:3306/my?autoReconnect=true&useUnicode=true&characterEncoding=utf8" />
		<!-- 用戶名 -->
		<property name="user" value="root" />
		<!-- 密碼 -->
		<property name="password" value="root" />
		<!-- 設置數據庫連接池的最大連接數 -->
		<property name="maxPoolSize" value="100" />
		<!-- 設置數據庫連接池的最小連接數 -->
		<property name="minPoolSize" value="3" />
		<!-- 設置數據庫連接池的初始化連接數 -->
		<property name="initialPoolSize" value="3" />
		<!-- 設置數據庫連接池的連接的最大空閒時間,單位爲秒 -->
		<property name="maxIdleTime" value="100" />
	</bean>

	<!-- ===================ibatis配置=============================== -->
	<bean id="sqlMapClient"
		class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource">
			<ref local="dataSource" />
		</property>
		<property name="configLocations">
			<value>classpath:ibatis/sqlmap-config.xml</value>
		</property>
	</bean>

	<!-- ===================事務管理器=============================== -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!--啓動spring註解功能-->
    <tx:annotation-driven transaction-manager="txManager" />
    
    <aop:config>
        <aop:pointcut id="baseServiceMethods" expression="execution(* com.cjh.dao.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="baseServiceMethods" />
    </aop:config>

    <aop:aspectj-autoproxy />

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="save*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
            <tx:method name="del*" propagation="REQUIRED" isolation="REPEATABLE_READ"/>
        </tx:attributes>
    </tx:advice>

</beans>

5.Ibatis配置文件:

<!DOCTYPE sqlMapConfig         
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"         
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<sqlMap resource="com/cjh/model/Userinfo.xml" />
</sqlMapConfig>

6.操作語句配置文件:

<?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>
 	<typeAlias alias="Userinfo" type="com.cjh.model.Userinfo"/>
    <insert id="saveUser" parameterClass="Userinfo">
		<![CDATA[
				INSERT INTO
				user_info(
				username,
				password
				) VALUES (
				    #username#,
				    #password#
				)
		]]> 
	</insert>
	
</sqlMap>




7.BaseDao代碼:

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.ibatis.sqlmap.client.SqlMapClient;

public class BaseDao extends SqlMapClientDaoSupport{
	@Resource
	private SqlMapClient sqlMapClient;

	@PostConstruct
	public void initSqlMapClient() {
		super.setSqlMapClient(sqlMapClient);
	}
}





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章