Eclipse搭建Maven项目整合SSM框架(maven+spring+springmvc+mybatis)

环境准备:

数据库:MYSQL8.0
JAVA:JDK1.8

一、新建MAVEN项目

File→New→Maven Project→New Maven project

Group Id: com.ssm
Artifacrt Id: SSMdemo

二、声明依赖(pom.xml)


  4.0.0
  com.ssm
  SSMdemo
  0.0.1-SNAPSHOT
  war
  SSMdemo
  
  
  
    5.1.4.RELEASE
    3.5.0
    2.0.0
    1.2.17
    1.0.9
    8.0.12
    2.9.8
    1.2
    2.5
    2.0
    5.1.8
  
  
    
    
        org.springframework
        spring-webmvc
        ${spring.version}
       
    
    
        org.springframework
        spring-context
        ${spring.version}
    
    
        org.springframework
        spring-beans
        ${spring.version}
    
    
        org.springframework
        spring-jdbc
        ${spring.version}
    
    
        org.springframework
        spring-aspects
        ${spring.version}
    
    
        org.springframework
        spring-jms
        ${spring.version}
    
    
        org.springframework
        spring-context-support
        ${spring.version}
    

    
    
        org.mybatis
        mybatis
        ${mybatis.version}
    
    
        org.mybatis
        mybatis-spring
        ${mybatis.spring.version}
    
    
        com.github.miemiedev
        mybatis-paginator
        ${mybatis.paginator.version}
    
    
    
        com.github.pagehelper
        pagehelper
        ${pagehelper.version}
    
    
    
        mysql
        mysql-connector-java
         ${mysql.version}
    
    
    
    
        com.alibaba
        druid
        ${druid.version}
    
    
    
        com.fasterxml.jackson.core
        jackson-databind
        ${jackson.version}
    

    
    
        jstl
        jstl
        ${jstl.version}
    
    
        javax.servlet
        servlet-api
        ${servlet-api.version}
        provided
    
    
        javax.servlet
        jsp-api
        ${jsp-api.version}
        provided
    

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

    ${project.artifactId}
    
        
        
            org.apache.maven.plugins
            maven-resources-plugin
            2.7
            
                UTF-8
            
        
        
        
            org.apache.maven.plugins
            maven-compiler-plugin
            3.2
            
                1.8
                1.8
                UTF-8
            
        
    
    
        
            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
            

            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                2.12.4
            
        
    
  


三、Dao层整合

1.Mybatis的配置文件(src/main/resources/mybatis/SqlMapConfig.xml)

像数据库连接池、事务之类的配置会交给Spring来管理,所以只是配置分页插件





    
    
        
        
        
            
            
            
        
    


2.配置:数据源、事务管理、SqlSessionFactory、mapper扫描器。

(src/main/resources/spring/applicationContext-dao.xml)



    
     
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

3.数据库的配置文件(src/main/resources/properties/db.properties)
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

validationQuery = "SELECT 1"
##验证连接是否可用,使用的SQL语句

四、Service层整合

1.在spring容器配置service(src/main/resources/spring/applicationContext-service.xml)


        
    
    
    

2.事务控制(src/main/resources/spring/applicationContext-trans.xml)


    
    
        
        
    
    
    
        
            
            
            
            
            
            
            
            
            
            
        
    
    
    
        
    

五、Controller层整合

1.SpringMVC配置(src/main/resources/spring/springmvc.xml)



    
    

    
    

    
    
        
        
    
    
    
    
    

六、web.xml文件配置(src/main/webapp/WEB-INF/web.xml)



    
        index.jsp
    
    
    
        contextConfigLocation
        classpath:spring/applicationContext*.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    
 
    
    
        CharacterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            utf-8
        
    
    
        CharacterEncodingFilter
        /*
    
 
    
     
        SSMdemo
        org.springframework.web.servlet.DispatcherServlet
        
        
            contextConfigLocation
            classpath:spring/springmvc.xml
        
        1
    
    
        SSMdemo
        /
     

整合完成,目录结构如下图所示。


项目源码目录截图

七、进行测试

PageController(com.ssm.controller.PageController)

package com.ssm.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class PageController {
    
    @RequestMapping("/userSearchView")
    public String userSearchView() {
        return "manage/usersearch";
    }
    
}

启动项目,访问http://localhost:8080/SSMdemo/userSearchView。

测试截图

访问成功,整合完成。

若对您有一点帮助,请点赞支持一下哦!ヾ(o◕∀◕)ノ
完整项目源码已上传,地址如下:
CSCN:https://download.csdn.net/download/qq_29518175/10939132
Github:https://github.com/molongwoyoupu/Spring-SpringMVC-MyBatis-Maven-Bootstrap3
欢迎各位到github帮忙Star一下哦。:.゚ヽ(。◕‿◕。)ノ゚.:。+゚

你可能感兴趣的:(Eclipse搭建Maven项目整合SSM框架(maven+spring+springmvc+mybatis))