Spring MVC + Spriing + MyBatis整合,寫給新人


開發環境:

開發工具:MyEclipse 8.6

數據庫:MySQL

操作系統:WIN8.1

Jar包:

Spirng和SpringMVC版本:3.2.9

MyBatis版本:3.2.8

其他關聯Jar包如圖:

 ---------------------------------------------------------------------------------

OK , 准備的東西齊全了 。這就動手,先用MyEclipse新建一個網站工程,然后將所有的Jar包放到WebRoot/WEB-INF/lib文件夾下面。

首先配置web.xml文件,增加Spring的支持

 

  <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <context-param>
      <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationDataSource.xml</param-value>
  </context-param>

 

接着在src下建立

applicationDataSource.xml

jdbc.properties文件

mybatis-config.xml

以上三個文件

mybatis-config.xml 文件自然就是MyBatis的配置了,其實只用到了攔截器,后面我們會看到。jdbc.properties文件里面配置具體的數據庫信息,在applicationDataSource.xml文件中讀取
先看jdbc.properties的內容:

這里簡單配置了數據庫的一些信息,我們現在看applicationDataSource.xml文件中的配置

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:aop="http://www.springframework.org/schema/aop"
 7     xmlns:tx="http://www.springframework.org/schema/tx"
 8     xmlns:mvc="http://www.springframework.org/schema/mvc"
 9     xsi:schemaLocation="
10     http://www.springframework.org/schema/beans 
11     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
12     http://www.springframework.org/schema/aop
13     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
14     http://www.springframework.org/schema/tx
15     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
16     ">
17   <!-- 這里我用了aop:aspectj-autoproxy,如果使用aop有對類的配置的話就派上用場了。個人習慣而已 -->
18     <aop:aspectj-autoproxy proxy-target-class="true"/>
19     
20     <!-- 載入jdbc.properties文件 -->
21     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
22         <property name="locations">
23             <list><value>classpath:jdbc.properties</value></list>
24         </property>
25     </bean>
26     
27    <!-- 配置數據源 ,注意value部分的配置寫法 -->
28     <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource"
29         destroy-method="close">
30         <property name="driverClassName" value="${driverClassName}" />
31         <property name="url" value="${url}" />
32         <property name="username" value="${username}" />
33         <property name="password" value="${password}" />
34         <!-- 數據庫連接池保持的最小連接數 -->
35         <property name="minIdle" value="${minIdle}" />
36         <!-- 數據庫連接池保持的最大連接數 -->
37         <property name="maxIdle" value="${maxIdle}" />
38         <!--
39             當數據庫連接因為某種原因斷掉之后,再重新從連接池中拿另外一個連接時實際上這個連接可能
40             已經無效,所以為了確保所拿到的連接全都有效需要在獲取連接,返回連接以及連接空閑時進行 有效性驗證
41             下面3個設置為ture時進行驗證,默認為false
42         -->
43         <!-- 取得連接時是否進行有效性驗證 -->
44         <property name="testOnBorrow" value="true" />
45         <!-- 返回連接時是否進行有效性驗證 -->
46         <property name="testOnReturn" value="true" />
47         <!-- 連接空閑時是否進行有效性驗證 -->
48         <property name="testWhileIdle" value="true" />
49 
50     </bean>
51   <!-- 配置sqlSessionFactory ,注意mapperLocations的配置,這個路徑里面存放了MyBatis的Mapper文件,configLocation則加載了mybatis-config.xml文件,不需要的可以不配置 -->
52     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
53         <property name="configLocation" value="classpath:mybatis-config.xml"/>
54          <property name="dataSource" ref="datasource"/>
55          <property name="mapperLocations">
56              <list>
57                  <value>classpath:org/springmvc_demo/mapper/*.xml</value>
58              </list>
59          </property>
60     </bean>
61     
62     <!-- JDBC的事務管理 -->
63     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
64         <property name="dataSource" ref="datasource"/>
65     </bean>
66     
67     <!-- 事務管理對應的方法規則 -->
68     <tx:advice transaction-manager="transactionManager" id="txAdvice">
69         <tx:attributes>
70             <tx:method name="add*" propagation="REQUIRED"/>
71             <tx:method name="save*" propagation="REQUIRED"/>
72             <tx:method name="update*" propagation="REQUIRED"/>
73             <tx:method name="modify*" propagation="REQUIRED"/>
74             <tx:method name="del*" propagation="REQUIRED"/>
75             <tx:method name="delete*" propagation="REQUIRED"/>
76             <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
77             <tx:method name="query*" propagation="SUPPORTS" read-only="true"/>
78             <tx:method name="*" read-only="true"/>
79         </tx:attributes>
80     </tx:advice>
81     
82     <aop:config>
83        <!-- 這里的配置應用事務管理的包名我提前准備好了,根據自己的實際情況修改包名,但是事物管理還是建議在業務邏輯層控制 -->
84 <aop:pointcut expression="execution(* org.springmvc_demo.service.impl.*.*(..))" id="transactionAop"/> 85 <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionAop"/> 86 </aop:config> 87  <!-- 我們需要一個 sqlSessionTemplate對象,使用看之后的代碼配置-->
88 <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate"> 89 <constructor-arg index="0" ref="sqlSessionFactory"/> 90 </bean> 91 92 </beans>

 Spring的配置到此結束,我們在來到web.xml中增加SpringMVC的配置。

 這里配置文件我沒有使用默認的路徑,而是將springmvc的配置文件放到了src根目錄下,<load-on-startup>1</load-on-startup>這個一定要加上。注意url-pattern的配置,不要修改。

 1 <servlet>
 2       <servlet-name>springMVC</servlet-name>
 3       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
 4       <init-param>
 5         <param-name>contextConfigLocation</param-name>
 6         <param-value>classpath*:applicationContext.xml</param-value>      
 7       </init-param>
 8       <load-on-startup>1</load-on-startup>
 9   </servlet>
10   
11   <servlet-mapping>
12       <servlet-name>springMVC</servlet-name>
13       <url-pattern>/</url-pattern>
14   </servlet-mapping>

web.xml文件修改完畢后,我們來看SpringMVC的配置文件的內容

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xmlns:context="http://www.springframework.org/schema/context"
 7     xmlns:mvc="http://www.springframework.org/schema/mvc"
 8     xsi:schemaLocation="
 9     http://www.springframework.org/schema/beans 
10     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
11     http://www.springframework.org/schema/context
12     http://www.springframework.org/schema/context/spring-context-3.0.xsd
13     http://www.springframework.org/schema/mvc
14     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
15     ">
16     
17     <!-- 包掃描的配置放在了SpringMVC加載的配置文件中,否則將無法正常工作 -->
18     <context:component-scan base-package="org.springmvc_demo"/>
19     <mvc:annotation-driven/>
20     
21     <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
22         <property name="prefix" value="/"/>
23         <property name="suffix"  value=".jsp"/>
24         <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
25     </bean>
26 </beans>

OK ,這里的配置就到此為止,需要其他功能的自行配置,唯一需要注意的是我沒有在這里對靜態資源做配置,這樣的話html,css等靜態資源按照上面web.xml中的配置將無法正常使用。所以我們還需要再次返回web.xml增加對靜態資源的配置。

 1   <servlet-mapping>
 2       <servlet-name>default</servlet-name>
 3       <url-pattern>*.html</url-pattern>
 4   </servlet-mapping>
 5   
 6   <servlet-mapping>
 7       <servlet-name>default</servlet-name>
 8       <url-pattern>*.htm</url-pattern>
 9   </servlet-mapping>
10   
11   <servlet-mapping>
12       <servlet-name>default</servlet-name>
13       <url-pattern>*.js</url-pattern>
14   </servlet-mapping>
15   
16   <servlet-mapping>
17       <servlet-name>default</servlet-name>
18       <url-pattern>*.css</url-pattern>
19   </servlet-mapping>
20   
21   <servlet-mapping>
22       <servlet-name>default</servlet-name>
23       <url-pattern>*.jpg</url-pattern>
24   </servlet-mapping>
25   
26    <servlet-mapping>
27       <servlet-name>default</servlet-name>
28       <url-pattern>*.png</url-pattern>
29   </servlet-mapping>
30   
31    <servlet-mapping>
32       <servlet-name>default</servlet-name>
33       <url-pattern>*.gif</url-pattern>
34   </servlet-mapping>

加上以上的配置,靜態資源就可以正常訪問了。
到此為止,SpringMVC +Spring + MyBatis的配置文件就基本配置完畢了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM