Spring整合SpringMVC + Mybatis基础框架的配置文件

目录

前言

新建一个普通的Maven项目
基本目录结构

├── src                    # 
│   ├── main                # 
│   │   └── java            # java代码目录
│   │   └── resources       # 配置文件目录, 存放下面Spring配置文件
│   ├── test                # 单元测试目录
├── web                     # web目录
│   └── WEB-INF             # web.xml 配置文件目录

1. Mybatis层编写

1、在 resources
目录下新建数据库配置文件 database.properties

jdbc.driver=com.mysql.jdbc.Driver
# 如果是使用 MySQL8.0+ 那么还需要增加一个时区的配置; serverTimezone=Asia/Shanghai
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

2、在 resources
目录下创建Mybatis配置文件 mybatis-config.xml




    
    
    
    
        
        
    
    
    
    
        
    

    
    
        
    

2. Spring层编写

1. Spring整合Mybatis

  1. 配置 Spring整合MyBatis
    ,这里数据源使用c3p0连接池;

  2. 编写Spring整合Mybatis的相关的配置文件;在 resources
    目录下创建 spring-dao.xml

注意:这里要引入上面Mybatis层的两个配置文件,配置文件的名称不要写错




    
    

    
    
        
        
        
        

        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
    
        
        
        
        
    

2. Spring整合service

将业务层的类注入到Spring中,在 resources
目录下创建 spring-service.xml




    
    

    
    
        
    

    
    
        
        
    

    
    
    
        
        
        
            
        
    

    
    
        
        
        
    

3. SpringMVC层编写

1. 编写web.xml

修改 WEB-INF
下的 web.xml
文件

这里引入Spring整合的配置文件 applicationContext.xml




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

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

    
    
        15
    

2. 编写spring-mvc.xml

resources
目录下创建 spring-mvc.xml





    
    
    
    
    
    
    
    
        
        
    

4. Spring配置整合文件,applicationContext.xml

resources
目录下创建 applicationContext.xml

这里引入上面三个配置文件 spring-dao.xml
spring-service.xml
spring-mvc.xml
整合成一个总的配置文件




    
    
    

依赖



    
        org.projectlombok
        lombok
        1.18.10
    
    
    
        junit
        junit
        4.13
    
    
    
        mysql
        mysql-connector-java
        5.1.47
    
    
    
        com.mchange
        c3p0
        0.9.5.2
    

    
    
        javax.servlet
        servlet-api
        2.5
    
    
        javax.servlet.jsp
        jsp-api
        2.2
    
    
        javax.servlet
        jstl
        1.2
    

    
    
        org.mybatis
        mybatis
        3.5.2
    
    
        org.mybatis
        mybatis-spring
        2.0.2
    

    
    
        org.springframework
        spring-webmvc
        5.1.9.RELEASE
    
    
        org.springframework
        spring-jdbc
        5.1.9.RELEASE
    
    
        org.aspectj
        aspectjweaver
        1.9.4
    




    
        
            src/main/java
            
                **/*.properties
                **/*.xml
            
            false
        
        
            src/main/resources
            
                **/*.properties
                **/*.xml
            
            false