完成后台管理系統功能(二)有關SSM的整合


一.有關SpringMVC 以及 Spring  和 Mybatis

1.SpingMVC

  1.1  首先SpringMVC是一個很流行的MVC框架。什么是MVC框架呢,就是通過把Model,View,Controller分離,把較為復雜的web應用分成邏輯清晰的幾部分,是為了簡化開發減少出錯。還是為了組內開發人員之間的配合。總之就是一種分層工作的辦法。

       1.2  SpringMVC,是Spring的一個子框架,當然擁有Spring的特性,如依賴注入。

               通俗的講Spring和SpringMVC之間的關系 ,假如Spring是一個工具箱,那么SpringMVC就是工具箱中的一個扳手。

       1.3  Spring工作流程描述     

  1. 用戶向服務器發送請求,請求被Spring 前端控制Servelt DispatcherServlet捕獲;
  2. DispatcherServlet對請求URL進行解析,得到請求資源標識符(URI)。然后根據該URI,調用HandlerMapping獲得該Handler配置的所有相關的對象(包括Handler對象以及Handler對象對應的攔截器),最后以HandlerExecutionChain對象的形式返回;
  3. DispatcherServlet 根據獲得的Handler,選擇一個合適的HandlerAdapter。(附注:如果成功獲得HandlerAdapter后,此時將開始執行攔截器的preHandler(...)方法)
  4. 提取Request中的模型數據,填充Handler入參,開始執行Handler(Controller)。 在填充Handler的入參過程中,根據你的配置,Spring將幫你做一些額外的工作:  HttpMessageConveter: 將請求消息(如Json、xml等數據)轉換成一個對象,將對象轉換為指定的響應信息。    數據轉換:對請求消息進行數據轉換。如String轉換成Integer、Double等 。     數據根式化:對請求消息進行數據格式化。 如將字符串轉換成格式化數字或格式化日期等 。數據驗證: 驗證數據的有效性(長度、格式等),驗證結果存儲到BindingResult或Error中。
  5. Handler執行完成后,向DispatcherServlet 返回一個ModelAndView對象;
  6. 根據返回的ModelAndView,選擇一個適合的ViewResolver(必須是已經注冊到Spring容器中的ViewResolver)返回給DispatcherServlet ;
  7. ViewResolver 結合Model和View,來渲染視圖
  8. 將渲染結果返回給客戶端。

2.Spring

IOC(Inversion of Control)控制反轉

本來是由應用程序管理的對象之間的依賴關系,現在交給了容器管理,這就叫控制反轉,即交給了 IOC 容器,Spring 的 IOC 容器主要使用 DI 方式實現的。不需要主動查找,對象的查找、定位和創建全部由容器管理。

Spring 中使用注解 Bean 管理:

Spring 中,bean 都是 Spring 容器管理的,使用注解來定義和使用 bean,而不需要使用 new 來創建對象。

 

Spring 中定義 bean:

@controller

@service

@repository

@component

Spring 中得到 bean:

@autowire

@resource

@Qualifer 

 

3.Mybatis

MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的持久層框架。 MyBatis 避免了幾乎所有的 JDBC 代碼和手動設置參數以及獲取結果集。 MyBatis 可以對配置和原生 Map 使用簡單的 XML 或注解,將接口和 Java POJOs(Plain Old Java Objects,普通的 Java 對象)映射成數據庫中的記錄。

在這里可以通過 mybatis的反向工程 ,去獲取數據庫中的pojo以及mapper方法。(在后續會說到這個)

 

4.SSM整合

 我們采用 SpringMVC+Spring+Mybatis 的框架完成后台管理系統。

1、 數據操作層:

Mybatis 整合 Spring,通過 Spring 管理 SqlSessionFactory、mapper 代理對象。 

整合內容

對應工程

Pojo

jingxi-backend-pojo

Mapper 映射文件

Jingxi-backend-mapper

Mapper 接口

Jingxi-backend-mapper

sqlmapConfig.xml

Jingxi-backend-controller

applicationContext-mybatis.xml

Jingxi-backend-controller

 

2、 業務層:所有的實現類都放到 Spring 容器中管理。由 Spring 創建數據庫連接池,並有 Spring 管理實務。 

整合內容

對應工程

Service 接口及實現類

Jingxi-backend-service

applicationContext-service.xml

Jingxi-backend-controller

 

3、 表現層:

Springmvc 整合 Spring 框架,由 Springmvc 管理 controller。 

整合內容

對應工程

applicationContext-mvc.xml

Jingxi-backend-controller

Controller

Jingxi-backend-controller

web.xml

Jingxi-backend-controller

 

構建核心文件

在 jingxi-backend-controller 項目中創建下面文件夾: 

 

 

Step1 配置 Mybatis:創建 SqlMapConfig.xml 配置文件 (用於配置mybatis的全局配置文件)

 

 Step2 配置 Spring 整合 Mybatis: 創建 applicationContext-mybatis.xml 和 db.properties 文件。

在applicationContext-maybatic.xml文件中  我們配置了數據庫連接池,sqlSessionFactory(mybatis的連接工廠),以及Mybatis映射文件的包掃描器

首先要配置數據庫連接池  我們就需要加載目錄下面的db.properties 文件,加載數據庫的信息。

db.properties

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/jingxi?characterEncoding=utf-8

jdbc.username=root

jdbc.password=apj123apj

 

 applicationContext-mybatis.xml

<!--數據庫連接池-->
<!--加載配置文件-->
<context:property-placeholder location="classpath:resource/*.properties"/>
<!--數據庫連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="maxActive" value="10"/>
<property name="minIdle" value="5"/>
</bean>

<!--讓spring管理sqlsessionfactory使用mybatis和spring整合包中的-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!--數據庫連接池-->
<property name="dataSource" ref="dataSource"/>

<!--加載mybatis的全局配置文件-->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jingxi.mapper"/>
</bean>
</beans>

step3:配置spring的事物管理 applicationContext-service.xml 

在application-service.xml文件中,我們配置了

①包的掃描器,掃描所有帶有@Service注解的類

<context:component-scan base-package="com.jingxi.service"/>

②配置事物,其中事務的傳播行為需要說明一下,當接口名以save、insert、add、create、delete、upate開頭時spring會自動幫我們開啟事務(前提是我們配置了事務傳播行為),而find、select、get開頭的接口是查詢,不涉及更改數據庫,因此不需要事務,spring不會為查詢接口自動開啟事務。下面再說說切面,也就是事務的作用范圍,execution(* com.taotao.service.*.*(..))的意思是,com.taotao.service下的任意類的任意方法的任意參數及任意返回值都是事務的切入點。

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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-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
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<context:component-scan base-package="com.jingxi.service"/>
<!--事務管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--數據源-->
<property name="dataSource" ref="dataSource"/></bean>
<!--通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--傳播行為-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="create*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--切面-->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.jingxi.service.*.*(..))"/>
</aop:config>
</beans>

 

 

 

Step4: 配置表現層 SpringMVC applicationContext-mvc.xml 

在applicatContext-mvc.xml文件中配置 

①配置包掃描器,掃描帶有@Controller注解的類

②配置視圖解析器

③配置資源文件的加載

<?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:p="http://www.springframework.org/schema/p"  xmlns:context="http://www.springframework.org/schema/context"       xmlns:mvc="http://www.springframework.org/schema/mvc"  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-4.0.xsd         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

 

      <context:component-scan base-package="com.jingxi.controller" />

       <mvc:annotation-driven />

      <bean

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

            <property name="prefix" value="/WEB-INF/jsp/" />

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

      </bean>

<mvc:resources location="/WEB-INF/js/" mapping="/js/**"/>

<mvc:resources location="/WEB-INF/css/" mapping="/css/**"/>

</beans>

 

Step5:配置 web.xml 

web.xml文件主要配置了

①加載spring容器

②springmvc的前端控制器

其中前端控制器配置中<load-on-startup>1</load-on-startup>這句話的意思是tomcat啟動之后便加載DispatcherServlet,如果不配置的話,需要等請求訪問的時候才會加載DispatcherServlet

③解決post亂碼

<?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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="taotao" version="2.5">
<display-name>jingxi-backend</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<!--加載spring容器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--springmvc的前端控制器-->
<servlet>
<servlet-name>jingxi-backend</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--contextConfigLocation不是必須的,如果不配置contextConfigLocation,springmvc的配置文件默認在:WEB-INF/servlet的name+"-servlet.xml" -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jingxi-backend</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--解決post亂碼-->
<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>
</web-app>

 

Step6 添加靜態資源文件 

靜態的資源文件可以去http://download.csdn.net/detail/u012453843/9794517這個地址進行下載

 

 

Step7 構建數據庫 

step8  到此 配置ssm基本完成了 不過我們會發現我們還沒有寫對應數據庫的model  以及 各種mapper(dao)方法

           這些我們可以通過mybatis的反向工程來自動生成。

1.安裝插件

2.安裝完插件之后重新啟動 STS. 接下來創建一個新項目’MybatisMapper’: 

 

 

3.接下來修改 generatorConfig.xml 如下: 

4.然后運行生成代碼: 

5.生成如下代碼:

 

6.將生成的代碼復制到對應 module 

7.修改 jingxi-backend-mapper 的 pom 文件,添加如下內容以保證發布的時

候.xml 文件可以被發布到項目中: 

<!-- 如果不添加此節點mybatis的mapper.xml文件都會被漏掉。 -->

   <build>

                <resources>

            <resource>

                <directory>src/main/java</directory>

                <includes>

                    <include>**/*.properties</include>

                    <include>**/*.xml</include>

                </includes>

                <filtering>false</filtering>

            </resource>

        </resources>

        </build>

 

 

 

 至此,ssm框架的整合結束~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM