SSM基本配置文件示例

對(duì)于三個(gè)框架的結(jié)合使用,導(dǎo)入jar包后需要進(jìn)行寫配置文件,其基本的配置以下做個(gè)示例:
一,web.xml

在使用時(shí)需要對(duì)web.xml文件進(jìn)行配置,主要有:
1.spring的監(jiān)視器ContextLoaderListener,快捷方式添加即可,注意需要寫入spring.xml配置文件所在的路徑:

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:spring.xml</param-value>
</context-param>
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

2.springMVC的分發(fā)器DispatcherServlet,快捷方式添加即可,注意需要寫入springMVC.xml配置文件所在路徑和控制分發(fā)的請(qǐng)求路徑(默認(rèn)/):

<servlet>
  <!-- 配置控制轉(zhuǎn)發(fā)的資源文件 -->
  <servlet-name>springDispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:springmvc.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

<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>
</filter>
<filter-mapping>
  <filter-name>characterEncodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 配置rest風(fēng)格的url PUT和DELETE請(qǐng)求-->
<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>
二,springMVC.xml

spring與springMVC整合時(shí)需要注意一點(diǎn),springMVC掃描的帶有@Controller包不能被spring監(jiān)聽(tīng)器所掃描到,否則會(huì)將springMVC中的請(qǐng)求路徑給過(guò)濾掉,所以配置文件如下:

<context:component-scan base-package="com.wxhl.edu">
   <context:include-filter type="annotation" 
     expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 視圖解析器 -->
<bean id="resourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"></property>
    <property name="suffix" value=".jsp"></property>
</bean>
<!-- 防止處理靜態(tài)請(qǐng)求 -->
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
三,spring.xml

spring.xml中主要配置的主要是封裝類和默認(rèn)對(duì)象的屬性,因此配置的較多,此處只給出必要的配置

<!-- 配置需要掃描的包,和不掃描的類,防止與spring沖突 -->
<context:component-scan base-package="com.wxhl">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>   
    
<!-- spring IOC容器 -->
<!--1. 數(shù)據(jù)源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <!-- 指定連接數(shù)據(jù)庫(kù)的JDBC驅(qū)動(dòng) -->  
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>  
    <!-- 連接數(shù)據(jù)庫(kù)所用的URL -->  
    <property name="jdbcUrl"  value="jdbc:mysql://localhost:3306/oa?useSSL=false&characterEncoding=utf-8"/>  
    <!-- 連接數(shù)據(jù)庫(kù)的用戶名 -->  
    <property name="user" value="root"/>  
    <!-- 連接數(shù)據(jù)庫(kù)的密碼 -->  
    <property name="password" value="wxhl"/>  
    <!-- 設(shè)置數(shù)據(jù)庫(kù)連接池的最大連接數(shù) -->  
    <property name="maxPoolSize" value="20"/>  
    <!-- 設(shè)置數(shù)據(jù)庫(kù)連接池的最小連接數(shù) -->  
    <property name="minPoolSize" value="2"/>  
    <!-- 設(shè)置數(shù)據(jù)庫(kù)連接池的初始化連接數(shù) -->  
    <property name="initialPoolSize" value="2"/>  
    <!-- 設(shè)置數(shù)據(jù)庫(kù)連接池的連接的最大空閑時(shí)間,單位為秒 -->  
    <property name="maxIdleTime" value="20"/>  
</bean>
<!-- 2.mybatis的SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"></property>
    <property name="typeAliasesPackage" value="com.wxhl.edu.entity"></property>
</bean>
<!-- 3.mybatis自動(dòng)掃描加載的SQL映射文件 /接口    MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.wxhl.edu.mapper"/>
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!-- 4.事務(wù)管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     <property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 5.使用聲明式事務(wù) -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- 6.加載日志管理配置 -->
<bean id="propertyConfigurer"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations" >  
        <list>  
            <value>classpath:log4j.properties</value>  
        </list>  
    </property>  
    <property name="fileEncoding">  
        <value>UTF-8</value>  
    </property>  
</bean>
四,注意

1.持久化層使用mybatis時(shí),需要?jiǎng)?chuàng)建對(duì)應(yīng)的mapper類或者xml文件,其xml大致配置如下:

<?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.wxhl.edu.mapper.SubprojectMapper">

<insert id="addSubprojectType"  parameterType="SubprojectType" keyProperty="id" 
    keyColumn="id" useGeneratedKeys="true">
     insert into tb_subprojectType(nameType)  values(#{nameType})
</insert>

<update id="updateSubprojectType" parameterType="SubprojectType">
    update tb_subprojectType set  nameType =#{nameType}
</update>
</mapper>

2.sprinMVC注解使用,請(qǐng)參閱:http://www.itdecent.cn/p/e24b279e2e6c

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,586評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,275評(píng)論 6 342
  • 最近回顧了史蒂芬?柯維《與時(shí)間有約》這本書,想和大家分享一下這本書成功轉(zhuǎn)化我的一些新觀點(diǎn)并是如何影響我的生活的,希...
    森迪Sedona閱讀 372評(píng)論 2 2
  • 什么是品牌? 美國(guó)市場(chǎng)營(yíng)銷學(xué)會(huì)對(duì)品牌的定義是:品牌是一種名稱、術(shù)語(yǔ)、標(biāo)記、符號(hào)或設(shè)計(jì),或是它們的組合運(yùn)用,其目...
    布小姐閱讀 2,468評(píng)論 0 20
  • 2017年3月26日,帝都發(fā)布了史無(wú)前例的商辦限購(gòu)政策,其后不斷補(bǔ)刀,終成絕殺 針對(duì)買方的 1)限購(gòu):交夠5年社保...
    房房房閱讀 270評(píng)論 0 0

友情鏈接更多精彩內(nèi)容