• 搭建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,之前开发过程中忽略了这一点,所以启动一直报错找不到资源文件。

  • 相关阅读:
    linux基本命令
    操作系统
    罗马数字和整型的关系
    LightOJ 1234 Harmonic Number(打表 + 技巧)
    LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
    LightOJ 1245 Harmonic Number (II)(找规律)
    LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
    LightOJ 1282 Leading and Trailing (快数幂 + 数学)
    LightOJ 13361336
    hdu 5510 Bazinga
  • 原文地址:https://www.cnblogs.com/alsf/p/9295549.html
Copyright © 2020-2023  润新知