一、創建一個maven的web項目,導入相關的依賴jar包
二、預先搭建好springmvc的框架,這里要創建一個springmvc.xml的配置文件,這個配置文件中
1.先開啟掃描,讓他掃描controller的注解@controller,可以設置只掃描這個控制器注解,
2.開啟spring對mvc的支持,就是配置映射器和適配器
3.配置視圖解析器
4.配置過濾掉靜態資源
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--配置掃描器 只掃描控制器controller--> <context:component-scan base-package="com.music"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--配置視圖解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="InternalResourceViewResolver"> <property name="prefix" value="/WEB_INF/jsp"></property> <property name="suffix" value=".jsp"></property> </bean> <!--過濾靜態資源--> <!--<mvc:resources mapping="/js/**" location="/js/"></mvc:resources> <mvc:resources mapping="/css/**" location="/css/"></mvc:resources> <mvc:resources mapping="/images/**" location="/images/"></mvc:resources>--> <!--開啟spring對springmvc的支持--> <mvc:annotation-driven></mvc:annotation-driven> </beans>
三、配置web.xml文件
這里先配置就是說springmvc框架所需要的,dispacherServlet的一個前端控制器,還需要把之前我們配置 的springmvc .xml文件加載進來讓我們配置的掃描器能夠掃描起到作用,如果有需要的話還要對我們的傳輸的數據進行編碼格式的過濾,就需要再我們添加的控制器的上邊再加上一個過濾器,(為什么要在上邊呢,因為web.xml文件好像有規定這些配置的位置防止先后的順序)
springmvc.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--解決中文亂碼的 過濾器 監聽器要在上邊--> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置前端控制器--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加載指定的配置文件springmvc.xml--> <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>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
注:這里可以正常的測試一下,簡單的寫幾頁面,讓它能在控制器的作用下正常的跳轉就說明,這個框架搭建的沒有問題
三、搭建一個mybatis的框架
正常來說,mybatis的框架我們需要編寫mybatis-config.xml文件,如果要使用xml配置的形式就還需要配置,映射的xml文件,注解的會就不用了,后邊會說他們的區別,我們的mybatis文件的配置,
1.我們需要先配置一個配置文件,然后再把這個配置文件用properties標簽加載到我們的這個mybatis文件當中,配置全局映射的級別和開啟緩存,下邊可以配置實體類的別名,在下邊就是配置上給們的數據庫的連接的環境,事務的級別,要不要 使用連接池,用mappers標簽來配置我們的映射文件,這個文件在后邊的整合spring的時候就不需要了,所以我們就在這強掉一下mappers中有幾個屬性的使用區別
<?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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--配置掃描器 但是只讓他掃描service層和dao層 無需掃描注解--> <context:component-scan base-package="com.music"> <!--exclude不掃描--> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--整合mybatia和spring--> <!--配置連接池--> <!--配置數據源 DriverManagerDataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql:///song?serverTimezone=GMT%2B8"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!--配置sqlsessionFactory的工廠--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="factoryBean"> <property name="dataSource" ref="dataSource"></property> <!-- ****************目錄不一致的情況 要在這里添加這沒一條配置******************************************** --> <!-- *通配符來匹配 **任意文件夾下的 *任意以xml結尾的文件 --> <!-- <property name="mapperLocations" value="classpath:com/music/**/*.xml"></property>--> <!-- ************************************************************ --> </bean> <!--配置dao下的接口 掃描 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="MapperScannerConfig"> <property name="basePackage" value="com.music.dao"></property> </bean> </beans>
這樣還不算完,因為此時我們的applicationcontext。xml文件仍然沒有和我們的整個spring的框架整合起來,沒有任何的東西將我們配置的文件加載到web容器中,
所以我們就需要考慮如何將我們的文件加載進來呢?我們可以通過一個sevletcontext的域對象來加載,因為這個域對象的生命周期和我們的服務器的生命周期一致,
我們可以通過他的init的方法來配置加載我們的文件,所以我們呢要在web。xml文件中監聽這個對象的創建,
web。xml文件修改如下
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <!--設置配置文件的路徑--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--解決中文亂碼的 過濾器 監聽器要在上邊--> <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> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--配置spring的監聽器 這個監聽器他默認就只加載WEB_INF下的applicationContest.xml的文件--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--配置前端控制器--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!--加載指定的配置文件springmvc.xml--> <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>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
此時我們就就將三個框架整合差不多了,就差個事務的控制,我們一鼓作氣,配完它!!!
我們就只需要在applicationContext。xml文件中去加上事務的控制就歐克了
1.開啟一個事務管理器
2.配置這個事務的管理器通知
3.織入事務到代碼中
添加如下的代碼
<!--配置spring框架聲明式事務處理--> <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事務通知--> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--織入事務--> <aop:config> <aop:pointcut id="txpoint" expression="execution(* com.music.service.Impl.*.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"></aop:advisor> </aop:config>
編寫一個查詢和保存的功能測試一下,這就初步完成了ssm框架的整合,
開心
高興
nice
2333