搭建ssm框架項目基本原理和主要的配置文件小結


原文地址:https://blog.csdn.net/baidu_32739019/article/details/73928040

1.springmvc是spring框架的一個模塊,springmvc和spring無需通過中間整合層進行整合。springmvc是一個基於mvc的web框架。

mvc的思想大家已經很熟悉了,簡稱“Model-View-Controller”。

下面先簡單介紹下我對spring-mvc的理解。

上面這張圖大概說明了springmvc的運行過程,看起來可能有點雲里霧里的,總結起來就是下面這些:

  1. 客戶端發起請求到前端控制器(DispatcherServlet).

  2. 前端控制器請求HandlerMappering 查找Handler,可以根據xml配置、注解進行查找。

  3. DispatcherServlet將請求提交到Controller;

  4. Controller調用業務邏輯處理后,返回ModelAndView;

  5. DispatcherServlet查詢一個或多個ViewResoler視圖解析器,找到ModelAndView指定的視圖;

  6. 視圖負責將結果顯示到客戶端。

這里稍微解釋下常用的幾個組件名稱和作用。

  • 前端控制器(DispatcherServlet):用於接收請求,響應結果

  • 處理器映射器(HandlerMapping):根據請求的url查找Handler(三大核心組件之一)

  • 處理器適配器(HandlerAdapter):按照特定的規則去執行Handler(三大核心組件之一)

  • 處理器(Handler):編寫Handler時按照HandlerAdapter的要求去做,這樣適配器才可以去正確執行Handler

  • 視圖解析器(View resolver):進行視圖解析(三大核心組件之一)

  • 視圖(View):包括jsp、pdf等

    在了解了上面的基礎原理后下面來講下幾個主要的配置文件。項目開發前所需要的jar包提前要導入項目工程里面去。

 

有幾個主要的配置文件,先了解下每個配置文件的作用。

1. web.xml:當服務啟動時首先會去加載web.xml這個資源文件,里面包括了對前端控制器亂碼問題等配置。

2.applicatonContext.xml : 一般配置數據源,事務注解 ,指定SqlMapConfig.xml等。

在這里我使用的是applicatonContext-*.xml的形式將DAO層、Service層、Transaction層分開配置,這樣便於管理

分別為applicatonContext-dao.xml、applicatonContext-service.xml、applicatonContext-transaction.xml

分開配置時,需要在web.xml中配置上下文位置。

3.springmvc.xml: 里面配置的是控制層的 ,如視圖解析器靜態資源 mvc 文件上傳攔截器等。

4.SqlMapConfig.xml: 該配置文件為MyBatis的配置文件,里面無需配置,一切交給spring管理,但是xml文件基礎配置要有。

 

以下是配置文件代碼:

web.xml:處理配置applicationContext的位置,和前端控制器,前端控制器里面需要配置springMvc.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_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>ssm_boot_crm</display-name>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
    <!-- 上下文的位置 -->
    <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>
 
 
    <!-- POST提交過濾器 UTF-8 -->
    <filter>
        <filter-name>encoding</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>encoding</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>
    <!-- 前端控制器,就是對控制器的處理,像調用哪個控制器,怎么攔截之類的 -->
    <servlet>
        <servlet-name>crm</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 此處不配置 默認找 /WEB-INF/[servlet-name]-servlet.xml -->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>crm</servlet-name>
        <!-- 1:*.do *.action 攔截以.do結尾的請求 (不攔截 jsp png jpg .js .css) 
        2:/ 攔截所有請求 (不攔截.jsp) 建議使用此種 方式 (攔截 .js.css .png) (放行靜態資源) 
        3:<!-- 攔截所有請求(包括.jsp) 此種方式 不建議使用 -->
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>
</web-app>

applicationContext-dao.xml: 這個是Dao層,配置數據庫,mybatis的sqlsessionFactory,

<?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"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- srping框架 配置文件 用於管理數據庫連接池 -->
    <!-- 配置 讀取properties文件 db.properties -->
    <context:property-placeholder location="classpath:db.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的工廠 -->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 綁定數據源 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 配置Mybatis的核心 配置文件所在位置 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        <!-- 配置pojo別名 -->
        <property name="typeAliasesPackage" value="com.company.ssm.crm.pojo" />
    </bean>
    
    <!-- 配置  1:原始Dao開發 接口實現類 Mapper.xml 三個 
             2:接口開發 接口 不寫實現類 Mapper.xml 二個 (UserDao、ProductDao 
        、BrandDao。。。。。。。)
             3:接口開發、並支持掃描 cn.itcast.core.dao(UserDao。。。。。) 寫在此包下即可被掃描到 
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.company.ssm.crm.dao" />
    </bean>
    
</beans>

applicationContext-service.xml:配置掃描@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"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
 
    <!-- 配置掃描包 掃描 @Service    spring代理管理業務層 -->
    <context:component-scan base-package="com.company.ssm.crm.service" />
</beans>

applicationContext-transaction.xml:專門配置數據庫的事務管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
 
 
    <!-- spring 事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 數據源 -->
        <property name="dataSource" ref="dataSource" />
    </bean>
 
    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 傳播行為 -->
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    
    <!-- AOP 切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution(* cn.itcast.core.service.*.*(..))" />
    </aop:config>
    
</beans>

springmvc.xml:專門用來配置前端控制器的,配置@Controller注解,視圖解析器攔截什么文件

注意:mvc:annotation-driven,之前一直不理解這句話的意思:

用來注解驅動:配置處理器映射器和適配器,也就是:HandlerMapping和HandlerAdapter

<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"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.0.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/task
           http://www.springframework.org/schema/task/spring-task-4.0.xsd
        http://code.alibabatech.com/schema/dubbo        
        http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
 
    <!-- 加載屬性文件 -->
    <context:property-placeholder location="classpath:resource.properties" />
    <!-- 配置掃描 器 -->
    <context:component-scan base-package="com.company.ssm.crm.controller" />
    <!-- 配置處理器映射器 適配器 -->
    <mvc:annotation-driven />
 
    <!-- 配置視圖解釋器 jsp -->
    <bean id="jspViewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
 
</beans>

SqlMapConfig.xml(由於這個SqlMapConfig.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="com.javen.dao.IUserDao" >
  <resultMap id="BaseResultMap" type="com.javen.model.User" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_name" property="userName" jdbcType="VARCHAR" />
    <result column="password" property="password" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="INTEGER" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, user_name, password, age
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from user_t
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from user_t
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.javen.model.User" >
    insert into user_t (id, user_name, password, 
      age)
    values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, 
      #{age,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.javen.model.User" >
    insert into user_t
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="password != null" >
        password,
      </if>
      <if test="age != null" >
        age,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        #{age,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.javen.model.User" >
    update user_t
    <set >
      <if test="userName != null" >
        user_name = #{userName,jdbcType=VARCHAR},
      </if>
      <if test="password != null" >
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="age != null" >
        age = #{age,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.javen.model.User" >
    update user_t
    set user_name = #{userName,jdbcType=VARCHAR},
      password = #{password,jdbcType=VARCHAR},
      age = #{age,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>
</mapper>

db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/crm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

以上就是主要的配置文件的配置,特別注意的是在web.xml中默認加載的資源文件再WEB_INF目錄下,如果的你xml不在的話就要寫清楚的文件路徑,例如寫在src目錄下面就要寫classpath,之前開發過程中忽略了這一點,所以啟動一直報錯找不到資源文件。


免責聲明!

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



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