SSM框架整合

SSM框架整合

基于Maven+SpringMVC+Spring+MyBatis的组合,快速搭建SSM框架。适合初学者学习。

第一步:准备开发环境
JDK:1.8
Maven:apache-maven-3.6.0
Tomcat:apache-tomcat-8.5.41
(注:这些软件在此不再说明怎样安装,可以去问万能百度、谷歌等等)

第二步:添加Maven相关依赖


    4.0.0
    com.xiaowen
    ssmDemo
    0.0.1-SNAPSHOT
    war
    
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
        

        
            javax.servlet.jsp
            javax.servlet.jsp-api
            2.3.1
        

        
        
            javax.servlet
            jstl
            1.2
        

        
        
            org.springframework
            spring-core
            4.1.7.RELEASE
        
        
            org.springframework
            spring-beans
            4.1.7.RELEASE
        
        
            org.springframework
            spring-tx
            4.1.7.RELEASE
        
        
            org.springframework
            spring-context
            4.1.7.RELEASE
        
        
            org.springframework
            spring-context-support
            4.1.7.RELEASE
        

        
            org.springframework
            spring-web
            4.1.7.RELEASE
        

        
            org.springframework
            spring-webmvc
            4.1.7.RELEASE
        

        
            org.springframework
            spring-aop
            4.1.7.RELEASE
        


        
            org.springframework
            spring-aspects
            4.1.7.RELEASE
        

        
            org.springframework
            spring-jdbc
            4.1.7.RELEASE
        

        
            org.mybatis
            mybatis-spring
            1.2.3
        


        
        
            log4j
            log4j
            1.2.17
        

        
            org.slf4j
            slf4j-log4j12
            1.7.12
        

        
        
            org.mybatis
            mybatis
            3.3.0
        

        
        
            mysql
            mysql-connector-java
            5.1.37
        
        
        
            com.alibaba
            druid
            1.0.16
        
        
        
            org.projectlombok
            lombok
            1.18.6
            provided
        

        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.1
        

        
            com.fasterxml.jackson.core
            jackson-core
            2.9.1
        

        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.1
        
    

第三步:配置相关的配置

jdbc.properties:jdbc配置如下

######本地地址######
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

######配置连接池#######
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000

mybatis:mybatis-config.xml配置如下




    
    
        
    


spring:applicationContext.xml配置如下

    
    
        
    
    
    
        
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
        
        
    

    
    
        
        
    
    
    
    
        
    

    
    
        
    
  
      
      
          
          
              
              
              
              
              
              
              
              
              
              
              
              
              
              
              
          
      
  
      
      
          
          
    
    
   
    
   

springmvc:spring-mvc.xml配置如下

     
    

     
     
    
    
    
    
    
    
    
        
        
    
    
    
  

log4j:log4j.properties配置如下

 log4j.rootLogger=DEBUG, Console  
  
#Console  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.layout=org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n  
  
log4j.logger.java.sql.ResultSet=INFO  
log4j.logger.org.apache=INFO  
log4j.logger.java.sql.Connection=DEBUG  
log4j.logger.java.sql.Statement=DEBUG  
log4j.logger.java.sql.PreparedStatement=DEBUG  

第四步:创建sql语句

create table `t_student` (
    `sid` int ,
    `sno` varchar ,
    `name` varchar ,
    `sex` varchar 
); 
insert into `t_student` (`sid`, `sno`, `name`, `sex`) values('1','001','张三','男');
insert into `t_student` (`sid`, `sno`, `name`, `sex`) values('2','002','李四','男');
insert into `t_student` (`sid`, `sno`, `name`, `sex`) values('3','003','王五','女');
insert into `t_student` (`sid`, `sno`, `name`, `sex`) values('4','004','周柳','女');

第五步:编写pojo类

package com.xiaowen.model;

import java.io.Serializable;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
  * 学生实体类
 * @author xiaowen
 *
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student implements Serializable {
    private static final long serialVersionUID = 1L;
     private Integer sid;
     private String sno;
     private String name;
     private String sex;
}

第六步:编写mybatis映射文件




    
    
        
        
        
        
    
    
    
 

第七步:编写Dao数据层

package com.xiaowen.dao;
import java.util.List;
import com.xiaowen.model.Student;

public interface StudentMapper {
    /**
     * 查询所有学生信息
     * @return
     */
    List findAll();
}

第八步:编写service业务层及实现层

package com.xiaowen.service;
import java.util.List;
import com.xiaowen.model.Student;
public interface StudentService {
    List findAll();
}
package com.xiaowen.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiaowen.dao.StudentMapper;
import com.xiaowen.model.Student;
import com.xiaowen.service.StudentService;
@Service
public class StudentServiceImp implements StudentService {
 
    @Autowired
    private StudentMapper studentMapper;

    public List findAll() {
        return studentMapper.findAll();
    }
}

第九步:编写Controller控制层

package com.xiaowen.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xiaowen.model.Student;
import com.xiaowen.service.StudentService;
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping(value="/findAll")
    public List findAll() {
        List list=studentService.findAll();
        return list;
    }

第十步:启动Tomcat
浏览器访问:http://localhost:8080/ssmDemo/student/findAll.do,效果如下

1.jpg

到此,SSM整合已经完成

项目结构


2.jpg

关注公众号


更多干货资料免费下载.jpg

你可能感兴趣的:(SSM框架整合)