SSM整合與基本配置


Spring+SpringMVC+Mybatis整合

 

一.首先創建三個文件夾用戶存放Spring+SpringMVC+Mybatis,如下圖 

 

 

Mybatis文件夾下的配置

1.config文件內容

 

<?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>

    <!--配置指向mappe.xml的路徑-->

    <mappers>

        <mapper resource="mybatis/UserMapper.xml"/>

    </mappers>

 

</configuration>

2.UserMapper.xml文件配置

 

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

        PUBLIC "-//er 3.0//EN"

        "http://mybatimybatis.org//DTD Mapps.org/dtd/mybatis-3-mapper.dtd">

<!--指向Dao-->        

<mapper namespace="cn.yunhe.dao.UserMapper">

    <!--用戶信息查詢-->

    <select id="selectUser" parameterType="int" resultType="map">

        select * from user where id = #{id}

    </select>

 

 </mapper>

Spring文件夾下的文件配置

3.Spring-mybatis.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:context="http://www.springframework.org/schema/context"

       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.3.xsd

   http://www.springframework.org/schema/context

   http://www.springframework.org/schema/context/spring-context-4.3.xsd

   http://www.springframework.org/schema/tx

    http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

 

 

    <!--定義數據庫連接池-->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"

    init-method="close">

        <property name="url" value="jdbc:mysql://localhost:3306/practice"/>

        <property name="username" value="root"/>

        <property name="password" value="076634"/>

    </bean>

 

 <!--配置sessionFactory-->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

        <!-- 實例化sqlSessionFactory時需要使用上述配置好的數據源以及SQL映射文件 -->

        <property name="dataSource" ref="dataSource" />

        <!-- mybatis配置文件路徑 -->

        <property name="configLocation" value="classpath:mybatis/config.xml"/>

    </bean>

 

    <!--配置掃描器-->

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

        <!-- 掃描cn.yunhe.spring.mybatis.dao這個包以及它的子包下的所有映射接口類 -->

        <property name="basePackage" value="cn.yunhe.dao"/>

        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>

 

    </bean>

 

    <!--配置spring的事物管理器-->

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

        <property name="dataSource" ref="dataSource"/>

    </bean>

 

    <!--允許注解配置事物-->

    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

 

4.Spring.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:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.3.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.3.xsd">

 

    <!--自動掃描包-->

    <context:component-scan base-package="cn.yunhe"/>

    <!--讀取配置文件-->

    <import resource="spring-mybatis.xml"/>

</beans>

 

Springmvc文件下

5.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:p="http://www.springframework.org/schema/p"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:mvc="http://www.springframework.org/schema/mvc"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

            http://www.springframework.org/schema/beans/spring-beans-4.1.xsd

            http://www.springframework.org/schema/context

            http://www.springframework.org/schema/context/spring-context-4.1.xsd

            http://www.springframework.org/schema/mvc

            http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

   

     <!--掃描指定基礎包下的地址-->

    <context:component-scan base-package="cn.yunhe.controller"/>

    <!--讀取文件上傳資源文件-->

    <context:property-placeholder location="classpath:system.properties"/>

 

 

    <!--mav注解驅動-->

    <mvc:annotation-driven/>

 

    <!--jsp視圖解析器-->

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>

        <property name="prefix" value="/"/>

        <property name="suffix" value=".jsp"/>

 

    </bean>

 

    <!-- FREEMARKER視圖解析器 -->

    <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">

        <property name="prefix" value="/" />

        <property name="suffix" value=".ftl" />

        <property name="order" value="1"></property>

        <property name="contentType" value="text/html;charset=UTF-8"></property>

    </bean>

    <bean id="freemarkerConfig"

          class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">

        <property name="templateLoaderPath" value="" />

        <property name="freemarkerSettings">

            <props>

                <prop key="default_encoding">UTF-8</prop>

            </props>

        </property>

    </bean>

 

    <!--處理靜態資源-->

    <mvc:default-servlet-handler/>

 

    <!--配置SpringMVC的攔截器-->

   <mvc:interceptors>

       <bean class="cn.yunhe.controller.Intertype"/>

       <mvc:interceptor>

           <mvc:mapping path="/user/**"/>

           <mvc:exclude-mapping path="/user/get"/>

           <bean class="cn.yunhe.controller.SessionContrller"/>

       </mvc:interceptor>

       

   </mvc:interceptors>

 

    <!-- 多部分文件上傳 -->

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

        <!-- 上傳文件的大小,單位為字節 -->

        <property name="maxUploadSize" value="104857600" />

        <property name="maxInMemorySize" value="4096" />

        <!-- 請求的編碼格式 -->

        <property name="defaultEncoding" value="UTF-8"></property>

        <!-- 上傳文件的臨時路徑 -->

        <property name="uploadTempDir" value="fileUpload/temp"></property>

    </bean>

</beans>

 

 

Web.xml下的配置

 

<?xml version="1.0" encoding="UTF-8"?>

<web-app 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"

         version="2.5">

 

  <!-- 初始化 -->

  <context-param>

    <param-name>contextConfigLocation</param-name>

    <param-value>classpath:/sping/spring.xml</param-value>

  </context-param>

  <!-- 配置spring監聽器 -->

  <listener>

    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

 

  <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:/spingmvc/springmvc.xml</param-value>

    </init-param>

  </servlet>

  <servlet-mapping>

    <servlet-name>springmvc</servlet-name>

    <url-pattern>/</url-pattern>

  </servlet-mapping>

 

</web-app>


免責聲明!

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



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