SSM項(xiàng)目配置文件作用

一.首先配置web.xml


1.文件頭

<?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>66_ssm_student</display-name>

2.字符編碼過濾器

<filter>

<filter-name>CharactorEncoding</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>forceEncoding</param-name>

<param-value>true</param-value>

</init-param>? ?

? </filter>

<filter-mapping>

? <filter-name>CharactorEncoding</filter-name>

? <url-pattern>*</url-pattern>

? </filter-mapping>

3.配置springmvc的DispatcherServlet房官,DispatcherServlet是SpringMVC的核心控制器,就像是SpringMVC的心臟解恰,幾乎所有的請求都會經(jīng)過這個控制器,通過它沛硅,大大的降低了模塊之間的耦合度烦磁。所有學(xué)SpringMVC的同學(xué)們第一步肯定都是先配置這個Servlet,不然還寫啥SpringMVC啊


<servlet>

? <servlet-name>springmvc</servlet-name>

? <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

? <!-- 當(dāng)servlet-name的值對應(yīng)不上xml配置文件的時候厢绝,我們需要自己去配置文件路徑通常我們把spring的配置文件放在src下买决,所以路徑一定要加上classpath:文件名-->

? <init-param>

? <!-- contextConfigLocation是固定值? -->

? <param-name>contextConfigLocation</param-name>

? <param-value>classpath:spring-*.xml</param-value>

? </init-param>

? <!-- 設(shè)置為被第一個啟動的servlet -->

? <load-on-startup>1</load-on-startup>

? </servlet>

4.<servlet-mapping>含有<servlet-name>和<url-pattern>

<servlet-name>:Servlet的名字沛婴,唯一性和一致性吼畏,與<servlet>元素中聲明的名字一致。

<url-pattern>:指定相對于Servlet的URL的路徑嘁灯。該路徑相對于web應(yīng)用程序上下文的根路徑泻蚊。<servlet-mapping>將URL模式映射到某個Servlet,即該Servlet處理的URL

? <servlet-mapping>

? <servlet-name>springmvc</servlet-name>

? <url-pattern>/</url-pattern>

? </servlet-mapping>

</web-app>

二丑婿,在config文件夾下創(chuàng)建spring-context.xml.配置spring上下文

1.開頭

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"?

xmlns:context="http://www.springframework.org/schema/context"?

xmlns:mvc="http://www.springframework.org/schema/mvc"?

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?

xsi:schemaLocation="http://www.springframework.org/schema/beans?

? ? ? http://www.springframework.org/schema/beans/spring-beans-4.0.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-4.0.xsd">

2.掃描包路徑 ,通常情況下我們在創(chuàng)建spring項(xiàng)目的時候在xml配置文件中都會配置這個標(biāo)簽性雄,配置完這個標(biāo)簽后,spring就會去自動掃描base-package對應(yīng)的路徑或者該路徑的子包下面的java文件羹奉,如果掃描到文件中帶有@Service,@Component,@Repository,@Controller等這些注解的類秒旋,則把這些類注冊為bean?

注:在注解后加上例如@Component(value=”abc”)時,注冊的這個類的bean的id就是adc.

? ? ? <context:component-scan base-package="com.student"></context:component-scan>

3.自動注入

Spring為我們提供了一種極為方便注冊這些BeanPostProcessor的方式诀拭,即使用<context:annotation- config/>隱式地向 Spring容器注冊AutowiredAnnotationBeanPostProcessor迁筛、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor這4個BeanPostProcessor


? ? ? <context:annotation-config></context:annotation-config>

三耕挨,然后在config文件夾下創(chuàng)建spirng-mvc.xml细卧,配置spring-mvc

1.開頭

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"?

xmlns:context="http://www.springframework.org/schema/context"?

xmlns:mvc="http://www.springframework.org/schema/mvc"?

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?

xsi:schemaLocation="http://www.springframework.org/schema/beans?

? ? ? http://www.springframework.org/schema/beans/spring-beans-4.0.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-4.0.xsd">


2.請求映射

Spring 3.0.x中使用了mvc:annotation-driven后,默認(rèn)會幫我們注冊默認(rèn)處理請求筒占,參數(shù)和返回值的類贪庙,其中最主要的兩個類:DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter ,分別為HandlerMapping的實(shí)現(xiàn)類和HandlerAdapter的實(shí)現(xiàn)類赋铝,從3.1.x版本開始對應(yīng)實(shí)現(xiàn)類改為了RequestMappingHandlerMapping和RequestMappingHandlerAdapter插勤。

<!-- 配置HandlerMapping 得到beanname找到對應(yīng)的Controller-->? 這個不寫默認(rèn)提供

<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>

<!-- 請求映射 -->

? ? ? <mvc:annotation-driven></mvc:annotation-driven>

<!-- 支持靜態(tài)資源訪問 -->

<mvc:default-servlet-handler/>

<!-- 配置視圖解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/view/"></property>

<property name="suffix" value=".jsp"></property>

</bean>

<!-- 配置攔截器 exclude-mapping不攔截

? <mvc:interceptors>

? <mvc:interceptor>

? <mvc:mapping path="/*.do"/>

? <mvc:exclude-mapping path="/upload.dp"/>

? <bean class="com.interceptor.PassInterceptor"></bean>

? </mvc:interceptor>

? </mvc:interceptors>

? -->

<!--上傳文件--!>

<!-- 就是幫忙設(shè)置上傳的一些參數(shù) id 必須填寫

? ? ? ? 配置上傳文件的參數(shù)

? ? ? -->

? <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">

? ? <property name="defaultEncoding" value="utf-8"/>

? ? <property name="maxInMemorySize" value="10240"/><!-- 緩沖區(qū)大小 -->

? ? <property name="uploadTempDir" value="/upload/" /><!-- 上傳的文件夾 -->

? ? <property name="maxUploadSize" value="-1" /><!-- -1沒有最大上傳大小限制 -->

? </bean>


四.config下創(chuàng)建mybaits.xml配置文件

1.開頭

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

2.mapper類的簡寫

<configuration>

<!-- 讓mapper中的類可以短寫 -->

<typeAliases>

<package name="com.student.entity"/>

</typeAliases>

</configuration>

3.分頁助手

<configuration>

<plugins>

<plugin interceptor="com.github.pagehelper.PageInterceptor">

</plugin>

</plugins>

</configuration>

五.在config下配置spring-mybatis.xml配置文件

1.開頭

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"

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.0.xsd

? 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-4.0.xsd

? http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd

? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

2.配置數(shù)據(jù)源 ,有三種

(1)配置spring原生連接池 spring-jdbc

<bean id="dataSource"

class="org.springframework.jdbc.datasource.DriverManagerDataSource">

<property name="driverClassName"

value="oracle.jdbc.driver.OracleDriver"></property>

<property name="url"

value="jdbc:oracle:thin:@localhost:1521:orcl"></property>

<property name="username" value="user01"></property>

<property name="password" value="123"></property>

</bean>

(2)配置dbcp連接池,BasicDataSource提供了close()方法關(guān)閉數(shù)據(jù)源革骨,需要設(shè)定destroy-method=”close”屬性, 以便Spring容器關(guān)閉時析恋,數(shù)據(jù)源能夠正常關(guān)閉良哲;

<bean id="dataSource2"

class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName"

value="oracle.jdbc.driver.OracleDriver"></property>

<property name="url"

value="jdbc:oracle:thin:@localhost:1521:orcl"></property>

<property name="username" value="user01"></property>

<property name="password" value="123"></property>

</bean>

(3) 配置c3p0連接池,dbcp沒有自動回收空閑連接的功能,c3p0有自動回收空閑連接功能 助隧;?

兩者主要是對數(shù)據(jù)連接的處理方式不同筑凫,C3P0提供最大空閑時間,DBCP提供最大連接數(shù)并村;?

前者當(dāng)連接超過最大空閑連接時間時巍实,當(dāng)前連接就會自動斷開,DBCP當(dāng)連接數(shù)超過最大連接數(shù)時哩牍,所有連接都會被斷開棚潦;

<bean id="dataSource3"

class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close">

<property name="driverClass"

value="oracle.jdbc.driver.OracleDriver"></property>

<property name="jdbcUrl"

value="jdbc:oracle:thin:@localhost:1521:orcl"></property>

<property name="user" value="user01"></property>

<property name="password" value="123"></property>

</bean>

3.配置SqlsessionFactory,如果是spring和mybaits整合之后的配置文件,一般以這種方式實(shí)現(xiàn),SqlSessionFactory的創(chuàng)建:

<bean id="sqlSessionFactory"

class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource2"></property>

<property name="configLocation" value="classpath:mybatis.xml"></property>


4.配置MapperScannerConfig

<bean id="mapperScanner"

class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.student.dao"></property>

<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>

</bean>

5.-- 事務(wù)管理 -->

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"></property>

</bean>

<!-- 事務(wù)管理走注解,交給txManager來負(fù)責(zé)

-->

<tx:annotation-driven transaction-manager="txManager"/>


六膝昆,寫mapper文件

1.頭

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

2丸边,映射名稱空間叠必,namespace屬性

在MyBatis中,Mapper中的namespace用于綁定Dao接口的妹窖,即面向接口編程纬朝。

它的好處在于當(dāng)使用了namespace之后就可以不用寫接口實(shí)現(xiàn)類,業(yè)務(wù)邏輯會直接通過這個綁定尋找到相對應(yīng)的SQL語句進(jìn)行對應(yīng)的數(shù)據(jù)處理

.<mapper namespace="com.student.dao.IStudentMapper">

3.<!-- 1.代碼片段 -->骄呼,替代*

<sql id="allColumns">

student_id,student_name,student_age,student_gender,student_score,

delete_flag,create_time,modify_time,drop_time

</sql>

4.結(jié)果集映射 -

<resultMap type="Student" id="studentMap">

<id column="student_id" property="studentId"/>

<result column="student_name" property="studentName"/>

<result column="student_age" property="studentAge"/>

<result column="student_gender" property="studentGender"/>

<result column="student_score" property="studentScore"/>

<result column="delete_flag" property="deleteFlag"/>

<result column="create_time" property="createTime"/>

<result column="modify_time" property="modifyTime"/>

<result column="drop_time" property="dropTime"/>

</resultMap>


5.增刪改查操作

<insert id="save" parameterType="Student">

insert into student(student_id,student_name,

<trim>

<if test="studentAge!=null">student_age,</if>

<if test="studentGender!=null">student_gender,</if>

<if test="studentScore!=null">student_score,</if>

</trim>

create_time)

values(sys_guid(),#{studentName},

<trim>

<if test="studentAge!=null">#{studentAge},</if>

<if test="studentGender!=null">#{studentGender},</if>

<if test="studentScore!=null">#{studentScore},</if>

</trim>

sysdate)

</insert>

<update id="remove" parameterType="Student">

<if test="studentId!=null">

update student set delete_flag='1' where student_id=#{studentId}

and delete_flag='0'

</if>

</update>

<update id="update" parameterType="Student">

update student

<set>

<if test="studentName!=null">student_name=#{studentName},</if>

<if test="studentAge!=null">student_age=#{studentAge},</if>

<if test="studentGender!=null">student_gender=#{studentGender},</if>

<if test="studentScore!=null">student_score=#{studentScore},</if>

</set>

where student_id=#{studentId} and delete_flag='0'

</update>

<select id="queryAll" resultMap="studentMap">

select <include refid="allColumns"></include>

from student

</select>

<select id="queryById" resultMap="studentMap" parameterType="String">

select <include refid="allColumns"></include>

from student

<where>

<if test="value!=null">and student_id=#{studentId}</if>

and delete_flag='0'

</where>

</select>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末共苛,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蜓萄,更是在濱河造成了極大的恐慌俄讹,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,692評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件绕德,死亡現(xiàn)場離奇詭異患膛,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)耻蛇,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,482評論 3 392
  • 文/潘曉璐 我一進(jìn)店門踪蹬,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人臣咖,你說我怎么就攤上這事跃捣。” “怎么了夺蛇?”我有些...
    開封第一講書人閱讀 162,995評論 0 353
  • 文/不壞的土叔 我叫張陵疚漆,是天一觀的道長。 經(jīng)常有香客問我刁赦,道長娶聘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,223評論 1 292
  • 正文 為了忘掉前任甚脉,我火速辦了婚禮丸升,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘牺氨。我一直安慰自己狡耻,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,245評論 6 388
  • 文/花漫 我一把揭開白布猴凹。 她就那樣靜靜地躺著夷狰,像睡著了一般。 火紅的嫁衣襯著肌膚如雪郊霎。 梳的紋絲不亂的頭發(fā)上沼头,一...
    開封第一講書人閱讀 51,208評論 1 299
  • 那天,我揣著相機(jī)與錄音歹篓,去河邊找鬼瘫证。 笑死揉阎,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的背捌。 我是一名探鬼主播毙籽,決...
    沈念sama閱讀 40,091評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼毡庆!你這毒婦竟也來了坑赡?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,929評論 0 274
  • 序言:老撾萬榮一對情侶失蹤么抗,失蹤者是張志新(化名)和其女友劉穎毅否,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體蝇刀,經(jīng)...
    沈念sama閱讀 45,346評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡螟加,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,570評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了吞琐。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片捆探。...
    茶點(diǎn)故事閱讀 39,739評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖站粟,靈堂內(nèi)的尸體忽然破棺而出黍图,到底是詐尸還是另有隱情,我是刑警寧澤奴烙,帶...
    沈念sama閱讀 35,437評論 5 344
  • 正文 年R本政府宣布助被,位于F島的核電站,受9級特大地震影響切诀,放射性物質(zhì)發(fā)生泄漏揩环。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,037評論 3 326
  • 文/蒙蒙 一趾牧、第九天 我趴在偏房一處隱蔽的房頂上張望检盼。 院中可真熱鬧,春花似錦翘单、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,677評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至柬唯,卻和暖如春认臊,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背锄奢。 一陣腳步聲響...
    開封第一講書人閱讀 32,833評論 1 269
  • 我被黑心中介騙來泰國打工失晴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留剧腻,地道東北人。 一個月前我還...
    沈念sama閱讀 47,760評論 2 369
  • 正文 我出身青樓涂屁,卻偏偏與公主長得像书在,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子拆又,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,647評論 2 354

推薦閱讀更多精彩內(nèi)容