zoukankan      html  css  js  c++  java
  • SSM框架的基本配置

    相信不少初学习SSM框架的同学被框架各个配置文件弄晕,这些配置文件有不少配置是整合Spring,SpringMVC,Mybatis的必须基本配置,这里作下总结。

    先讲需要导入的jar包

    asm-3.2.0.RELEASE.jar
    
    asm-3.3.1.jar
    
    c3p0-0.9.jar
    
    cglib-2.2.2.jar
    
    com.springsource.net.sf.cglib-2.2.0.jar
    
    com.springsource.org.aopalliance-1.0.0.jar
    
    com.springsource.org.apache.commons.logging-1.1.1.jar
    
    com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
    
    jackson-core-asl-1.7.2.jar
    
    jackson-mapper-asl-1.7.2.jar
    
    javassist-3.17.1-GA.jar
    
    javax.servlet.jsp.jstl.jar
    
    jsf-api.jar
    
    jsf-impl.jar
    
    jstl-impl.jar
    
    junit.jar
    
    log4j-1.2.17.jar
    
    mybatis-3.2.2.jar
    
    mybatis-spring-1.2.0.jar
    
    mysql-connector-java-5.1.26-bin.jar
    
    org.hamcrest.core_1.1.0.v20090501071000.jar
    
    org.springframework.transaction-3.2.2.RELEASE.jar
    
    slf4j-api-1.7.5.jar
    
    slf4j-log4j12-1.7.5.jar
    
    spring-aop-3.2.0.RELEASE.jar
    
    spring-beans-3.2.0.RELEASE.jar
    
    spring-context-3.2.0.RELEASE.jar
    
    spring-core-3.2.0.RELEASE.jar
    
    spring-expression-3.2.0.RELEASE.jar
    
    spring-jdbc-3.2.0.RELEASE.jar
    
    spring-orm-3.2.0.RELEASE.jar
    
    spring-test-3.2.0.RELEASE.jar
    
    spring-web-3.2.0.RELEASE.jar
    
    spring-webmvc-3.2.0.RELEASE.jar
    View Code

    首先是web.xml,这是项目的全局配置文件

    这里负责:加载applicationContext.xml,springDispatcherServlet-servlet.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" version="2.5">
      <display-name>ssm-curd</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>
      
      
      <!-- 1,启动Spring容器 -->
      <!-- needed for ContextLoaderListener -->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <!-- 这里就会加载classpath:applicationContext.xml -->
            <param-value>classpath:applicationContext.xml</param-value>
        </context-param>
    
        <!-- Bootstraps the root web application context before servlet initialization -->
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        
        
        
        <!-- 2,SpringMVC的前端控制器,拦截所有的请求 -->
        <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
        <servlet>
            <servlet-name>springDispatcherServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
               <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>classpath:WEB-INF/springDispatcherServlet-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <!-- Map all requests to the DispatcherServlet for handling -->
        <servlet-mapping>
            <servlet-name>springDispatcherServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
        
        <!-- 3,配置了前端控制器就顺便把字符编码拦截器顺便配上 -->
        <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>
            <init-param>
                <param-name>forceRequestEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
            <init-param>
                <param-name>forceRequestEncoding</param-name>
                <param-value>true</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharacterEncodingFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
        
        
        <!-- 4,使用Rest风格的URI:将页面普通的post请求转化为指定的delete/put请求 -->
        <filter>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
        </filter>
        <filter-mapping>
            <filter-name>HiddenHttpMethodFilter</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>
    View Code

    然后是applicationContext.xml,这是Spring的配置文件

    负责:数据库源,指定mybatis-config.xml(mybatis全局配置文件),指定mapper/*.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: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.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
            ">
    
    <context:component-scan base-package="com.atguigu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    
    <!-- Spring的配置文件,这里主要配置和业务逻辑有关的 -->
    <!-- 数据源,事务控制。。。 -->
    <context:property-placeholder location="classpath:dbconfig.properties"/>
    <bean id="ComboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass"  value="${jdbc.driverClass}"></property>
        <property name="user"  value="${jdbc.user}"></property>
        <property name="password"  value="${jdbc.password}"></property>
    </bean>
    
    
    <!-- =============================================== -->    
    
    
    <!-- 配置和Mybatis的整合 -->
    <bean id="SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定配置mybatis全局配置文件的位置 -->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 指定Mybatis数据源 -->
        <property name="dataSource" value="ComboPooledDataSource"></property>
        <!-- 指定mybatis,mapper文件的位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
    </bean>
    
    <!-- 配置扫描器,将mybatis接口实现到ioc容器中 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描所有的Dao接口的实现,加入到ioc容器中 -->
        <property name="basePackage" value="com.atguigu.curd.dao"></property>
    </bean>
        
    
    <!-- =============================================== -->    
        
        
    <!-- 事务控制配置 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 控制住数据源 -->
        <property name="dataSource" ref="ComboPooledDataSource"></property>
    </bean>
    <!-- 开启基于注解的事务,推荐重要的事务用xml配置 -->
    <aop:config>
        <!-- 切入点表达式 -->
        <aop:pointcut expression="execution(* com.atguigu.curd.service..*(..))" id="txPoint"/>
        <!-- 配置事务增强 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
    </aop:config>
    
    <!-- 配置增强:事务如何切入 -->
    <tx:advice id="txAdvice">
        <tx:attributes>
            <!-- 所有方法都是事务方法 -->
            <tx:method name="*"/>
            <!-- 以get开始的所有方法 -->
            <tx:method name="get*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
    
    
    </beans>
    View Code

    mybatis-config.xml(mybatis全局配置文件)

    通过package, 可以直接指定package的名字, mybatis会自动扫描你指定包下面的javabean,并且默认设置一个别名,默认的名字为非限定类名来作为它的别名。

    <?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>
        <settings>
            <setting name="mapUnderscoreToCamelCase" value="true"/>
        </settings>
        <typeAliases>
            <package name="com.atguigu.curd.bean"/>
        </typeAliases>
    </configuration>
    View Code

    springDispatcherServlet-servlet.xml,这是SpringMVC的配置文件:包含网站跳转逻辑的控制配置。

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
        <!-- SpringMVC的配置文件:包含网站跳转逻辑的控制配置 -->
        <!-- 有关网站逻辑 都写-->
        
        <context:component-scan base-package="com.atguigu" use-default-filters="false">
            <!-- 只扫描控制器,禁用默认过滤规则 -->
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>
    
        <!-- 配置视图解析器,方便页面返回 -->
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
        
        <!-- 两个必须的标准配置 -->
        <!-- 将SpringMVC不能处理的请求交给tomcat -->
        <mvc:default-servlet-handler/>
        <!-- 能支持SpringMVC的更高级功能:基于JSR303校验,快捷的ajax,映射动态请求等 -->
        <mvc:annotation-driven/>
        
    </beans>
    View Code

    参考资料https://www.cnblogs.com/zzb-yp/p/9295397.html

    https://blog.csdn.net/jdbdh/article/details/83582815#2.5%E3%80%81db.properties

    https://www.jianshu.com/p/eeb48046215f

  • 相关阅读:
    从淘宝数据结构来看电子商务中商品属性设计
    YY淘宝商品数据库设计
    ElasticSearch-聚合bucket学习
    ElasticSearch-聚合bucket
    Elasticsearch(9) --- 聚合查询(Bucket聚合)
    Elasticsearch-多字段搜索(Multifield Search)和提高精确度
    五、设备管理与文件系统
    六、YUM仓库
    四、查找命令
    二、用户权限
  • 原文地址:https://www.cnblogs.com/clno1/p/12506436.html
Copyright © 2011-2022 走看看