一.首先配置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>