SpringMVC+Spring+Mybatis整合

  • 2020 年 9 月 27 日
  • 筆記
  1. SpringMVC+Spring+Mybatis整合

maven工程結構:

 

pom文件的依賴:

 <dependencies>
        <!–mybatis環境–>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.3</version>
        </dependency>
        <!–mysql環境–>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <!–spring整合jdbc–>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!–spring整合mybatis–>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>2.0.3</version>
        </dependency>
        <!–druid連接池–>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!–分頁插件坐標–>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <!–springmvc環境–>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <!–jackson相關坐標3個–>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>
        <!–servlet環境–>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <!–其他組件–>
        <!–junit單元測試–>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!–spring整合junit–>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>

配置文件:

spring和mybatis的整合

  • 配置service層包掃描
  • 配置連接池數據源
  • 配置mybatis的核心類(用於整合)
  • 配置mybatis的映射掃描器(用於整合)
  • 配置mybatis的分頁插件
  • 配置事務管理

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”//www.springframework.org/schema/beans”
       xmlns:context=”//www.springframework.org/schema/context”
       xmlns:tx=”//www.springframework.org/schema/tx”
       xmlns:xsi=”//www.w3.org/2001/XMLSchema-instance”
       xsi:schemaLocation=”//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd
        //www.springframework.org/schema/context //www.springframework.org/schema/context/spring-context.xsd
        //www.springframework.org/schema/tx //www.springframework.org/schema/tx/spring-tx.xsd”>
    <!–主程式對應的配置文件–>

    <!–開啟bean註解掃描–>
    <context:component-scan base-package=”com.XXX.service”/>

    <!–載入properties文件–>
    <context:property-placeholder location=”classpath*:jdbc.properties”/>

    <!–數據源–>
    <bean id=”dataSource” class=”com.alibaba.druid.pool.DruidDataSource”>
        <property name=”driverClassName” value=”${jdbc.driver}”/>
        <property name=”url” value=”${jdbc.url}”/>
        <property name=”username” value=”${jdbc.username}”/>
        <property name=”password” value=”${jdbc.password}”/>
    </bean>

    <!–整合mybatis到spring中–>
    <bean class=”org.mybatis.spring.SqlSessionFactoryBean”>
        <property name=”dataSource” ref=”dataSource”/>
        <property name=”typeAliasesPackage” value=”com.itheima.domain”/>
        <property name=”plugins”><!–分頁插件–>
            <array>
                <bean class=”com.github.pagehelper.PageInterceptor”>
                    <property name=”properties”>
                        <props>
                            <prop key=”helperDialect”>mysql</prop>
                            <prop key=”reasonable”>true</prop>
                        </props>
                    </property>
                </bean>
            </array>
        </property>
    </bean>

    <!–映射掃描–>
    <bean class=”org.mybatis.spring.mapper.MapperScannerConfigurer”>
        <property name=”basePackage” value=”com.itheima.dao”/>
    </bean>

    <!–開啟註解式事務–>
    <tx:annotation-driven transaction-manager=”txManager”/>

    <!–事務管理器–>
    <bean id=”txManager” class=”org.springframework.jdbc.datasource.DataSourceTransactionManager”>
        <property name=”dataSource” ref=”dataSource”/>
    </bean>
</beans>

 

SpringMVC環境配置

  • 註解的掃描
  • 處理器適配器和處理器映射器
  • 釋放靜態資源

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”//www.springframework.org/schema/beans”
       xmlns:context=”//www.springframework.org/schema/context”
       xmlns:mvc=”//www.springframework.org/schema/mvc”
       xmlns:xsi=”//www.w3.org/2001/XMLSchema-instance”
       xsi:schemaLocation=”//www.springframework.org/schema/beans //www.springframework.org/schema/beans/spring-beans.xsd
        //www.springframework.org/schema/mvc //www.springframework.org/schema/mvc/spring-mvc.xsd
        //www.springframework.org/schema/context //www.springframework.org/schema/context/spring-context.xsd”>

    <mvc:annotation-driven/>

    <context:component-scan base-package=”com.xxxx.controller”/>

    <mvc:default-servlet-handler/>
</beans>

注意:   <mvc:default-servlet-handler/>這個釋放靜態資源路不配置也是可以的 在web.xml中可以攔截*.do的請求是一樣的效果

web.xml文件配置(1+1+1

 

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns=”//xmlns.jcp.org/xml/ns/javaee”
         xmlns:xsi=”//www.w3.org/2001/XMLSchema-instance”
         xsi:schemaLocation=”//xmlns.jcp.org/xml/ns/javaee
         //xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd”
         version=”3.1″>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext.xml</param-value>
    </context-param>
    <!–啟動伺服器時,通過監聽器載入spring運行環境–>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <servlet>
        <servlet-name>DispatcherServlet</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>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>(這裡就可以寫*.do那麼就不需要配置釋放靜態資源了)
    </servlet-mapping>

</web-app>

到此再寫dao service controller 層就可以了  訪問資料庫測試一下就完成了ssm的基礎搭建