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

整合SSM (Spring+SpringMVC+Mybatis) (一)

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

整合SSM (Spring+SpringMVC+Mybatis) (一)

1.Spring+SpringMVC

1.1将Maven项目转换为web工程




1.2引入依赖 pom.xml里

 
        8
        8
        5.2.9.RELEASE
    
 
    
        org.springframework
        spring-webmvc
        ${spring.version}
    

    
        org.springframework
        spring-context-support
        ${spring.version}
    

    
        org.freemarker
        freemarker
        2.3.28
    
    

    
        com.fasterxml.jackson.core
        jackson-databind
        2.12.5
    


    
        commons-fileupload
        commons-fileupload
        1.4
    

1.3配置DispatcherServlet
先创建一个applicationContext.xml文件

web.xml

    
        spring_mvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext.xml
        
        1
    
    
    
        spring_mvc
        /
    
> 1.4对Spring配置文件加上web相关的    applicationContext.xml

  


    

    

    

1.5解决乱码问题

1.请求乱码

POST请求乱码,在web.xml中添加过滤器

    
        characterFileter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        characterFileter
        /*
    

2.响应乱码 主要是对响应数据乱码的解决,视图里的数据乱码,取决于模板引擎的配置

在appliactionContext.xml里把标签展开配置

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


1.6整合freemarker模板引擎

先在WEB-INF文件夹下建一个ftl文件夹

在applicationContext.xml里加

    
        
        
    

    
        
        
            
                UTF-8
            
        
    

1.7配置MultipartResolver 在applicationContext.xml里添加

    
        

        
    

2Spring+Mybatis
2.1引入依赖

 
    
        org.mybatis
        mybatis
        3.5.6
    
    
    
        org.mybatis
        mybatis-spring
        2.0.6
    

    
        mysql
        mysql-connector-java
        8.0.22
    

    
        org.springframework
        spring-jdbc
        ${spring.version}
    
     
    
        com.alibaba
        druid
        1.2.6
    

2.2配置数据源和连接池 applicationContext.xml
在包扫描的上面一行加一个 db.properties是我的jdbc

    

        
        
        
        
        
    

    
        
        

        
    

    

        
    

Spring声明式事务配置 applicationContext.xml


       
    

    

3.整合其他模块
3.1测试模块(Test)

 
        junit
        junit
        4.13.2
        test
    

    
        org.springframework
        spring-test
        ${spring.version}
    

    
        javax.servlet
        javax.servlet-api
        4.0.1
        provided
    

测试用例

//@RunWith来自Junit,选择运行环境的
//SpringJUnit4ClassRunner.class来自Spring-Text
// 实现了Junit的接口,目的是告诉Junit测试类,用Spring的环境
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})//locations配置文件,classes配置类
@WebAppConfiguration//告诉junit当前为web环境
public class App {
//把c1118dao给注入进来
@Autowired
private C1118Dao c1118Dao;


   @Test
    public void testSelect(){
       C1118 c1118 = c1118Dao.queryById(1);
       System.out.println(c1118);
   }

最终

mybatis-config.xml





    

        
    

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

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

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