【千锋逆战】SSM的整合

1.pom文件中新增依赖



    4.0.0

    com.qfedu
    Days26SSM02
    1.0-SNAPSHOT
    war

    
        
        4.3.6.RELEASE
    

    
        
        
            org.springframework
            spring-webmvc
            ${spring-version}
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.8.10
        
        
        
            mysql
            mysql-connector-java
            5.1.44
        
        
        
            org.mybatis
            mybatis
            3.4.4
        
        
        
            org.mybatis
            mybatis-spring
            1.3.0
        
        
        
            org.springframework
            spring-test
            ${spring-version}
        
        
        
            org.springframework
            spring-jdbc
            ${spring-version}
        
        
        
            org.aspectj
            com.springsource.org.aspectj.weaver
            1.6.8.RELEASE
        
        
        
            com.alibaba
            druid
            1.0.28
        
        
        
            log4j
            log4j
            1.2.17
        
        
        
            junit
            junit
            4.12
        
        
        
            org.projectlombok
            lombok
            1.18.6
            provided
        
    


    
        
        
            
                src/main/java
                
                    **/*.xml
                
                false
            
        
        
            
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.6.1
                
                    1.8
                    1.8
                
            

            
            
                org.apache.tomcat.maven
                tomcat7-maven-plugin
                2.2
                
                    /
                    8888
                
            
        
    

2.Web.xml




    
    
        dispatcherServlet
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
    
    
    
        dispatcherServlet
        /
    

    
    
        encode
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    

    
        encode
        /*
    

3.resources下的db.properties

url=jdbc:mysql://localhost:3306/hello2
driver=com.mysql.jdbc.Driver
user=root
pass=123456
maxActive=50
minIdle=1

4.resources下的spring-mvc.xml




    
    

    
    
    

    
    
    
    
    

    
    


5.resource下的spring-mybatis.xml




  
  

  
  
      
      
      
      
      
      
      
      
  

  
  
  
      
      
      
      
  

  
  
  
      
      
  

  
  
      
  

  
      
      
          
          
          
          
      
  

  
  
      
      
  

6.resources下的mybatis-config.xml




   
      
   

7.resource下的log4j.properties

# Global logging configuration
log4j.rootLogger=ERROR, stdout, F
# MyBatis logging configuration...
log4j.logger.com.qfedu.dao=TRACE
# Console output...
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

8.配置文件结束,环境搭建完毕,开始编写代码, User.java

package com.qfedu.pojo;

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

@Data
@NoArgsConstructor
@AllArgsConstructor
public class User {

    private int uid;
    private String username;
    private String password;
    private int age;
    private String addr;
}

9.UserController.java

package com.qfedu.controller;

import com.qfedu.pojo.User;
import com.qfedu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    /**
     * 动态注入service对象
     */
    @Resource
    private IUserService userService;

    @GetMapping("/Users")
    public List getUsers(){
        return  userService.getAllUsers();
    }
}

10.IUserService.java

package com.qfedu.service;

import java.util.List;

import com.qfedu.pojo.User;

public interface IUserService {

   List getAllUsers();
}

11.UserServiceImpl.java

package com.qfedu.service.impl;

import java.util.List;

import javax.annotation.Resource;

import com.qfedu.dao.IUserDao;
import com.qfedu.pojo.User;
import com.qfedu.service.IUserService;
import org.springframework.stereotype.Service;

/**
 * 注意Service的实现类之上一定要添加service注解
 */
@Service
public class UserServiceImpl implements IUserService {

    /**
     * 动态注入dao组件
     */
    @Resource
    private IUserDao userDao;

    @Override
    public List getAllUsers() {
        return userDao.getAllUsers();
    }
}

12.IUserDao.java

package com.qfedu.dao;

import com.qfedu.pojo.User;

import java.util.List;

public interface IUserDao {

   List getAllUsers();
}

13.dao/mapper下新增UserMapper.xml




   
   
   

14.启动tomcat服务器插件,访问http://localhost:8888/Users,得到结果

image

你可能感兴趣的:(【千锋逆战】SSM的整合)