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

2021-10-30学习记录

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

2021-10-30学习记录

Spring+SpringMVC+Mybatis整合

目录

Spring+SpringMVC+Mybatis整合

1.添加依赖

1.1Spring相关

1.2 mybatis相关依赖

1.3其他依赖

2.写配置文件

2.1.从web.xml写起

2.2编写SpringMVC配置

2.3编写Spring配置

2.4mybatis配置


1.添加依赖

一定要注意版本统一!!!

1.1Spring相关

spring相关依赖可以分为四部分

1.Ioc相关包

2.aop相关依赖

3.JdbcTempla相关依赖

4.Spring MVC相关依赖。

由于Spring mvc依赖于Spring Ioc所以只需要添加Spring-webmvc依赖即可将mvc与Ioc依赖都添加进项目的依赖中。

        
            org.springframework
            spring-webmvc
            5.3.1
        

aop相关依赖

        
            org.springframework
            spring-aspects
            5.3.10
        

JdbcTempla相关依赖

        
            org.springframework
            spring-jdbc
            5.0.9.RELEASE
        

 Spring相关依赖到此就结束了。

1.2 mybatis相关依赖

mybatis相关依赖有两个:mybatis核心与mybatis与spring整合

        
            org.mybatis
            mybatis
            3.5.7
        
        
            org.mybatis
            mybatis-spring
            2.0.3
        

一定要注意版本号的对应!!!

1.3其他依赖

项目中用到的其他依赖比如数据源,数据库连接,servlet等

        
            mysql
            mysql-connector-java
            8.0.16
        
        
            com.alibaba
            druid
            1.1.10
        
        
            javax.servlet
            javax.servlet-api
            4.0.1
            provided
        
        
            org.junit.jupiter
            junit-jupiter-api
            ${junit.version}
            test
        

2.写配置文件

2.1.从web.xml写起

 MVC项目首要的配置,在这个xml中我们要完成的任务有:

  • Ioc容器的启动
  • DispatcherServlet的配置
  • CharacterEncodingFilter的配置
  • HiddenHttpMethodFilter的配置(restful风格)
  • 根据需求配置其他

1.配置IoC容器


        contextConfigLocation
        classpath:Spring/application.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

2,DispatcherServlet配置

    
        DispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:SpringMVC/SpringMVC.xml
        
        1
    
    
        DispatcherServlet
        /
    

3.其他过滤器

注意这两个过滤器必须在服务器启动的同时生效,应该放在xml文件的最上方来保证首先执行。

filter>
    CharacterEncodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    
        encoding
        UTF-8
    
    
        forceResponseEncoding
        true
    
    
        forceRequestEncoding
        true
    

    
        CharacterEncodingFilter
        /*
    
    
        HiddenHttpMethodFilter
        org.springframework.web.filter.HiddenHttpMethodFilter
    
    
        HiddenHttpMethodFilter
        /
    

2.2编写SpringMVC配置

SpringMVC主要处理web相关的内容,在其配置文件中我们要完成的任务有:

  • 开启注解扫描
  • 配置视图解析器
  • 启用默认Servlet处理器
  • 根据需求配置其他

1.开启注解扫描:SpringMvc只处理web层数据,只需扫描用来与页面交互的包即可


    

2.配置视图解析器(这里使用的是thymeleaf模板引擎)

 
        
        
        
            
                
                    
                        
                        
                        
                        
                    
                
            
        
    

3.启用默认servlet处理器

    

2.3编写Spring配置

在Spring配置文件中我们需要完成以下几部分的配置

  • 开启注解扫描
  • 配置数据源
  • 配置事务管理器
  • 配置aop
  • 配置mybatis相关
  • 其他需求

1.开启注解扫描:我们已经将controllor相关的类交给SpringMvc去处理了,现在只需将剩下的类来交给Spring处理就行。

    
         
     

2.配置数据源(这里使用durid)

    
    
        
        
        
        
        
        
    

3.配置事务管理器

  
        
    

4.配置aop

        
    
    
    
        
            
        
    

5.整合mybatis 


        
        
        
    
    
        
    

到此其实就整合完毕了,如果对mybatis还有其他的需求的话也可以单独创立mybatis的配置文件进行相关配置。

2.4mybatis配置

mybatis的主要配置其实已经在Spring的配置文件中完成了,如果对mybatis有其他要求的话也可以单独配置

3.测试

准备一个实体类User

public class User {
    private Integer id;
    private String username;
    private String password;
    private String email;
}

准备一个接口UserMapper,其中准备一个getUserById方法

public interface UserMapper {
    User getUserById(Integer id);

}

准备一个UserMapper,xml文件






    
SELECT  * FROM t_user WHERe id=#{id}

进行测试

public class TEST {
    @Test
    public void testUser(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring/application.xml");
        UserMapper userMapper = applicationContext.getBean("userMapper", UserMapper.class);
        User user = userMapper.getUserById(9);
        System.out.println(user);
    }
}

结果:

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

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

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