溫馨提示×

溫馨提示×

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

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

分布式商城項目SSM整合

發(fā)布時間:2020-07-30 18:55:01 來源:網(wǎng)絡(luò) 閱讀:515 作者:yc王志威 欄目:編程語言

一、SSM 框架整合思路

一個項目中往往有三層即 Dao 層、 Service 層和 Web 層。 在整合之前, 分析一下 SSM 這三大框架的整合思路。

1.1 dao 層

1、 在 dao 層中, mybatis 整合 spring, 通過 spring 管理 SqlSessionFactory、 mapper 代理對象。
在整合過程中, 需要 mybatis 和 spring 的整合包。 整合包如下:

<!-- mybatis 與 spring 繼承 -->
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
</dependency>

2、 使用 mybatis 框架, 須創(chuàng)建該框架的核心配置文件——mybatis-config.xml。
3、 使用 spring 框架, 須創(chuàng)建一個 spring-dao.xml 配置文件, 該文件的內(nèi)容有:
1) 配置數(shù)據(jù)源。
2) 需要讓 spring 容器管理 SqlsessionFactory, 其是單例存在的。
3) 把 mapper 的代理對象放到 spring 容器中, 使用掃描包的方式加載 mapper 的代理對象。

1.2 Service 層

所有的 service 實現(xiàn)類都要放到 spring 容器中管理。 由 spring 創(chuàng)建數(shù)據(jù)庫連接池, 并由spring 來管理事務(wù)。

整合內(nèi)容 對應(yīng)工程
Service 接口 ycshop-manager-interfaces
Service 實現(xiàn)類 ycshop-manager-service
Spring-service.xml 配置文件 ycshop-manager-service
1.3 Web 層(表現(xiàn)層)

表現(xiàn)層由 springmvc 來管理 controller。 總的來說, springmvc 框架的核心配置文件的內(nèi)
容有:
1. 需要掃描 controller
2. 配置注解驅(qū)動
3. 配置視圖解析器

二 dao 整合

2.1 mybaits-config 配置文件

在項目 ycshop-manager-service 工程中創(chuàng)建 mybatis-config.xml 文件。
分布式商城項目SSM整合
內(nèi)容如下:

<?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>
    <!-- mybatis 的分頁插件, 這個可以沒有。 但是這個配置文件必須要有 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="mysql" />
        </plugin>
    </plugins>
</configuration>
2.2 數(shù)據(jù)源配置文件 db.properties

將與數(shù)據(jù)庫的連接屬性配置到配置文件中, 方便修改。 具體內(nèi)容如下:

jdbc.url=jdbc:mysql://47.100.x.x:3306/ycshop?characterEncoding=utf-8
jdbc.user=xxx
jdbc.pwd=aaa
jdbc.driver=com.mysql.jdbc.Driver
jdbc.initPoolSize=5
jdbc.maxPoolSize=10

其中47.100.x.x是數(shù)據(jù)庫url
ycshop是數(shù)據(jù)庫名
xxx是數(shù)據(jù)庫連接的用戶名
aaa是數(shù)據(jù)庫連接的密碼

2.3 spring-dao.xml 配置文件

在這個配置文件中配置數(shù)據(jù)庫連接池、 SqlSessionFactory(Mybatis 的連接工廠)、 Mybatis
映射文件的包掃描器, 配置內(nèi)容如下:

<?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"
    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.3.xsd">
    <!-- 引入外部配置文件(數(shù)據(jù)庫的連接方式) -->
    <context:property-placeholder location="classpath:db.properties" />
    <!-- 配置 C3P0 的數(shù)據(jù)源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.pwd}"></property>
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    <!-- mapper 配置 -->
    <!-- 讓 spring 管理 sqlsessionfactory 使用 mybatis 和 spring 整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 數(shù)據(jù)庫連接池 -->
        <property name="dataSource" ref="dataSource" />
        <!-- 加載 mybatis 的全局配置文件, 雖然這個全局配置文件是空的, 但是這個全 局配置文件是必不可少的 -->
        <property name="configLocation" value="classpath:mybatis-config.xml" />
        <!-- 掃描 sql 配置文件:mapper 需要的 xml 文件 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml" />
    </bean>
    <!-- 配置 Mapper 掃描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="cn.yuechenc.manager.dao.mapper" />
    </bean>
</beans>

三 service 整合

3.1 spring-service.xml 配置文件

在此配置文件中配置所有的 service 包掃描以及事務(wù)管理配置。 具體配置文件如下:

<?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:aop="http://www.springframework.org/schema/aop"
    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.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <context:component-scan base-package="cn.yuechenc.manager.service"></context:component-scan>
    <!-- spring 聲明式事務(wù)管理控制 配置事務(wù)管理器類 -->
    <bean id="txManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!-- 配置事務(wù)增強(如何管理事務(wù), 只讀、 讀寫...) -->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" 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="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!-- aop 配置, 攔截哪些方法(切入點表達式, 攔截上面的事務(wù)增強) -->
    <aop:config>
        <aop:pointcut id="pt"
            expression="execution(* cn.yuechenc.manager.service.*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
    </aop:config>
</beans>
3.2 web.xml 文件

在上面的整合過程中, 編寫了兩個 spring 的配置文件:spring-dao.xml;spring-service.xml。
那么那么程序是怎么知道這 2 個文件的呢? 這就需要在服務(wù)層初始化 spring 容器了, 方法是
在 ycshop-manager-service 工程下的 web.xml 文件中進行配置。
內(nèi)容如下:

<?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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
</web-app>

四 web 層(表現(xiàn)層) 整合

在ycshop-manager-web 工程中創(chuàng)建 spring-mvc.xml 文件。 如下:
分布式商城項目SSM整合
具體內(nèi)容如下:

<?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/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
        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.3.xsd">

    <context:component-scan base-package="cn.yuechenc.manager.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>
</beans>
4.2 web.xml 文件

在 ycshop-manager-web 工程中創(chuàng)建 web.xml 配置文件。 文件內(nèi)容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
    version="3.1">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!-- spring-*.xml:表示所有以 spirng-開頭, 以.xml 結(jié)束的所有文件 classpath:表示類路徑下 -->
            <param-value>classpath:spring-*.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name><!-- 匹配所有以.do 結(jié)尾的請求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

到此, 開發(fā)框架就已盡整合完成, 并且完成了一個簡單的示例程序。 但是到目前為止,
我們運行程序的時候是不會成功的。 原因很簡單, 在 web 層中并沒有對 servie 層(服務(wù)層
接口實現(xiàn)) 的引用。 而 service 層是獨立發(fā)布的, 而現(xiàn)在我們 web 層并不能引用到 service 服
務(wù)層的實現(xiàn)。
現(xiàn)在就需要使用到 dubbo 來進行服務(wù)的發(fā)布。
下一篇中來進行 dubbo 服務(wù)的發(fā)布和引用。

向AI問一下細節(jié)

免責(zé)聲明:本站發(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)容。

AI