SSM整合

SSM 的整合

  1. 依赖填入

    
    
        4.0.0
    
        com.shj
        ssm_test
        1.0-SNAPSHOT
    
        war
    
        
        
            
            4.3.6.RELEASE
        
    
        
        
            
                
                    src/main/java
                    
                        **/*.xml
                    
                
            
    
            
                
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.6.1
                    
                        1.8
                        1.8
                    
                
    
                
                
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                    
                        /
                        8080
                    
                
            
        
    
    
        
            
                junit
                junit
                4.12
            
    
            
                org.mybatis
                mybatis
                3.4.4
            
    
            
                mysql
                mysql-connector-java
                5.1.44
            
    
            
                org.springframework
                spring-webmvc
                ${spring-version}
            
    
            
            
                org.aspectj
                com.springsource.org.aspectj.weaver
                1.6.8.RELEASE
            
    
            
            
                com.alibaba
                druid
                1.0.28
            
    
            
            
                org.mybatis
                mybatis-spring
                1.3.1
            
    
            
            
                log4j
                log4j
                1.2.17
            
    
            
            
                com.fasterxml.jackson.core
                jackson-databind
                2.8.1
            
    
            
            
                org.springframework
                spring-jdbc
                ${spring-version}
            
    
            
            
                org.springframework
                spring-test
                ${spring-version}
            
    
            
            
                org.projectlombok
                lombok
                1.18.6
                provided
            
        
    
    
    
  2. web.xml配置

    
    
    
        
        
            springmvc
            org.springframework.web.servlet.DispatcherServlet
            
                contextConfigLocation
                classpath:spring-mvc.xml
            
        
    
        
            springmvc
            /
        
    
        
        
            encode
            org.springframework.web.filter.CharacterEncodingFilter
        
        
            encode
            /*
        
    
    
  3. 配置spring-mvc.xml文件

    
    
    
        
        
    
        
        
        
    
        
        
        
        
    
        
        
    
    
  4. db.properties文件

    #db.properties文件
    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3307/shop_mybatis
    user=root
    pass=970809
    
  5. 配置spring-mybatis.xml

    • 为了增强可读性,将与mybatis有关的配置分离出来

    • 
      
      
      
          
          
      
          
          
              
              
              
              
          
      
          
          
              
              
              
              
          
      
          
          
              
              
          
      
          
          
      
          
          
              
                  
                  
                  
                  
              
          
      
      
          
          
              
              
          
      
      
  6. mybatis-config.xml配置

    • 
      
      
          
          
              
          
      
      
  7. log4j.properties配置

    # 全局日志配置
    # 日志有四个级别 :error,warn,info,debug
    log4j.rootLogger=ERROR, stdout, F
    # MyBatis 日志配置 ,可自行设置追踪路径
    log4j.logger.dao=TRACE
    # 控制台输出
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
    
    # 持久化
    log4j.appender.F = org.apache.log4j.DailyRollingFileAppender
    log4j.appender.F.File =myproj.log
    log4j.appender.F.Append = true
    log4j.appender.F.Threshold = DEBUG
    log4j.appender.F.layout=org.apache.log4j.PatternLayout
    log4j.appender.F.layout.ConversionPattern=%-d{yyyy-MM-dd HH\:mm\:ss}-[%p %F\:%L]  %m%n
    

    配置完成,开始代码编写

    1. 实体类

      • 使用lombok注解

      • package pojo;
        
                import lombok.AllArgsConstructor;
                import lombok.Data;
                import lombok.NoArgsConstructor;
        
        @Data
        @NoArgsConstructor
        @AllArgsConstructor
        public class User {
            private String uid;
            private String name;
            private String pass;
            private String phone;
        }
        
    2. dao接口

      public interface IUserDao {
          List getAllUsers();
      }
      
    3. 映射文件

      
      
      
      
      
      
      
          
               select * from users
          
      
          
      
      
      
      
    4. service接口

      package service;
      
      import pojo.User;
      
      import java.util.List;
      
      public interface IUserService {
      
          List getAllUsers();
      }
      
    5. service实现类

      package service.Imp;
      
      import dao.IUserDao;
      import org.springframework.stereotype.Service;
      import pojo.User;
      import service.IUserService;
      
      import javax.annotation.Resource;
      import java.util.List;
      
      @Service
      public class UserService implements IUserService {
      
          @Resource
          IUserDao userDao;
      
      
          @Override
          public List getAllUsers() {
              return userDao.getAllUsers();
          }
      }
      
    6. Controller控制类

      package controller;
      
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      import pojo.User;
      import service.IUserService;
      
      import javax.annotation.Resource;
      import java.util.List;
      
      @RestController
      public class UserController {
      
          @Resource
          private IUserService userService;
      
          @GetMapping("/getAllUsers")
          public List getAllUsers() {
              List list = userService.getAllUsers();
              return list;
          }
      }
      

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