SSM框架JAVAWEB项目中各类配置文件模板及作用

本文介绍了SSM框架(Spring、SpringMVC、MyBatis)在JAVAWEB项目中的核心配置文件,包括web.xml、applicationContext.xml、springMVC-config.xml、MyBatis映射文件和db.properties。内容涉及web应用的启动配置、Spring Bean定义、前端设置、数据库连接和MyBatis的整合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近学习ssm框架的一些东西,首先就是各种配置文件,以及文件和项目中类和接口的映射关系,稍稍总结记录,以做不时之需

如有不正确的地方,请留言指正。

1、web.xml文件

这个文件是创建web项目时生成的,默认路径:main/WEB-INF/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>ssm-crud</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>
  
  <!-- 启动Spring容器 -->
  <!-- needed for ContextLoaderListener -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- SpringMVC的前端控制器,拦截所有请求 -->
  <servlet>
    <servlet-name>springDispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!--
        <init-param>
            <param-name>contextConfigLoction</param-name>
            <param-value>location</param-value>
        </init-param>
     -->
     <load-on-startup>1</load-on-startup>
  </servlet>
  <!-- Map all requests to the DispatcherServlet for.. -->
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <!-- 字符编码过滤器,一定要放在所有过滤器之前 -->
  <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>forceResponseEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <!-- 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>
    <servlet-name>/*</servlet-name>
  </filter-mapping>
  
</web-app>

关于标签更详细的说明见:https://blog.youkuaiyun.com/guihaijinfen/article/details/8363839

ssm框架整合入门系列——编写ssm整合的关键配置文件(web.xml) - 练涛 - 博客园

2、aplicationContext.xml 文件

文件一般放在resources/aplicationContext.xml

模板文件:里面都有各类标签的解释

https://download.youkuaiyun.com/download/faith_chao/10317606

<?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:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    "> <!-- 开启注解扫描 --> 
    <context:component-scan base-package="cn.dtw"></context:component-scan> 
    <!-- 读取配置文件 --> 
    <context:property-placeholder location="classpath:jdbc.properties"/> 
    <!-- 数据源 使用c3p0连接,需要jar包支持 --> 
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 
        <property name="jdbcUrl" value="${jdbcUrl}"></property> 
        <property name="driverClass" value="${driverClass}"></property> 
        <property name="user" value="${user}"></property> 
        <property name="password" value="${password}"></property> 
    </bean> 
    <!-- 集成Mybatis(sqlSessionFactory )--> 
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
        <property name="dataSource" ref="dataSource"></property> 
        <property name="typeAliasesPackage" value="cn.dtw"></property> 
    </bean> 
    <!-- 映射帮助类 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 
        <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> 
        <property name="basePackage" value="cn.dtw.dao"></property> 
    </bean> 
    <!-- 事务管理器 --> 
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
        <property name="dataSource" ref="dataSource"></property> 
    </bean> 
    <!-- 事务注解 --> 
    <tx:annotation-driven/> 
    <!-- mvc注解驱动 --> 
    <mvc:annotation-driven/> 
    <!-- 处理静态资源 --> 
    <mvc:default-servlet-handler/> 
    <!-- 注册异常处理 --> 
    <bean id="exceptionHandler" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
        <property name="defaultErrorView" value="error"></property> 
        <property name="exceptionAttribute" value="exp"></property> 
        <property name="exceptionMappings"> 
            <props> 
                <prop key="cn.dtw.exception.MyException">myError</prop> 
            </props> 
        </property> 
    </bean> 
    <!-- 支持上传文件 --> 
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
        <!-- 指定默认的编码格式,默认是ISO-8859-1 --> 
        <property name="defaultEncoding" value="utf-8"></property> 
        <!-- 上传文件的最大值,单位是字节 --> 
        <property name="maxUploadSize" value="10000000"></property> 
        <!-- 上传文件的临时文件夹 --> 
        <property name="uploadTempDir" value="tempDir"></property> 
    </bean> 
    <!-- 视图处理 --> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/jsp/"></property> 
        <property name="suffix" value=".jsp"></property> 
    </bean> 
    <!-- 使用动态代理技术 --> <!--    
        <bean id="logAspect" class="cn.dtw.aop.LogAspect"></bean>
        <aop:config>
            切入点
            <aop:pointcut expression="execution(public java.lang.Integer addCar(cn.dtw.entity.Car))" id="pointcut"/>        
            <aop:pointcut expression="execution(* cn.dtw.service.*.*(..))" id="pointcut"/>
            切入面
            <aop:aspect ref="logAspect">
                <aop:before method="before" pointcut-ref="pointcut"/>
            </aop:aspect>
        </aop:config> 
    --> 
    <bean id="logAspect" class="cn.dtw.aop.LogAspect"></bean> 
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 
</beans>

4、springMVC-config.xml

设置响应静态文件,视图解析器,拦截器、请求编码格式,多为前端的配置
 

4、mabatis映射文件相关,命名方式与对应的dao层的类名一致,一般情况下一个映射文件对应一个dao层的类

<?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.king.dao.BankOrgBeanDao">
    <resultMap id="bankOrgBean" type="bankOrgBean">
        <result property="id" column="id"/>
        <result property="channelBankId" column="channelBankId"/>
        <result property="channelBankName" column="channelBankName"/>
        <result property="createTime" column="createTime"/>
        <result property="thirtpartyDecisionStatus" column="thirtpartyDecisionStatus"/>
        <result property="thirdPartyList" column="thirdPartyList"/>
    </resultMap>

    <select id="findAllList" parameterType="String" resultMap="bankOrgBean">
        SELECT
        id,
        channelBankId,
        channelBankName,
        createTime,
        thirtpartyDecisionStatus,
        thirdPartyList
        FROM  t_bank_org
        WHERE id != null and id !=''
    </select>
</mapper>

5、db.propreties

主要是以键值对的形式配置连接的数据库驱动、地址、用户名、访问密码,以及连接池【此配置也可忽略】的相关配置

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://10.22.2.2:3306/databases
jdbc.username=localhost
jdbc.password=localhost
c3p0.acquireIncrement=3
c3p0.initialPoolSize=3
c3p0.idleConnectionTestPeriod=900
c3p0.minPoolSize=2
c3p0.maxPoolSize=400
c3p0.maxStatements=100
c3p0.numHelperThreads=10
c3p0.maxIdleTime=600
c3p0.maxConnectionAge=600

6、mybaties-config.xml文件

6.1、为dao层的类提供一个映射【别名】,以简化代码

6.2、整合mabatis映射文件到配置中,以便applicationContext.xml中sqlseeesion【sqlFactory】解析配置文件中的sql语句

     <!--导入数据库包-->
     <context:property-placeholder location="classpath:db.properties"/>

可以以配置文件的方式注入,也可以使用标签注入:

<!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>

       <property name="url" value="jdbc:mysql://10.22.2.2:3306/data_center"/>
        <property name="username" value="locathose1"/>
        <property name="password" value="locathose1"/>
    </bean>

模板如下:

<?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="logImpl" value="LOG4J"/>-->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <typeAliases>
        <typeAlias alias="merchantComputeDTO" type="com.froad.dc.bean.MerchantCompute"/>
        <typeAlias alias="outletComputeDTO" type="com.froad.dc.bean.OutletCompute"/>
        <typeAlias alias="schedulerTaskDTO" type="com.froad.dc.bean.SchedulerTask"/>
        <typeAlias alias="userInfoComputeDTO" type="com.froad.dc.bean.UserInfoCompute"/>        
    </typeAliases>


    <!-- dao映射 -->
    <mappers>

        <mapper resource="mapper/taskDao.xml"/>
        <mapper resource="mapper/merchantDao.xml"/>
        <mapper resource="mapper/outletDao.xml"/>        
        <mapper resource="mapper/orderProductDao.xml"/>

    </mappers>
</configuration>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值