黄色网址大全免费-黄色网址你懂得-黄色网址你懂的-黄色网址有那些-免费超爽视频-免费大片黄国产在线观看

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節(jié)點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 詳細的SSM整合步驟

詳細的SSM整合步驟

更新時間:2022-04-24 09:08:36 來源:動力節(jié)點 瀏覽1867次

SSM集成的三個階段

Spring和spring MVC環(huán)境配置

Spring集成Mybatis配置

其他組件配置:聲明式事務、日志······

1.Spring和spring MVC環(huán)境配置

(1)依賴spring webmvc

將spring MVC的依賴包添加到pom文件中

		<依賴>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.6.RELEASE</version>
        </依賴>

(2)網(wǎng)頁端在XML文件中配置DispatcherServlet

    <小服務程序>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <初始化參數(shù)>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext*.xml</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet 映射>
        <servlet-name>springmvc</servlet-name>
        <url-模式>/</url-模式>
    </servlet-mapping>

(3)在ApplicationContext中啟用spring MVC注解模式在XML中配置spring MVC注解模式

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mv="http://www.springframework.org/schema/mvc"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task"
       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.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/task
            http://www.springframework.org/schema/task/spring-task.xsd">
    <context:component-scan base-package="com.imooc"/>
    <mvc:注解驅(qū)動>
        <!--解決響應輸出中文亂碼問題-->
        <mvc:消息轉(zhuǎn)換器>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <列表>
                        <value>文本/html;charset=utf-8</value>
                        <value>應用程序/json;charset=utf-8</value>
                    </列表>
                </屬性>
            </豆>
        </mvc:message-converters>
    </mvc:注解驅(qū)動>
    <!--排除靜態(tài)資源,提高程序處理效率-->
    <mvc:default-servlet-handler/>

(4)配置request和response字符集

為解決request中的字符集編碼問題,在web上配置了xml中的characterencoding過濾器。這是一個 POST 請求,一個 get 請求是更改 Tomcat 的服務器 XML 文件。tomcat8之后,默認get請求是按照UTF-8的字符集編碼的,所以不需要手動配置

    <過濾器>
        <過濾器名稱>字符過濾器</過濾器名稱>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <初始化參數(shù)>
            <param-name>編碼</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </過濾器>
    <過濾器映射>
        <過濾器名稱>字符過濾器</過濾器名稱>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

解決響應中的字符集編碼問題,見1.3啟用MVC中的配置內(nèi)容:spring MVC注解模式下注解驅(qū)動標簽

(5)配置FreeMarker模板引擎

給pom文件添加依賴包

freemarker:FreeMarker模板引擎

Spring context support: Spring對FreeMarker模板引擎的支持

        <依賴>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <版本>2.3.30</版本>
        </依賴>
        <依賴>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.2.6.RELEASE</version>
        </依賴>

應用程序上下文。在 XML 文件中配置 FreeMarker 模板引擎

    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
                <!--配置ftl文件存儲地址-->
        <property name="templateLoaderPath" value="/WEB-INF/ftl"></property>
        <property name="freemarkerSettings">
            <道具>
                <!--這個 UTF-8 是讀取 ftl 文件時,使用 UTF-8 來讀取 ftl 文件本身的內(nèi)容-->
                <prop key="defaultEncoding">UTF-8</prop>
            </道具>
        </屬性>
    </豆>
    <!--這個bean id "ViewResolve"r 固定名字,這是SpringMVC強制規(guī)定的;這個配置決定了使用哪個模板引擎來解析數(shù)據(jù) -->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
     	<!--這個配置意味著視圖解析器將數(shù)據(jù)與模板引擎結合起來,產(chǎn)生一個新的視圖html Fragment,輸出到響應時使用utf-8字符集編碼-->
        <property name="contentType" value="text/html;charset=utf-8"></property>
        <!--配置模板引擎擴展-->
        <property name="suffix" value=".ftl"></property>
    </豆>

(6)配置Json序列化組件

給pom文件添加依賴包

jackson core:jackson的核心

Jackson annotations:一個注解包,提供了一系列注解,可以用在實體類上,方便序列化和反序列化

Jackson databind:數(shù)據(jù)綁定包這使我們能夠與 Spring MVC 中的數(shù)據(jù)進行有效交互

        <依賴>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>杰克遜核心</artifactId>
            <版本>2.12.5</版本>
        </依賴>
        <依賴>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>杰克遜注解</artifactId>
            <版本>2.12.5</版本>
        </依賴>
        <依賴>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <版本>2.12.5</版本>
        </依賴>

2.Spring與Mybatis的集成配置

(1)POM文件依賴mybatis spring和驅(qū)動

        <!--Mybatis集成Spring引入依賴-->
        <依賴>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.2.6.RELEASE</version>
        </依賴>
        <依賴>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <版本>3.5.4</版本>
        </依賴>
        <!--Mybatis 和 spring 集成組件-->
        <依賴>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <版本>2.0.3</版本>
        </依賴>
        <!--mysql 驅(qū)動器-->
        <依賴>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <版本>8.0.25</版本>
        </依賴>
        <!--連接池-->
        <依賴>
            <groupId>com.alibaba</groupId>
            <artifactId>德魯伊</artifactId>
            <版本>1.1.14</版本>
        </依賴>

(2)應用上下文。在 XML 中配置數(shù)據(jù)源和連接池

    <!--Mybatis 與 Spring 整合配置之-->
    <!--配置數(shù)據(jù)源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <屬性名稱="網(wǎng)址"
                  value="jdbc:mysql://localhost:9999/imooc_reader?useSSL=false&characterEncoding=UTF-8&ser??verTimeZone=Asia/Shanghai&allowPublicKeyRetrieval=true"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
        <property name="initialSize" value="5"></property>
        <property name="maxActive" value="30"></property>
    </豆>

(3)應用上下文。在 XML 中配置 SqlSessionFactory

    <!--SqlSessionFactoryBean 用于根據(jù)配置信息創(chuàng)建配置文件SqlSessionFactory,我們不再需要自己創(chuàng)建代碼-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!--數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--mapper 文件存儲地址-->
        <property name="mapperLocations" value="classpath:mappers/*.xml"></property>
        <!--Mybatis Profile地址-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </豆>

(4)應用上下文。在 XML 中配置 Mapper 掃描器

    <!--配置Mapper Scanner,用于掃描com.imooc.reader.mapper包下的所有mapper接口,并根據(jù)對應的xml文檔(mapper文件)自動生成實現(xiàn)類-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imooc.reader.mapper"/>
    </豆>

(5)創(chuàng)建mybatis config xml

<!DOCTYPE 配置
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<配置>
    <設置>
        <!--開啟駝峰開關-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </設置>
</配置>

3.其他組件配置

(1)集成JUint單元測試

在pom文件中添加依賴包

        <!--單元測試依賴-->
        <依賴>
            <groupId>org.springframework</groupId>
            <artifactId>彈簧測試</artifactId>
            <version>5.2.6.RELEASE</version>
        </依賴>
        <依賴>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <版本>4.12</版本>
            <scope>測試</scope>
        </依賴>
        <依賴>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <版本>3.1.0</版本>
            <scope>提供</scope>
        </依賴>

測試文件需要添加注釋,方便初始化

@RunWith(SpringJUnit4ClassRunner.class) //這個注解表示JUnit會在運行時自動初始化IOC容器
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //這個注解指明了配置文件在哪里
公共類 TestServiceTest {
    @資源
    私有測試服務測試服務;
    @測試
    公共無效批處理導入(){
        testService.batchImport();
    }
}

(2)配置logback日志輸出

在pom文件中引入相關依賴

        <依賴>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-經(jīng)典</artifactId>
            <版本>1.2.3</版本>
        </依賴>

創(chuàng)建 logback XML 并完成相關配置

<配置>
    <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern> %d{YYYY-MM-dd HH:mm:ss} %-5level [%thread] %logger{30} - %msg%n</pattern>
            <charset>utf-8</charset>
        </編碼器>
    </appender>
    <root level="調(diào)試器">
        <appender-ref ref="console"></appender-ref>
    </root>
    <!--在幾天內(nèi)將日志保存在文件中-->
    <appender name="accessHistoryLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>d:/logs/history.%d.log</fileNamePattern>
        </rollingPolicy>
        <編碼器>
            <pattern>[%thread] %d %level %logger{30} - %msg%n</pattern>
        </編碼器>
    </appender>
    <logger name="com.imooc.reader.interceptor.LoginInterceptor" level="info" additivity="false">
        <appender-ref ref="accessHistoryLog"/>
    </logger>
</配置>
</配置>

(3)聲明式事務配置

在ApplicationContext中配置XML

    <!--聲明式事務配置-->
    <!--transactionManager 控制事務的開啟、提交和回滾-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<!--配置數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"></property>
    </豆>
    <!--啟用注釋模式的聲明性事務-->
    <tx:annotation-driven transaction-manager="transacationManager"/>

只要在方法上加上@Transactional注解,就表示開啟了聲明式事務。如果大家對此比較感興趣,想了解更多相關知識,不妨來關注一下動力節(jié)點的SpringMVC教程,教程內(nèi)容細致全面,通俗易懂,適合沒有基礎的小伙伴學習,希望對大家能夠有所幫助哦。

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 可以免费看黄的网址 | 激情五月亚洲 | 亚洲高清国产一区二区三区 | 国产一级做a爰片在线看免费 | 亚洲阿v天堂2021在线观看 | 星光影院网高清在线观看 | 一本大道在线视频 | 国产大片黄在线观看 | 欧美巨大性hd | 精品伊人久久久 | 噜噜噜噜天天狠狠 | a级黄色毛片 | 国产男女 爽爽爽爽视频 | 免费福利在线看黄网站 | 羞羞影院体验区 | 成年看片免费高清观看 | 天堂v亚洲国产v一区二区 | 日韩欧美在线综合 | 国产欧美日韩在线视频 | 欧美高清xxxx性 | 色综合成人网 | 午夜看毛片| 日日碰狠狠添天天爽爽爽 | 欧美日韩亚洲一区二区 | 欧美日韩aa一级视频 | 亚欧乱色一区二区三区 | 久99re视频9在线观看 | 亚洲天堂777 | 欧美理论片大全在线观看 | 欧美日韩国产一区二区三区不卡 | 黑人巨大videos极度另类 | 最近最新免费中文字幕一 | 免费在线欧美 | 在线一级片 | 欧美性精品hdvideosex | 中国女与老外在线精品 | 97国产大学生情侣11在线视频 | 黄色免费网站观看 | 天天看天天射天天视频 | 污黄视频网站 | 日本中文字幕二区三区 |