1. 程式人生 > >簡單的SpringMVC+Mybatis整合

簡單的SpringMVC+Mybatis整合

先申明幾點:

1.這個整合呢,我也是看的其他大神的各種資料來學習的。

2.SpringMVC框架的原理、能做什麼、有什麼長短處什麼的,我這邊就不寫了,我相信你在學這個框架的時候,你肯定得先去了解。

3.我儘量簡短的寫,然後呢,我儘可能的把已經整合好的框架原始碼丟上來,到時候你們下載下去自己研究,這樣呢,記憶深刻一點。

4.這次整合呢,我也是第一次,肯定會有一些你們看不懂的問題,到時候呢,加我QQ:728793963我們相互學習。


直接來乾貨吧!!!


1.jar包準備


2.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">
<!-- 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>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

3.sping-mvc.xml配置如下:

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自動掃描該包,使SpringMVC認為包下用了@controller註解的類是控制器 -->
<context:component-scan base-package="net.fhxy.web" />
<!-- <bean name="/index" class="net.fhxy.web.UserController" ></bean> -->
<!--避免IE執行AJAX時,返回JSON出現下載檔案 -->
<!-- <bean id="mappingJacksonHttpMessageConverter" -->
<!-- class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> -->
<!-- <property name="supportedMediaTypes"> -->
<!-- <list> -->
<!-- <value>text/html;charset=UTF-8</value> -->
<!-- </list> -->
<!-- </property> -->
<!-- </bean> -->
<!-- 啟動SpringMVC的註解功能,完成請求和註解POJO的對映 -->
<mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>
<!-- 自定義引數繫結 -->
<bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="net.fhxy.pojo.converter.UserDateConverter"></bean>
</list>
</property>
</bean>
<!-- 定義跳轉的檔案的前後綴 ,檢視模式配置-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 這裡的配置我的理解是自動給後面action的方法return的字串加上字首和字尾,變成一個 可用的url地址 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<!-- 配置檔案上傳,如果沒有使用檔案上傳可以不用配置,當然如果不配,那麼配置檔案中也不必引入上傳元件包 -->
<!-- <bean id="multipartResolver"   -->
<!--         class="org.springframework.web.multipart.commons.CommonsMultipartResolver">   -->
        <!-- 預設編碼 -->
<!--         <property name="defaultEncoding" value="utf-8" />   -->
        <!-- 檔案大小最大值 -->
<!--         <property name="maxUploadSize" value="10485760000" />   -->
        <!-- 記憶體中的最大值 -->
<!--         <property name="maxInMemorySize" value="40960" />   -->
<!--     </bean>  -->

</beans>

4.SpringMVC和Mybatis的整合配置如下:

<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
                        http://www.springframework.org/schema/beans/spring-beans.xsd  
                        http://www.springframework.org/schema/context  
                        http://www.springframework.org/schema/context/spring-context.xsd  
                        http://www.springframework.org/schema/mvc  
                        http://www.springframework.org/schema/mvc/spring-mvc.xsd
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 自動掃描 -->
<context:component-scan base-package="net.fhxy" />
<!-- 引入配置檔案 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 初始化連線大小 -->
<!-- <property name="initialSize" value="${initialSize}"></property> -->
<!-- 連線池最大數量 -->
<!-- <property name="maxActive" value="${maxActive}"></property> -->
<!-- 連線池最大空閒 -->
<!-- <property name="maxIdle" value="${maxIdle}"></property> -->
<!-- 連線池最小空閒 -->
<!-- <property name="minIdle" value="${minIdle}"></property> -->
<!-- 獲取連線最大等待時間 -->
<!-- <property name="maxWait" value="${maxWait}"></property> -->
</bean>


<!-- spring和MyBatis完美整合,不需要mybatis的配置對映檔案 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自動掃描mapping.xml檔案 -->
<property name="mapperLocations" value="classpath:net/fhxy/pojo/mapping/*.xml"></property>
</bean>


<!-- DAO介面所在包名,Spring會自動查詢其下的類 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="net.fhxy.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>


<!--事務管理 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userDAO" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="net.fhxy.dao.IUserDAO" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>


<!-- <bean id="userDAO" class="net.fhxy.dao.UserDAO"> -->
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> -->
<!-- </bean> -->
<!-- <bean id="userService" class="net.fhxy.service.impl.UserServiceImpl"> -->
<!-- <property name="userDAO" ref="userDAO"></property> -->
<!-- </bean> -->
<!-- <bean id="userController" class="net.fhxy.web.UserController"> -->
<!-- <property name="userService" ref="userService"></property> -->
<!-- </bean> -->
</beans>

5.jdbc配置如下:(jdbc的連線呢,常用的三種資料庫,參照這個寫就可以)



#-------------------------------------------------------------------------------
# Oracle Settings


#jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@server:1521:orcl
#jdbc.username=admin
#jdbc.password=admin
#
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
#hibernate.show_sql=true
#hibernate.hbm2ddl.auto=update
#-------------------------------------------------------------------------------
# MySQL Settings
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/fhxy
jdbc.username=root
jdbc.password=admin


#hibernate.dialect=org.hibernate.dialect.MySQLDialect
#hibernate.show_sql=true
#hibernate.hbm2ddl.auto=update


#-------------------------------------------------------------------------------
# SQL Server Settings
# jdbc.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
# jdbc.url=jdbc:sqlserver://localhost:1433;DatabaseName=AssetDataBase#建立資料庫"AssetDataBase"
# jdbc.username=root
# jdbc.password=admin


# hibernate.dialect=org.hibernate.dialect.SQLServerDialect
# hibernate.show_sql=true
# hibernate.hbm2ddl.auto=update

6.log日誌檔案的配置如下:(如果大家有空的話,可以去研究一下log4j這個的詳細配置,對以後的專案開發很有好處!!!)

log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n

7.整合專案架構圖如下:

8.註明

1>這個框架呢,我是在公司整理的,jdk不一樣,所以你們到時候用的時候,首先要該jdk,我這個是1.7的,tomcat是7.0的

2>如果遇到許可權問題呢,刪掉位元組碼檔案,重新編譯一下就ok,資料庫的sql我忘了拷貝回來,就兩張表,動手寫一下幾分鐘的事情,他們之間有一個外來鍵關聯。

3>如果還是有問題的話,可以問我,最好是先百度谷歌,這樣你自己的記憶也深刻一點。

4>原始碼問我要吧,有問題Q我!