溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SSM框架怎么搭建

發(fā)布時間:2022-09-30 10:54:22 來源:億速云 閱讀:124 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了SSM框架怎么搭建的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SSM框架怎么搭建文章都會有所收獲,下面我們一起來看看吧。

1.創(chuàng)建一個web項目,并且導(dǎo)入相應(yīng)的jar包(spring\spring-core\spring-jdbc\spring-aop\spring-context\spring-webmvc\junit\commons-lang\mybatis\mybatis-spring等等)

2.配置web.xml文件

(1)配置監(jiān)聽器listener(類型為ContextLoaderListener),用于初始化容器加載的配置文件路徑,啟動web容器時,自動裝配applicationContext配置信息,因為它實現(xiàn)了servletContextListener接口

<!--  加載Spring容器配置 -->
          <!-- 配置ContextLoaderListener 監(jiān)聽器 -->
              作用:ContextLoaderListener的作用就是啟動Web容器時,自動裝配ApplicationContext的配置信息.因為它實現(xiàn)了ServletContextListener這個接口,在web.xml配置這個監(jiān)聽器,啟動容器時,就會默認執(zhí)行它實現(xiàn)的方法 -->         <listener>
             <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
         </listener><!--  設(shè)置Spring容器加載所有的配置文件的路徑 -->
          <context-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:config/spring/applicationContext.xml</param-value>
         </context-param>

(2)聲明與注冊一個servlet(類型為DispatcherServlet作為前端控制器,并且初始化路徑)

<!-- 配置SpringMVC核心控制器 -->
         <servlet>
             <!--配置SpringMVC的前端控制器 -->
             <servlet-name>springmvc</servlet-name>
             <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
             <!-- 配置前端控制器的路徑 -->
             <init-param>
                 <param-name>contextConfigLocation</param-name>
                 <param-value>classpath:config/springmvc/springmvc.xml</param-value>
             </init-param>
              <!--啟動加載一次 -->    
             <load-on-startup>1</load-on-startup>
         </servlet><!--  為DispatcherServlet建立映射  -->
         <servlet-mapping>
             <servlet-name>springmvc</servlet-name>
             <!--  2.4.1此處可以可以配置成*.do   -->
             <url-pattern>*.do</url-pattern>
         </servlet-mapping>

(3)注冊一個過濾器filter(編碼過濾器)

 <!-- 解決工程編碼過濾器  -->
         <filter>
             <filter-name>encodingFilter</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>encodingFilter</filter-name>
             <url-pattern>/*</url-pattern>
         </filter-mapping>

3.配置db.properties文件

數(shù)據(jù)源:jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=路徑

jdbc.username=賬號

jdbc.password=密碼

dbcp.initialSize=3

dbcp.minIdle=3

dbcp.maxIdle=5

dbcp.maxTotal=15

4.第一個會啟動spring.xml文件。

配置信息:

(1)自動掃描包

(2)注解配置

(3)引入數(shù)據(jù)源配置文件

5.第二個配置spring-mybatis配置文件

配置:

(1)配置一個數(shù)據(jù)源DruidDataSource

(2)配置SqlSessionFactoryBean類型的bean,用于注冊并掃描mapper中的sql語句

(3)配置MapperScannerConfigurer,自動掃描DAO接口;

(4)配置注解式方式事務(wù);

(5)spring-aop配置

6.配置spring-mvc.xml

(1)springmvc前端控制器掃描包

(2)啟用springMVC注解模式

(3)靜態(tài)資源配置

(4)配置json轉(zhuǎn)換器

(5)配置對模型試圖名的解析

(6)配置攔截器

關(guān)于“SSM框架怎么搭建”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“SSM框架怎么搭建”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ssm
AI