SSM框架整合理解
把IntelliJ IDEA+Maven+Spring + SpringMVC + MyBatis項目部署姨涡,框架流程梳理調(diào)試了一遍梗逮,加深自己的理解。
回顧SSM框架
Spring
Spring就像是整個項目中裝配bean的大工廠绣溜,在配置文件中可以指定使用特定的參數(shù)去調(diào)用實體類的構(gòu)造方法來實例化對象慷彤。
Spring的核心思想是IoC(控制反轉(zhuǎn)),即不再需要程序員去顯式地new
一個對象怖喻,而是讓Spring框架幫你來完成這一切底哗。
SpringMVC
SpringMVC在項目中攔截用戶請求,它的核心Servlet即DispatcherServlet承擔(dān)中介或是前臺這樣的職責(zé)锚沸,將用戶請求通過HandlerMapping去匹配Controller跋选,Controller就是具體對應(yīng)請求所執(zhí)行的操作。SpringMVC相當(dāng)于SSH框架中struts哗蜈。
mybatis
mybatis是對jdbc的封裝前标,它讓數(shù)據(jù)庫底層操作變的透明。mybatis的操作都是圍繞一個sqlSessionFactory實例展開的距潘。mybatis通過配置文件關(guān)聯(lián)到各實體類的Mapper文件炼列,Mapper文件中配置了每個類對數(shù)據(jù)庫所需進行的sql語句映射。在每次與數(shù)據(jù)庫交互時音比,通過sqlSessionFactory拿到一個sqlSession俭尖,再執(zhí)行sql命令。
SSM框架流程
SSM框架搭建
創(chuàng)建Maven的Web項目
- 通過IntelliJ IDEA創(chuàng)建maven項目:
- 選中Createfrom archetype,選擇maven-archetype-webapp
- 在Properties中添加一個參數(shù) archetypeCatalog=internal稽犁,提高maven項目構(gòu)建速度
- SSH框架Web項目框架
- main:
- 創(chuàng)建java文件夾:項目代碼
- resources文件夾:
- mapping文件夾:數(shù)據(jù)庫表xml
- xml配置文件
- webapp:
- WEB-INF:
- 創(chuàng)建jsp文件夾:不同顯示頁面
- web.xml:配置文件
- WEB-INF:
- main:
- Tomcat啟動項目
- 為項目配置Tomcat
配置各種XML
-
pom.xml——引入項目所需要的jar包
- spring核心依賴
- mybatis依賴
- mybatis-spring整合包依賴
- mysql驅(qū)動依賴
- 其他依賴:
- 日志相關(guān):log4j焰望、slf4j
- 連接池相關(guān):commons-dbcp、c3p0已亥、Druid
- Json相關(guān):fastjson
- 其他:jstl
- PS:此外還有SpringBoot可以簡化xml中的配置項數(shù)量熊赖。SpringBoot完全拋棄了繁瑣的XML文件配置方式,而是替代性地用注解方式來實現(xiàn)虑椎。
- 參考文章:IDEA下從零開始搭建SpringBoot工程
- 調(diào)試過程中的錯誤有很大一部分是所引的jar沒有在pom.xml配置秫舌,這部分需要仔細細致。
- 關(guān)于jar包的版本號的修改绣檬,可以在<properties></properties>標(biāo)簽中用變量保存版本號,<dependencies></dependencies>中具體的jar包的版本用變量代替嫂粟,方便后續(xù)修改娇未。
-
web.xml
- 這是整個web項目的配置文件。
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>cloudmusic_ssm_demo</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.properties</param-value> </context-param> <!-- 編碼過濾器 --> <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> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- spring監(jiān)聽器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 防止spring內(nèi)存溢出監(jiān)聽器星虹,比如quartz --> <listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class> </listener> <!-- spring mvc servlet--> <servlet> <servlet-name>SpringMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>SpringMVC</servlet-name> <!-- 此處也可以配置成 *.do 形式 --> <url-pattern>/</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </welcome-file-list> <!-- session配置 --> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
- <servlet>中的配置零抬,加載SpringMVC的配置文件。
- SpringMVC具有統(tǒng)一的入口DispatcherServlet宽涌,所有的請求都通過DispatcherServlet平夜。DispatcherServlet是前置控制器,配置在web.xml文件中的卸亮。攔截匹配的請求忽妒,Servlet攔截匹配規(guī)則要自已定義,把攔截下來的請求兼贸,依據(jù)某某規(guī)則分發(fā)到目標(biāo)Controller來處理段直。
- 攔截所有的請求,并加載所有的ssm配置文件(路徑為classpath:spring-mvc.xml)
- 在web.xml中使用contextConfigLocation參數(shù)定義要裝入的Spring配置文件溶诞。
- 加載路徑為classpath:spring-mybatis.xml文件
- 參考文章: SSM:spring+springmvc+mybatis框架中的XML配置文件功能詳細解釋
-
spring-mvc.xml
<?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-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 自動掃描 @Controller--> <context:component-scan base-package="com.ssm.demo.controller"/> <!--避免IE執(zhí)行AJAX時鸯檬,返回JSON出現(xiàn)下載文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/> <!-- JSON轉(zhuǎn)換器 --> </list> </property> </bean> <!-- 定義跳轉(zhuǎn)的文件的前后綴 螺垢,視圖模式配置 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp"/> </bean> <!-- 文件上傳配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默認編碼 --> <property name="defaultEncoding" value="UTF-8"/> <!-- 上傳文件大小限制為31M喧务,31*1024*1024 --> <property name="maxUploadSize" value="32505856"/> <!-- 內(nèi)存中的最大值 --> <property name="maxInMemorySize" value="4096"/> </bean> </beans>
- controller注入:使用組件掃描方式,掃描包下面所有的Controller枉圃,可以使用注解來指定訪問路徑功茴。
- Spring 所有功能都在 Bean 的基礎(chǔ)上演化而來,所以必須事先將 Controller 變成 Bean孽亲。配置了一個 AnnotationMethodHandlerAdapter痊土,它負責(zé)根據(jù) Bean 中的 Spring MVC 注解對 Bean 進行加工處理,使這些 Bean 變成控制器并映射特定的 URL 請求墨林。
- 視圖解析:在Controller中設(shè)置視圖名的時候會自動加上前綴和后綴赁酝。
-
spring-mybatis.xml:Spring與MyBatis的整合配置文件
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 自動掃描 --> <context:component-scan base-package="com.ssm.demo"/> <!-- 第一種方式:加載一個properties文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"/> </bean> <!-- 第二種方式:加載多個properties文件 <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> <value>classpath:common.properties</value> </list> </property> <property name="fileEncoding" value="UTF-8"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean> --> <!-- 配置數(shù)據(jù)源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driverClass}"/> <property name="url" value="${jdbcUrl}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <!-- 初始化連接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 連接池最大數(shù)量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 連接池最大空閑 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 連接池最小空閑 --> <property name="minIdle" value="${minIdle}"></property> <!-- 獲取連接最大等待時間 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- mybatis和spring完美整合犯祠,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!-- 自動掃描mapping.xml文件 --> <property name="mapperLocations" value="classpath:mapping/*.xml"></property> </bean> <!-- DAO接口所在包名,Spring會自動查找其下的類 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.ssm.demo.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- (事務(wù)管理)transaction manager, use JtaTransactionManager for global tx --> <tx:annotation-driven transaction-manager="transactionManager"/> </beans>
- 自動掃描酌呆,自動注入衡载,配置數(shù)據(jù)庫
- 自動掃描,將標(biāo)注Spring注解的類自動轉(zhuǎn)化Bean,同時完成Bean的注入
- 加載數(shù)據(jù)資源屬性文件
- 配置數(shù)據(jù)源(三種方式隙袁,采用DBCP)
- 配置sessionfactory
- 裝配Dao接口
- 聲明式事務(wù)管理
- 注解事務(wù)切面
- Mapper.xml映射文件中定義了操作數(shù)據(jù)庫的sql痰娱,每一個sql是一個statement,映射文件是myBatis的核心菩收。
- 自動掃描酌呆,自動注入衡载,配置數(shù)據(jù)庫
-
jdbc.properties:JDBC屬性文件
driverClass=com.mysql.jdbc.Driver jdbcUrl=jdbc:mysql://localhost:3306/db_ssm?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull username=root password=147789 #定義初始連接數(shù) initialSize=0 #定義最大連接數(shù) maxActive=20 #定義最大空閑 maxIdle=20 #定義最小空閑 minIdle=1 #定義最長等待時間 maxWait=60000
創(chuàng)建業(yè)務(wù)流程
以數(shù)據(jù)庫查詢表內(nèi)容為例
持久層:DAO層(mapper)做數(shù)據(jù)持久層的工作梨睁,負責(zé)與數(shù)據(jù)庫進行聯(lián)絡(luò)的一些任務(wù)都封裝在此,
- DAO層的設(shè)計首先是設(shè)計DAO的接口娜饵,
- 然后在Spring的配置文件中定義此接口的實現(xiàn)類坡贺,
- 然后就可在模塊中調(diào)用此接口來進行數(shù)據(jù)業(yè)務(wù)的處理,而不用關(guān)心此接口的具體實現(xiàn)類是哪個類箱舞,顯得結(jié)構(gòu)非常清晰遍坟,
- DAO層的數(shù)據(jù)源配置,以及有關(guān)數(shù)據(jù)庫連接的參數(shù)都在Spring的配置文件中進行配置晴股。
業(yè)務(wù)層:Service層 主要負責(zé)業(yè)務(wù)模塊的邏輯應(yīng)用設(shè)計愿伴。
- 首先設(shè)計接口,再設(shè)計其實現(xiàn)的類
- 接著再在Spring的配置文件中配置其實現(xiàn)的關(guān)聯(lián)电湘。這樣我們就可以在應(yīng)用中調(diào)用Service接口來進行業(yè)務(wù)處理隔节。
- Service層的業(yè)務(wù)實現(xiàn),具體要調(diào)用到已定義的DAO層的接口寂呛,
- 封裝Service層的業(yè)務(wù)邏輯有利于通用的業(yè)務(wù)邏輯的獨立性和重復(fù)利用性官帘,程序顯得非常簡潔。
表現(xiàn)層:Controller層(Handler層)負責(zé)具體的業(yè)務(wù)模塊流程的控制
- 在此層里面要調(diào)用Service層的接口來控制業(yè)務(wù)流程昧谊,
- 控制的配置也同樣是在Spring的配置文件里面進行刽虹,針對具體的業(yè)務(wù)流程,會有不同的控制器呢诬,我們具體的設(shè)計過程中可以將流程進行抽象歸納涌哲,設(shè)計出可以重復(fù)利用的子單元流程模塊,這樣不僅使程序結(jié)構(gòu)變得清晰尚镰,也大大減少了代碼量阀圾。
模型層:Model層 主要存放實體類
項目代碼結(jié)構(gòu):
-
controller:
- “@RequestMapping”請求路徑映射,如果標(biāo)注在某個controller的類級別上狗唉,則表明訪問此類路徑下的方法都要加上其配置的路徑初烘;最常用是標(biāo)注在方法上,表明哪個具體的方法來接受處理某次請求。
- 調(diào)用service層方法
- spring mvc 支持如下的返回方式:ModelAndView, Model, ModelMap, Map,View, String, void肾筐。本文返回的是String哆料,通過model進行使用。
-
service:建立service接口和實現(xiàn)類
- impl:接口對應(yīng)實現(xiàn)類:
- 調(diào)用Dao層的數(shù)據(jù)庫操作以及model層的實體類
- impl:接口對應(yīng)實現(xiàn)類:
-
dao
- 定義接口中的方法
- 一個Dao對應(yīng)一個對應(yīng)的mapper文件吗铐,實現(xiàn)Dao對應(yīng)的定義的接口方法
-
mapping:
- mapper.xml:實現(xiàn)dao中接口定義的方法
<?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" > <mapper namespace="com.ssm.demo.dao.UserDao"> <resultMap id="UserBaseMap" type="com.ssm.demo.model.User"> <id column="id" property="id" jdbcType="BIGINT"/> <result column="user_name" property="userName" jdbcType="VARCHAR"/> <result column="user_phone" property="userPhone" jdbcType="VARCHAR"/> <result column="user_email" property="userEmail" jdbcType="VARCHAR"/> <result column="user_pwd" property="userPwd" jdbcType="VARCHAR"/> <result column="pwd_salt" property="pwdSalt" jdbcType="VARCHAR"/> <result column="create_time" property="createTime" jdbcType="DATE"/> <result column="modify_time" property="modifyTime" jdbcType="DATE"/> <result column="is_delete" property="isDelete" jdbcType="SMALLINT"></result> </resultMap> <select id="selectUserById" parameterType="java.lang.Long" resultMap="UserBaseMap"> SELECT * FROM t_user WHERE id = #{userId} </select> <select id="selectUserByPhoneOrEmail" resultMap="UserBaseMap"> SELECT * FROM t_user WHERE user_email = #{emailOrPhone} OR user_phone = #{emailOrPhone} AND user_state = #{state} </select> <select id="selectAllUser" resultMap="UserBaseMap"> SELECT * FROM t_user </select> </mapper>
- namespace:當(dāng)前庫表映射文件的命名空間东亦,唯一的不能重復(fù)
- 映射實體類的數(shù)據(jù)類型 id:resultMap的唯一標(biāo)識
- column:庫表的字段名 property:實體類里的屬性名
- id:當(dāng)前sql的唯一標(biāo)識
- parameterType:輸入?yún)?shù)的數(shù)據(jù)類型
- 返回值的數(shù)據(jù)類型:resultMap適合使用返回值是自定義實體類的情況 ; resultType適合使用返回值的數(shù)據(jù)類型是非自定義的唬渗,即jdk的提供的類型典阵。
- {}:用來接受參數(shù)的,如果是傳遞一個參數(shù)#{id}內(nèi)容任意镊逝,如果是多個參數(shù)就有一定的規(guī)則,采用的是預(yù)編譯的形式select
-
model
- 實體屬性——對應(yīng)表中的元組的屬性
- getter和setter方法
DataBase ===> Entity ===> Mapper.xml ===> Mapper.Java ===> Service.java ===> Controller.java ===> Jsp.
參考文章
SSM框架整合(IntelliJ IDEA + maven + Spring + SpringMVC + MyBatis)
SSM框架感受
本質(zhì)上的MVC壮啊,xml配置、注解撑蒜,以及mapper的映射歹啼,讓開發(fā)更加簡潔和思路清晰