分布式商城項(xiàng)目SSM整合-創(chuàng)新互聯(lián)

一、SSM 框架整合思路

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

創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、橋西網(wǎng)絡(luò)推廣、小程序定制開發(fā)、橋西網(wǎng)絡(luò)營銷、橋西企業(yè)策劃、橋西品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們大的嘉獎(jiǎng);創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供橋西建站搭建服務(wù),24小時(shí)服務(wù)熱線:028-86922220,官方網(wǎng)址:muchs.cn
1.1 dao 層

1、 在 dao 層中, mybatis 整合 spring, 通過 spring 管理 SqlSessionFactory、 mapper 代理對(duì)象。
在整合過程中, 需要 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)建一個(gè) spring-dao.xml 配置文件, 該文件的內(nèi)容有:
1) 配置數(shù)據(jù)源。
2) 需要讓 spring 容器管理 SqlsessionFactory, 其是單例存在的。
3) 把 mapper 的代理對(duì)象放到 spring 容器中, 使用掃描包的方式加載 mapper 的代理對(duì)象。

1.2 Service 層

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

整合內(nèi)容 對(duì)應(yīng)工程
Service 接口ycshop-manager-interfaces
Service 實(shí)現(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ū)動(dòng)
3. 配置視圖解析器

二 dao 整合

2.1 mybaits-config 配置文件

在項(xiàng)目 ycshop-manager-service 工程中創(chuàng)建 mybatis-config.xml 文件。
分布式商城項(xiàng)目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 的分頁插件, 這個(gè)可以沒有。 但是這個(gè)配置文件必須要有 -->
    <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 配置文件

在這個(gè)配置文件中配置數(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 的全局配置文件, 雖然這個(gè)全局配置文件是空的, 但是這個(gè)全 局配置文件是必不可少的 -->
        <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ù)增強(qiáng)(如何管理事務(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 配置, 攔截哪些方法(切入點(diǎn)表達(dá)式, 攔截上面的事務(wù)增強(qiáng)) -->
    <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 文件

在上面的整合過程中, 編寫了兩個(gè) spring 的配置文件:spring-dao.xml;spring-service.xml。
那么那么程序是怎么知道這 2 個(gè)文件的呢? 這就需要在服務(wù)層初始化 spring 容器了, 方法是
在 ycshop-manager-service 工程下的 web.xml 文件中進(jìn)行配置。
內(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 文件。 如下:
分布式商城項(xiàng)目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é)尾的請(qǐng)求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

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

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

本文標(biāo)題:分布式商城項(xiàng)目SSM整合-創(chuàng)新互聯(lián)
文章位置:http://muchs.cn/article36/dejipg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、品牌網(wǎng)站設(shè)計(jì)微信公眾號(hào)、定制網(wǎng)站、自適應(yīng)網(wǎng)站商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作