SSM三大框架整合

框架整合思路

springmvc+mybaits的系統(tǒng)架構(gòu)


image.png

spring將各層進(jìn)行整合

通過spring管理持久層的mapper(相當(dāng)于dao接口)
通過spring管理業(yè)務(wù)層service只厘,service中可以調(diào)用mapper接口烙丛。
spring進(jìn)行事務(wù)控制。
通過spring管理表現(xiàn)層Controller羔味,Controller中可以調(diào)用service接口河咽。
mapper、service赋元、Controller都是javabean忘蟹,可以由spring來進(jìn)行統(tǒng)一管理。

第一步:整合dao層
mybatis和spring整合搁凸,通過spring管理mapper接口媚值。
使用mapper的掃描器自動掃描mapper接口在spring中進(jìn)行注冊。
第二步:整合service層
通過spring管理 service接口坪仇。
使用配置方式將service接口配置在spring配置文件中杂腰。
實(shí)現(xiàn)事務(wù)控制。
第三步:整合springmvc
由于springmvc是spring的模塊椅文,不需要整合喂很。

所需的jar包

mysql數(shù)據(jù)庫驅(qū)動包:
mybatis的jar包
mybatis和spring整合包
log4j包
druid數(shù)據(jù)庫連接池包
spring所有jar包(4.x搭配jdk8,3.x搭配jdk7)
jstl包

工程結(jié)構(gòu)

image.png

整合dao

數(shù)據(jù)庫服務(wù)器配置文件
在classpath下創(chuàng)建db.properties

#配置信息采取name = value的形式

#1.配置驅(qū)動程序
jdbc.driver =com.mysql.jdbc.Driver

#2.配置數(shù)據(jù)庫url
jdbc.url = jdbc:mysql://localhost:3306/coder_school?useUnicode=true&characterEncoding=UTF-8

#3.配置用戶名
jdbc.username=root

#4.配置用戶密碼
jdbc.password=root

日志配置文件
在classpath下創(chuàng)建log4j.properties

# Global logging configuration
#\u751F\u4EA7\u73AF\u5883\u914D\u7F6Einfo   ERROR
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

Mybatis配置文件
在classpath下創(chuàng)建mybatis/SqlMapConfig.xml

<?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">
<configuration>
    <typeAliases>
        <package name="school.coder.domain"></package>
    </typeAliases>
</configuration>

spring管理SqlSessionFactory皆刺、mapper
在classpath下創(chuàng)建spring/ applicationContext-dao.xml
配置數(shù)據(jù)源

<?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:mvc="http://www.springframework.org/schema/mvc"
    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-4.0.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-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 ">
        
    <!-- 加載db.properties文件中的內(nèi)容少辣,db.properties文件中key命名要有一定的特殊規(guī)則 -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置數(shù)據(jù)源 ,dbcp -->

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <!-- 基本屬性 url羡蛾、user漓帅、password -->
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />

        <!-- 配置初始化大小、最小、最大 -->
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />

        <!-- 配置獲取連接等待超時(shí)的時(shí)間 -->
        <property name="maxWait" value="60000" />

        <!-- 配置間隔多久才進(jìn)行一次檢測忙干,檢測需要關(guān)閉的空閑連接器予,單位是毫秒 -->
        <property name="timeBetweenEvictionRunsMillis" value="60000" />

        <!-- 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒 -->
        <property name="minEvictableIdleTimeMillis" value="300000" />

        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />

        <!-- 打開PSCache捐迫,并且指定每個(gè)連接上PSCache的大小 -->
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />

        <!-- 配置監(jiān)控統(tǒng)計(jì)攔截的filters乾翔,去掉后監(jiān)控界面sql無法統(tǒng)計(jì) -->
        <property name="filters" value="stat" />
    </bean>
    
    <!-- sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數(shù)據(jù)庫連接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加載mybatis的全局配置文件 -->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
    </bean>
    
    <!-- mapper掃描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 掃描包路徑,如果需要掃描多個(gè)包施戴,中間使用半角逗號隔開 -->
        <property name="basePackage" value="school.coder.mapper"></property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
</beans>

有關(guān)連接池的參考

整合service

Spring管理service bean反浓,并進(jìn)行事務(wù)控制
定義service接口

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

        <context:component-scan base-package="school.coder.service.impl"></context:component-scan>


<!--     <bean id="ibs" class="com.neusoft.service.impl.AccountServiceImpl"/>
    <bean id="ius" class="com.neusoft.service.impl.UserServiceImpl"/> -->
</beans>

在applicationContext-transaction.xml中使用spring聲明式事務(wù)控制方法

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    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-4.0.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-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 ">

<!-- 事務(wù)管理器 
    對mybatis操作數(shù)據(jù)庫事務(wù)控制,spring使用jdbc的事務(wù)控制類
-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>


</beans>

測試service赞哗,在方法實(shí)現(xiàn)上添加 @Transactional注解測試事務(wù)是否生效雷则。

springmvc.xml配置文件
在classpath下創(chuàng)建spring/springmvc.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: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/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">
   
    <mvc:annotation-driven/>
   
   <mvc:resources location="/assets/" mapping="/assets/**"/>

      <!-- 文件上傳 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <property name="maxUploadSize" value="10000000"></property>
    <property name="defaultEncoding" value="UTF-8"></property>
</bean>
        
   <!-- 配置自動掃描的包 -->
   <context:component-scan base-package="school.coder.controller"></context:component-scan>
   
        <!-- 配置視圖解析器 如何把handler 方法返回值解析為實(shí)際的物理視圖 -->
     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp">
        </property>
     </bean>


<mvc:interceptors>

<!--如果配置了多個(gè)攔截器,則按順序執(zhí)行 -->

<!-- 登陸認(rèn)證攔截器 -->

<mvc:interceptor>

               <!-- /**表示所有url包括子url路徑 -->

    <mvc:mapping path="/admin/**"/>
    <mvc:exclude-mapping path="/admin/login"/>
    <bean class="school.coder.interceptor.AdminLoginInterceptor"></bean>

</mvc:interceptor>

</mvc:interceptors>
 
</beans>

web.xml配置文件
配置前端控制器
在web.xml中肪笋,添加spring容器監(jiān)聽器月劈,加載spring容器。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         id="WebApp_ID" version="3.1">


    <!-- tomcat服務(wù)器藤乙,一啟動馬上讀取spring/applicationContext-*.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext-*.xml</param-value>
    </context-param>

    <!-- tomcat服務(wù)器艺栈,一啟動就加載spring框架的ContextLoaderListener
    意味著,spring容器啟動湾盒,開始作用 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>


    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/springmvc.xml</param-value>
        </init-param>
    </servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
        <!--    攔截不了.jsp文件 -->
    </servlet-mapping>


    <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>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

三大框架整合完畢,編寫測試程序驗(yàn)證诅妹。


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末罚勾,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子吭狡,更是在濱河造成了極大的恐慌尖殃,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件划煮,死亡現(xiàn)場離奇詭異送丰,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)弛秋,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門器躏,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人蟹略,你說我怎么就攤上這事登失。” “怎么了挖炬?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵揽浙,是天一觀的道長。 經(jīng)常有香客問我,道長馅巷,這世上最難降的妖魔是什么膛虫? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮钓猬,結(jié)果婚禮上稍刀,老公的妹妹穿的比我還像新娘。我一直安慰自己逗噩,他們只是感情好掉丽,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著异雁,像睡著了一般捶障。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上纲刀,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天项炼,我揣著相機(jī)與錄音,去河邊找鬼示绊。 笑死锭部,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的面褐。 我是一名探鬼主播拌禾,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼展哭!你這毒婦竟也來了湃窍?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤匪傍,失蹤者是張志新(化名)和其女友劉穎您市,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體役衡,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡茵休,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了手蝎。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片榕莺。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖棵介,靈堂內(nèi)的尸體忽然破棺而出帽撑,到底是詐尸還是另有隱情,我是刑警寧澤鞍时,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布亏拉,位于F島的核電站扣蜻,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏及塘。R本人自食惡果不足惜莽使,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望笙僚。 院中可真熱鬧芳肌,春花似錦、人聲如沸肋层。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽栋猖。三九已至净薛,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蒲拉,已是汗流浹背肃拜。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留雌团,地道東北人燃领。 一個(gè)月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像锦援,于是被迫代替她去往敵國和親猛蔽。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容