【SpringMVC學習04】Spring、MyBatis和SpringMVC的整合

前兩篇springmvc的文章中都沒有和mybatis整合,都是使用靜態數據來模擬的,但是springmvc開發不可能不整合mybatis,另外mybatis和spring的整合我之前學習mybatis的時候有寫過一篇,但是僅僅是整合mybatis和spring,所以這篇文章我係統的總結一下spring、mybatis和springmvc三個框架的整合(後面學習到maven時,我會再寫一篇使用maven整合的文章,這篇沒有用到maven)。

1. jar包管理

  我之前有寫過一篇spring、hibernate和struts2整合的文章,在整合的時候,我個人不喜歡亂,不喜歡啪嘰一下將所有jar包往lib中一扔,因爲那樣沒有條理,所以在整合SSM的時候,我還是遵循jar包分類的原則,首先看一下SSM整合都用到了哪些jar包(點我下載): 
jar包 
  這裏我用的是dbcp,當然也可以用c3p0等其他連接池,歸歸類後jar包就很有條理。

2. 整合思路

  關於SSM的架構可以簡單看一下下面的草圖: 
架構 
  可以看出,spring在進行管理時,是很有條理的,每個層都由spring管理,然後不同的層可以調用其它層,Handler調用service,service調用mapper等。根據這個架構,我們來總結一下整合的思路,根據這個調用關係,我們可以從下往上一步步整合。

  1. 整合dao層。mybatis和spring整合,通過spring管理mapper接口。 
    使用mapper的掃描器自動掃描mapper接口在spring中進行註冊。
  2. 整合service層。通過spring管理 service接口。 
    使用配置方式將service接口配置在spring配置文件中。 
    實現事務控制。
  3. 整合springmvc。由於springmvc是spring的模塊,不需要整合。

  現在思路清晰了,接下來就開始整合了。在整合前先看一下我整合完的工程結構: 
工程結構

3. 整合dao層

  整合dao層也就是整合持久層,那麼需要spring的核心包,持久層包,mybatis包,數據庫以及連接池的包。所以將spring-persistence/spring-core/mysql-connector/mybatis/dbcp幾個文件夾中的jar包拷貝到lib中。

3.1 mybatis全局配置文件

  首先得寫mybatis的全局配置文件sqlMapConfig.xml,如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <!-- setting配置根據需要再添加 -->
    <!-- 配置別名 -->
    <typeAliases>
        <package name="ssm.po"/>
    </typeAliases>

    <!-- mapper這裏不需要配置了,因爲跟spring整合後,在spring那邊會進行mapper的掃描 
        但必須遵循:mapper.xml和mapper.java必須同名且在一個目錄
    -->
</configuration>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

  可以看出,整合的時候,這個全局配置文件已經很清爽了,基本沒啥東東了,因爲數據源啊、mapper啊啥的都交給spring去管理了。 
關於db.properties和log4j.properties中的內容,我就不寫了,可以參考我之前寫的mybatis相關博文,也可以直接看我的源碼。

3.2 配置spring配置文件

  配置完了mybatis的全局配置文件後,接下來就要配置spring的配置文件了,spring的配置文件我將分類寫在不同的文件中,都放在config/spring/目錄下了,這裏是對dao的整合,所以起名applicationContext-dao.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:mvc="http://www.springframework.org/schema/mvc"
       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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 加載db.properties文件中的內容 -->   
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 配置數據源dbcp -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- 配置sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
    </bean>

    <!-- 配置mapper掃描器 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 掃描包的路徑,如果需要掃描多個包,中間使用半角 逗號隔開-->
        <property name="basePackage" value="ssm.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

  可以看出,整合dao層的時候主要配置一下數據源、sqlSessionFactory和mapper掃描器,這樣的話,數據源,sqlSessionFactory和mapper在tomcat啓動時就被spring實例化到了容器中。接下來就是準備po類及mapper了。

3.3 逆向工程生成po類及mapper

  關於如何使用mybatis的逆向工程我就不再贅述了,如果不太清楚的童鞋請看一下這篇文章:☞點我查看。 
將生成的代碼拷貝到我們自己的工程中即可,如下(那些文件前的問號不管它,是我還沒同步到github的緣故): 
po 
  到這裏dao層就整合好了,下面來做個測試,整合的時候一定要步步爲營,別啪啪啪整合完了再一起測試,到時候出錯再找就不太方便了。我們針對ItemsMapper接口中的SelectByPrimaryKey()方法做一個測試:

public class ItemsMapperTest {

    ApplicationContext applicationContext = null;
    @Before
    public void setUp() throws Exception {

        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
    }

    @Test
    public void testSelectByPrimaryKey() {
        ItemsMapper itemsMapper = (ItemsMapper) applicationContext.getBean("itemsMapper");
        Items items = itemsMapper.selectByPrimaryKey(1);
        System.out.println(items);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

  能打印出id爲1的Items對象說明沒有問題。

3.4 一個小插曲

  這裏需要注意一個問題,剛剛逆向工程生成的代碼,我們儘量不要去改動它,爲什麼呢?比如我現在改動了生成後的代碼,後面萬一需求改變,我肯定會根據新的表關係再生成一次,然後再拷到自己的工程,這樣之前修改的東西就會被覆蓋,如果又不想被覆蓋,那就很麻煩了……要改的地方就太多了……所以我們永遠不去修改生成之後的代碼,如果有需要,我們在生成的代碼基礎上進行擴展(繼承,組合等),這樣就算代碼重新生成,也不會影響我們的擴展類。 
  我舉個例子:假如現在有個需求:

SELECT * FROM items WHERE items.name LIKE ‘%參數%’

  這個參數我們要通過一個Items的包裝類傳進來,Items的包裝類指的是,裏面封裝了Items的信息,還封裝了其他信息。因爲這個包裝類除了查詢Items相關信息外,還可能有關聯查詢,所以裏面不僅僅就只有Items本身的信息。 
  要實現這個需求,我們不能直接去改Items類,不能直接在Items類中添加東西,原因上面已經分析了,我們可以這麼做:

  1. 先寫一個Items的繼承類
  2. 在這個繼承類上進行擴展

  爲啥要先寫一個繼承類呢?直接寫Items的擴展類不就行了麼?原因還是上面提到的,完以後面這個Items改了咋整?這是一點,還有就是Items的繼承類裏面,我就可以做一些新的操作了。看一下實現:

//Items的繼承類
public class ItemsCustom extends Items {

    //可以添加商品信息的擴展屬性,如果不添加,其實就是Items

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
//這個就是Items的包裝類,我們從service開始,傳遞下去的都是包裝類
public class ItemsQueryVo {

    //原始的商品信息
    private Items items;

    //爲了系統 可擴展性,對原始生成的po進行擴展
    private ItemsCustom itemsCustom;

    public Items getItems() {
        return items;
    }

    public void setItems(Items items) {
        this.items = items;
    }

    public ItemsCustom getItemsCustom() {
        return itemsCustom;
    }

    public void setItemsCustom(ItemsCustom itemsCustom) {
        this.itemsCustom = itemsCustom;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

  然後我們定義itemsMapperCustom.xml映射文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="ssm.mapper.ItemsMapperCustom" >
    <!-- 定義商品查詢的sql片段,就是商品查詢條件 -->
    <sql id="query_items_where">
        <!-- 使用動態sql,通過if判斷,滿足條件進行sql拼接 -->
        <!-- 查詢條件通過ItemsQueryVo包裝對象中的itemsCustom屬性來傳遞-->
        <if test="itemsCustom != null">
            <if test="itemsCustom.name != null and itemsCustom.name != ''">
                items.name LIKE '%${itemsCustom.name}%'
            </if>
        </if>
    </sql>

    <select id="findItemsList" parameterType="ssm.po.ItemsQueryVo"
            resultType="ssm.po.ItemsCustom">
        SELECT items.* FROM items
        <where>
            <include refid="query_items_where"></include>
        </where>
    </select>
</mapper>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

  當然咯,對應的mapper接口要定義一下:

public interface ItemsMapperCustom {

    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;
}
  • 1
  • 2
  • 3
  • 4

  最後針對這個接口寫一個測試類:

public class ItemsMapperCustomTest {

    ApplicationContext applicationContext = null;
    @Before
    public void setUp() throws Exception {

        applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
    }


    @Test
    public void testFindItemsList() throws Exception {
        ItemsMapperCustom itemsMapper = (ItemsMapperCustom) applicationContext.getBean("itemsMapperCustom");

        ItemsQueryVo itemsQueryVo = new ItemsQueryVo();
        ItemsCustom itemsCustom = new ItemsCustom();
        itemsCustom.setName("手機");
        itemsQueryVo.setItemsCustom(itemsCustom);


        List<ItemsCustom> itemsCustomList = itemsMapper.findItemsList(itemsQueryVo);
        System.out.println(itemsCustomList);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

  可以看到,我們操作的都是Items的繼承類ItemsCustom和包裝類ItemsQueryVo,這個ItemsQueryVo將貫穿整條線,從service調用,到dao層。由逆向工程生成的原始的po類我們不去操作它們,除非是簡單的查詢,那麼直接查即可。

3. 整合service層

  先把jar包導了再說,整合service層需要配置事務了,所以要導入spring-aop中所有的jar包到lib中。 
  之前提到過,service是用來調用mapper的,mapper是用來操作數據庫的,其實上面的小插曲中的測試代碼就有點類似service層做的事,先獲取mapper接口的代理對象,然後操作數據庫。所以在service層,我們首先要獲取mapper接口的代理對象,只不過在這裏我們通過spring注入進來,然後通過這個代理對象去操作數據庫。下面看一下整個整合的步驟:

3.1 先寫service接口

public interface ItemsService {

    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo) throws Exception;

}
  • 1
  • 2
  • 3
  • 4
  • 5

  可以看出,這個接口和上面那個mapper接口其實是一樣的,當然並不是說一定一樣,只不過這裏要實現的邏輯都一樣而已。

3.2 service實現類

public class ItemsServiceImpl implements ItemsService {

    @Autowired
    private ItemsMapperCustom itemsMapperCustom;

    @Override
    public List<ItemsCustom> findItemsList(ItemsQueryVo itemsQueryVo)
            throws Exception {

        //通過itemsMapperCustom查詢數據庫
        return itemsMapperCustom.findItemsList(itemsQueryVo);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

  可以看出,實現類中是通過@Autowired注入itemsMapperCustom,這個itemsMapperCustom是上面那個插曲中定義的一個mapper。它會通過spring配的掃描器掃描到,並將對象裝到spring容器中,然後在這注入進來,然後調用findItemsList方法來操作數據庫。至於itemQueyVo,實際中,是將前臺傳來的數據封裝進來,然後傳進來的。這樣就打通了service與dao之間的通道了。

3.3 配置applicationContext-service.xml

  這裏是第二個spring的配置文件了,還是在config/spring文件夾下面,主要是用來配置所有的service的,如下:

<?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:mvc="http://www.springframework.org/schema/mvc"
       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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

   <!-- 商品管理的service -->
   <bean id="itemsService" class="ssm.service.impl.ItemsServiceImpl"/>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

  當然咯,如果使用註解的話,這裏就不用配了,先用xml的方式吧。

3.4 配置applicationContext-transaction.xml

  這裏是第三個spring的配置文件了,還是在config/spring文件夾下面,主要是用來配置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:mvc="http://www.springframework.org/schema/mvc"
       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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

   <!-- 事務管理器 -->
   <!-- 對mybatis操作數據事務控制,spring使用jdbc的事務控制類 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 數據源dataSource在applicationContex-dao.xml中配置了 -->
        <property name="dataSource" ref="dataSource"/>
   </bean>

   <!-- 通知 -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
            <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
   </tx:advice>

   <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* ssm.service.impl.*.*(..))"/>
   </aop:config>

</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

  這樣service層就整合完了。接下來就是整合springmvc了。

4. 整合springmvc

  上面提到過,springmvc是spring的一個模塊,所以不需要整合,我們只需要加入springmvc所需的包即可,將springmvc文件夾下的jar包導入到lib中即可。關於springmvc的使用,在前面幾篇文章都寫了,這裏爲了完整性,也總結一下。

4.1 配置前端控制器

  前端控制器要配置在WEB-INF/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" id="WebApp_ID" version="3.0">
  <display-name>SpringMVC_Study</display-name>
  <welcome-file-list> 
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置前端控制器DispatcherServlet -->
  <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:springmvc.xml</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>  
</web-app>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

4.2 配置處理器映射器、處理器適配器和視圖解析器

  這裏使用註解的方式配置,因爲註解的方式比較簡單。配置在springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       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-4.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd 
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 一個配置節解決映射器和適配器的配置註解配置 --> 
    <mvc:annotation-driven></mvc:annotation-driven>

    <!-- 掃描所有的Controller -->
    <context:component-scan base-package="ssm.controller"></context:component-scan>

    <!-- 配置視圖解析器 
        進行jsp解析,默認使用jstl標籤,classpath下得有jstl的包
    -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" />
</beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

4.3 編寫Controller(即Handler)

  接下來寫一個Controller,如下:

@Controller
public class ItemsController {

    @Autowired
    private ItemsService itemsService;

    @RequestMapping("/queryItems")
    public ModelAndView queryItems() throws Exception {

        //調用service查找數據庫,查詢商品列表
        //這裏傳入進去一個null表示沒有附加條件,查詢所有的。因爲service中接收的是一個ItemsQueryVo對象
        List<ItemsCustom> itemsList = itemsService.findItemsList(null);

        //返回ModelAndView
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("itemsList", itemsList);
        modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

        return modelAndView;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

  前臺jsp頁面還是第二篇博文中寫的那個,沒有變,就不寫了。最後還有個重要的步驟就是加載spring容器。

4.4 加載spring容器

  在web.xml中添加spring容器監聽器,加載spring容器:

<!-- 加載spring容器 -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring/applicationContext-*.xml</param-value>
 </context-param>
 <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

  到此爲止,spring、mybatis和springmvc就整合完了,開啓tomcat,然後在瀏覽器中輸入http://localhost:8080/SpringMVC_Study/queryItems.action即可查詢出Items表中的信息,說明整合完成。

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