栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

学习笔记:SSM框架整合步骤

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

学习笔记:SSM框架整合步骤

SSM整合三阶段

Spring与SpringMVC环境配置
Spring与Mybatis的整合配置
其他组件配置:声明式事务、日志······

一、Spring与SpringMVC环境配置

1.1 依赖Spring-webmvc
在pom文件中加入SpringMVC的依赖包

		
            org.springframework
            spring-webmvc
            5.2.6.RELEASE
        

1.2 在web.xml文件中配置DispatcherServlet

    
        springmvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext*.xml
        
        0
    
    
        springmvc
        /
    

1.3 启用SpringMVC注解模式
在applicationContext.xml中配置SpringMVC注解模式



    
    
        
        
            
                
                    
                        text/html;charset=utf-8
                        application/json;charset=utf-8
                    
                
            
        
    
    
    

1.4 配置请求与响应字符集
解决请求中的字符集编码问题,在web.xml中配置CharacterEncodingFilter,这是针对的POST请求,GET请求是要去改tomcat的server.xml文件,在tomcat8之后,默认get请求就是按照UTF-8的字符集编码,所以无需再手动配置

    
        characterFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        characterFilter
        /*
    

要解决响应中的字符集编码问题,见1.3 启用SpringMVC注解模式中mvc:annotation-driven标签中的配置内容

1.5 配置FreeMarker模板引擎
pom文件中加入依赖包
freemarker:FreeMarker模板引擎
spring-context-support:spring对FreeMarker模板引擎的支持

        
            org.freemarker
            freemarker
            2.3.30
        
        
            org.springframework
            spring-context-support
            5.2.6.RELEASE
        

applicationContext.xml文件中配置FreeMarker模板引擎

    
                
        
        
            
                
                UTF-8
            
        
    
    
    
     	
        
        
        
    

1.6 配置Json序列化组件
pom文件中加入依赖包
jackson-core:jackson的核心
jackson-annotations:注解包,提供一系列可以用在实体类上的注解,方便我们序列化与反序列化
jackson-databind:数据绑定包,通过这个数据绑定包,能够使我们与SpringMVC中的数据进行有效的交互

        
            com.fasterxml.jackson.core
            jackson-core
            2.12.5
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.12.5
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.5
        
二、Spring与Mybatis的整合配置

2.1 pom文件中依赖mybatis-spring及驱动

        
        
            org.springframework
            spring-jdbc
            5.2.6.RELEASE
        
        
            org.mybatis
            mybatis
            3.5.4
        
        
        
            org.mybatis
            mybatis-spring
            2.0.3
        
        
        
            mysql
            mysql-connector-java
            8.0.25
        
        
        
            com.alibaba
            druid
            1.1.14
        

2.2 applicationContext.xml中配置数据源与连接池

    
    
    
        
        
        
        
        
        
    

2.3 applicationContext.xml中配置SqlSessionFactory

    
    
    	
        
        
        
        
        
    

2.4 applicationContext.xml中配置Mapper扫描器

    
    
        
    

2.5 创建mybatis-config.xml



    
        
        
    

三、其他组件配置

3.1 整合JUint单元测试
pom文件中增加依赖包

        
        
            org.springframework
            spring-test
            5.2.6.RELEASE
        
        
            junit
            junit
            4.12
            test
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        

测试文件中需要加入注解,以便于完成初始化

@RunWith(SpringJUnit4ClassRunner.class) //这个注解说明JUnit在运行时会自动初始化IOC容器
@ContextConfiguration(locations = {"classpath:applicationContext.xml"}) //这个注解说明配置文件在什么地方
public class TestServiceTest {
    @Resource
    private TestService testService;

    @Test
    public void batchimport() {
        testService.batchimport();
    }
}

3.2 配置logback日志输出
pom文件中引入相关依赖

        
            ch.qos.logback
            logback-classic
            1.2.3
        

创建logback.xml,并完成相关配置


    
        
             %d{YYYY-MM-dd HH:mm:ss} %-5level [%thread] %logger{30} - %msg%n
            utf-8
        
    
    
        
    
    
    
        
            d:/logs/history.%d.log
        
        
            [%thread] %d %level %logger{30} - %msg%n
        
    
    
        
    


3.3 声明式事务配置
在applicationContext.xml中配置

    
    
    
    	
        
    
    
    

只要在方法上加上@Transactional注解,就表示启用了声明式事务

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/659162.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号