SSM框架JAVAWEB專案中各類配置檔案模板及作用
最近學習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.csdn.net/guihaijinfen/article/details/8363839
https://www.cnblogs.com/famine/p/9792253.html
2、aplicationContext.xml 檔案
檔案一般放在resources/aplicationContext.xml
模板檔案:裡面都有各類標籤的解釋
https://download.csdn.net/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>