教你搭建SSM项目框架

搭建一个SSM框架有三个基本的配置文件
- Spring的配置文件ApplicationContext.xml
- SpringMVC的配置文件DispatcherServlet.xml
- mybatis的配置文件 mybatis-config.xml
当然还有配置整个web容器的web.xml文件.还有其他的许多高级特性,我们在使用的时候再进行配置

web.xml

<pre style="background-color:#272822;color:#f8f8f2;font-family:'Fira Code';font-size:10.8pt;">

<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>Archetype Created Web Applicationdisplay-name>
  
 
  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>
  
  <servlet>
    <servlet-name>dispatcherServletservlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    <load-on-startup>1load-on-startup>
  servlet>
 <servlet-mapping>
   <servlet-name>dispatcherServletservlet-name>
   <url-pattern>/url-pattern>
 servlet-mapping>
  
  <filter>
    <filter-name>CharacterEncodingFilterfilter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
    <init-param>
      <param-name>encodingparam-name>
      <param-value>utf-8param-value>
    init-param>
    <init-param>
      <param-name>forceRequestEncodingparam-name>
      <param-value>trueparam-value>
    init-param>
    <init-param>
     <param-name>forceResponseEncodingparam-name>
     <param-value>trueparam-value>
    init-param>
 filter>
 <filter-mapping>
   <filter-name>CharacterEncodingFilterfilter-name>
   <url-pattern>/*url-pattern>
 filter-mapping>
  
  <filter>
    <filter-name>HiddenHttpMethodFilterfilter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
 filter>
 <filter-mapping>
   <filter-name>HiddenHttpMethodFilterfilter-name>
   <url-pattern>/*url-pattern>
 filter-mapping>
web-app>
pre>

在这里我们主要把Spring加入到web容器中去,配置spring的配置文件的位置,再让Web容器一启动就启动Spring容器,实际上Spring容器是作为一个处理所有请求的Servlet容器存在的,只不过他的init时间是容器启动时刻罢了.

ApplicationContext.xml


<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/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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="com.atguigu">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller">context:exclude-filter>
    context:component-scan>

    
    
    <context:property-placeholder location="classpath:db.properties">context:property-placeholder>
    <bean id="pooledDataSource" 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>


    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
        <property name="dataSource" ref="pooledDataSource"/>
        
        <property name="mapperLocations" value="classpath:mapper/*.xml">property>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        
        <property name="basePackage" value="com.atguigu.crud.dao">property>
    bean>
    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory">constructor-arg>
        <constructor-arg name="executorType" value="BATCH">constructor-arg>
    bean>


    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        
        <property name="dataSource" ref="pooledDataSource"/>
    bean>

    
    <aop:config>
        
        <aop:pointcut id="txpoint" expression="execution(* com.atguigu.crud.service..*(..))">aop:pointcut>
        
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txpoint"/>
    aop:config>

    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            
            <tx:method name="*"/>
            
            <tx:method name="get*" read-only="true"/>
        tx:attributes>
    tx:advice>
    

beans>

这里还使用到了一个数据库的连接配置文件db.properties

db.properties

jdbc.jdbcurl = jdbc:mysql://localhost:3306/db-crud?characterEncoding=utf8&useSSL=true
jdbc.driverClass = com.mysql.jdbc.Driver
jdbc.user = root
jdbc.password = 960611

DispatcherServlet.xml


<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-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    
    <context:component-scan base-package="com.atguigu" use-default-filters="false">
        
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller">context:include-filter>
    context:component-scan>

    
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view">property>
        <property name="suffix" value=".jsp">property>
    bean>
    
    
    <mvc:default-servlet-handler/>
    
    <mvc:annotation-driven/>
beans>

mybatis-config.xml



<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    settings>
    <typeAliases>
        <package name="com.atguigu.crud.bean"/>
    typeAliases>
    
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">plugin>
    plugins>
configuration>

项目的目录结构:

教你搭建SSM项目框架_第1张图片

你可能感兴趣的:(spring,框架,spring,mvc,spring)