首页天道酬勤SSM整合的方法是什么

SSM整合的方法是什么

admin 10-15 17:47 382次浏览
这篇文章主要介绍了SSM整合的方法是什么的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇SSM整合的方法是什么文章都会有所收获,下面我们一起来看看吧。

1.Spring和spring MVC环境配置

(1)依赖spring webmvc

将spring MVC的依赖包添加到pom文件中

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

(2)网页端在XML文件中配置DispatcherServlet

    <小服务程序>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <初始化参数>
            <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:注解驱动>
        <!--解决响应输出中文乱码问题-->
        <mvc:消息转换器>
            <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:注解驱动>
    <!--排除静态资源,提高程序处理效率-->
    <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>
        <初始化参数>
            <param-name>编码</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </过滤器>
    <过滤器映射>
        <过滤器名称>字符过滤器</过滤器名称>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

解决响应中的字符集编码问题,见1.3启用MVC中的配置内容:spring MVC注解模式下注解驱动标签

(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 文件本身的内容-->
                <prop key="defaultEncoding">UTF-8</prop>
            </道具>
        </属性>
    </豆>
    <!--这个bean id "ViewResolve"r 固定名字,这是SpringMVC强制规定的;这个配置决定了使用哪个模板引擎来解析数据 -->
    <bean id="ViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
     	<!--这个配置意味着视图解析器将数据与模板引擎结合起来,产生一个新的视图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:数据绑定包这使我们能够与 Spring MVC 中的数据进行有效交互

        <依赖>
            <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和驱动

        <!--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 驱动器-->
        <依赖>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <版本>8.0.25</版本>
        </依赖>
        <!--连接池-->
        <依赖>
            <groupId>com.alibaba</groupId>
            <artifactId>德鲁伊</artifactId>
            <版本>1.1.14</版本>
        </依赖>

(2)应用上下文。在 XML 中配置数据源和连接池

    <!--Mybatis 与 Spring 整合配置之-->
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
        <属性名称="网址"
                  value="jdbc:mysql://localhost:9999/imooc_reader?useSSL=false&characterEncoding=UTF-8&serverTimeZone=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 用于根据配置信息创建配置文件SqlSessionFactory,我们不再需要自己创建代码-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!--数据源-->
        <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接口,并根据对应的xml文档(mapper文件)自动生成实现类-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imooc.reader.mapper"/>
    </豆>

(5)创建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-经典</artifactId>
            <版本>1.2.3</版本>
        </依赖>

创建 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="调试器">
        <appender-ref ref="console"></appender-ref>
    </root>
    <!--在几天内将日志保存在文件中-->
    <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">
    	<!--配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </豆>
    <!--启用注释模式的声明性事务-->
    <tx:annotation-driven transaction-manager="transacationManager"/>

关于“SSM整合的方法是什么”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“SSM整合的方法是什么”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注花开半夏行业资讯频道。

SSM整合的方法是什么
Android 获取wifi连接历史记录 CMake文件配置
相关内容