搭建SSM的过程及关键配置文件

时间:2021-08-19
本文章向大家介绍搭建SSM的过程及关键配置文件,主要包括搭建SSM的过程及关键配置文件使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1. 系统目录结构

2. 资源配置文件逻辑

 /HRM_SSM/resource/applicationContext.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:aop="http://www.springframework.org/schema/aop"  
        xmlns:p="http://www.springframework.org/schema/p"  
        xmlns:tx="http://www.springframework.org/schema/tx"  
        xmlns:context="http://www.springframework.org/schema/context"  
        xsi:schemaLocation="   
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
            http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context.xsd">  
             
    <import resource="spring/spring-dao.xml"/>
    <import resource="spring/spring-service.xml"/>
    <import resource="spring/spring-mvc.xml"/>
    
    
</beans>

 /HRM_SSM/resource/db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/hrm?useSSL=false&serverTimezone=UTC& characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
jdbc.initialSize=10
jdbc.maxActive=50
jdbc.maxIdle=20
jdbc.minIdle=5
jdbc.maxWait=60000
jdbc.removeAbandonedTimeout=180
jdbc.removeAbandoned=true

/HRM_SSM/resource/log4j.properties

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

/HRM_SSM/resource/mybatis-config.xml

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

    <typeAliases>
        <!--这里给实体类取别名,方便在mapper配置文件中使用 -->
        <package name="com.gec.pojo" />
    </typeAliases>
    
    <mappers>    
        <!-- <mapper class="com.gec.mapper.UserMapper" />        
                
        <mapper class="com.gec.mapper.DeptMapper" />
        <mapper class="com.gec.mapper.DocumentMapper" />
        <mapper class="com.gec.mapper.EmployeeMapper" />
        <mapper class="com.gec.mapper.JobMapper" />
        <mapper class="com.gec.mapper.NoticeMapper" />
        <mapper class="com.gec.mapper.NotypeMapper" /> -->
        
    </mappers>

</configuration>

/HRM_SSM/resource/spring/spring-dao.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:mvc="http://www.springframework.org/schema/mvc"
       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.xsd
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd">
            
    <!-- 1.加载数据库配置文件 -->
    <context:property-placeholder location="classpath:db.properties"  />
    
    <!-- 2.配置数据源 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" scope="singleton">
            <property name="driverClassName" value="${jdbc.driver}" />  
            <property name="url" value="${jdbc.url}" />  
            <property name="username" value="${jdbc.username}" />  
            <property name="password" value="${jdbc.password}" />
            <property name="initialSize" value="${jdbc.initialSize}"/>
            <property name="maxActive" value="${jdbc.maxActive}"/>
            <property name="maxIdle" value="${jdbc.maxIdle}"/>
            <property name="minIdle" value="${jdbc.minIdle}"/>
            <property name="maxWait" value="${jdbc.maxWait}"/>
            <property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}"/>
            <property name="removeAbandoned" value="${jdbc.removeAbandoned}"/>
            <!-- sql 心跳 -->
            <property name= "testWhileIdle" value="true"/>
            <property name= "testOnBorrow" value="false"/>
            <property name= "testOnReturn" value="false"/>
            <property name= "validationQuery" value="select 1"/>
            <property name= "timeBetweenEvictionRunsMillis" value="60000"/>
            <property name= "numTestsPerEvictionRun" value="${jdbc.maxActive}"/>
    </bean>   
    
    <!-- 3.配置 sqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:mybatis-config.xml" />            
        <property name="dataSource"   ref="dataSource"/>    
    </bean>    
    

    <!-- 4.批量扫描包mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定扫描的包名 
        如果扫描多个包,每个包中间使用半角逗号分隔
        -->
        <property name="basePackage" value="com.gec.mapper"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>        
    </bean>    
            
</beans>

/HRM_SSM/resource/spring/spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">


    <!-- 1.开启注解驱动 -->
    <mvc:annotation-driven>
                <mvc:message-converters register-defaults="true">
                        <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                                <constructor-arg value="UTF-8"/>
                        </bean>
                        <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                                <property name="objectMapper">
                                        <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
                                                <property name="failOnEmptyBeans" value="false"/>
                                        </bean>
                                </property>
                        </bean>
                </mvc:message-converters>
        </mvc:annotation-driven>

    <!-- 2.防止静态资源被过滤 -->
    <mvc:default-servlet-handler />


    <!-- 3.扫描注解目录 -->
    <context:component-scan
        base-package="com.gec.controller" />

    <!-- 4.配置jsp 显示ViewResolver视图解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <!-- <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" 
            value=".jsp" /> -->
    </bean>

    <!-- 5.配置mutiles显示ViewResolver视图解析器 -->
    <!-- 配置MultipartResolver 用于文件上传 使用spring的CommosMultipartResolver -->
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
        p:defaultEncoding="UTF-8" p:maxUploadSize="5400000"
        p:uploadTempDir="fileUpload/temp">


        <!--D:\spring mvc workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\fileUpload -->
    </bean>
    <!-- defaultEncoding="UTF-8" 是请求的编码格式,默认为iso-8859-1 maxUploadSize="5400000" 
        是允许上传文件的最大值,单位为字节 uploadTempDir="fileUpload/temp" 为上传文件的临时路径 -->

    <!--6.关于拦截器的配置 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!--/** 包括路径及其子路径 -->
            <!--/admin/* 拦截的是/admin/add等等这种 , /admin/add/user不会被拦截 -->
            <!--/admin/** 拦截的是/admin/下的所有 -->
            <mvc:mapping path="/*.do" />
            <!--bean配置的就是拦截器 -->
            <bean class="com.gec.interceptor.SysInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
    
    

</beans>

/HRM_SSM/resource/spring/spring-service.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:mvc="http://www.springframework.org/schema/mvc"
    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.xsd
   http://www.springframework.org/schema/mvc
   https://www.springframework.org/schema/mvc/spring-mvc.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx.xsd">


    <!-- 1.扫描注解目录 -->
    <context:component-scan
        base-package="com.gec.service" />

    <!-- 2.配置事务管理器 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!-- 注入数据库连接池 -->
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 3开启事务注解 -->
    <tx:annotation-driven
        transaction-manager="txManager" />


</beans>

web.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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>HRM</display-name>
  
  <!-- 1.index.jsp配置 -->
  <welcome-file-list>    
    <welcome-file>index.jsp</welcome-file>    
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
 
     <!--2.encodingFilter防止乱码-->
   <filter>
       <filter-name>encodingFilter</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>encodingFilter</filter-name>
       <url-pattern>/*</url-pattern>
   </filter-mapping>
     
     
    <!--3.注册DispatcherServlet-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--关联一个springmvc的配置文件:【servlet-name】-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <!--启动级别-1-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!--/ 匹配所有的请求;(不包括.jsp)-->
    <!--/* 匹配所有的请求;(包括.jsp)-->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
   
   <!--4.Session过期时间-->
   <session-config>
       <session-timeout>15</session-timeout>
   </session-config>
  
</web-app>

3.Controller文件

4.service使用

 5.dao-maper

原文地址:https://www.cnblogs.com/cqmcu/p/15161185.html