MyEclipse SSM(Spring+SpringMVC+MyBatis)框架搭建

1、maven引入需要使用的jar

pom.xml

  
          
        4.0.2.RELEASE  
          
        3.2.6  
          
        1.7.7  
        1.2.17  
    
  
 		
   		
           junit
           junit
           4.9
       
   
   		  
         
           org.springframework  
           spring-core  
           ${spring.version}  
         
 
         
           org.springframework  
           spring-web  
           ${spring.version}  
         
         
           org.springframework  
           spring-oxm  
           ${spring.version}  
         
         
           org.springframework  
           spring-tx  
           ${spring.version}  
         
 
         
           org.springframework  
           spring-jdbc  
           ${spring.version}  
         
 
         
           org.springframework  
           spring-webmvc  
           ${spring.version}  
         
         
           org.springframework  
           spring-aop  
           ${spring.version}  
         
 
         
           org.springframework  
           spring-context-support  
           ${spring.version}  
         
 
         
           org.springframework  
           spring-test  
           ${spring.version}  
         
         
         
           org.mybatis  
           mybatis  
           ${mybatis.version}  
         
         
         
           org.mybatis  
           mybatis-spring  
           1.2.2  
         
         
         
           javax  
           javaee-api  
           7.0  
         
         
         
           mysql  
           mysql-connector-java  
           5.1.30  
         
         
         
           commons-dbcp  
           commons-dbcp  
           1.2.2  
         
         
         
           jstl  
           jstl  
           1.2  
         
         
         
         
           log4j  
           log4j  
           ${log4j.version}  
         
         
         
         
         
           com.alibaba  
           fastjson  
           1.1.41  
         
 
 
         
           org.slf4j  
           slf4j-api  
           ${slf4j.version}  
         
 
         
           org.slf4j  
           slf4j-log4j12  
           ${slf4j.version}  
         
         
         
         
           org.codehaus.jackson  
           jackson-mapper-asl  
           1.9.13  
         
         
         
           commons-fileupload  
           commons-fileupload  
           1.3.1  
         
         
           commons-io  
           commons-io  
           2.4  
         
         
           commons-codec  
           commons-codec  
           1.9  
         
    
  

2、建立jdbc属性文件 jdbc.properties

#============================================================================
# MySQL
#============================================================================
jdbc.mysql.driver=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8
jdbc.mysql.username=root
jdbc.mysql.password=root
#============================================================================
# common settings
#============================================================================
jdbc.initialSize=5
jdbc.minIdle=5
jdbc.maxIdle=20
jdbc.maxActive=100
jdbc.maxWait=100000
jdbc.defaultAutoCommit=false
jdbc.removeAbandoned=true
jdbc.removeAbandonedTimeout=600
jdbc.testWhileIdle=true
jdbc.timeBetweenEvictionRunsMillis=60000
jdbc.numTestsPerEvictionRun=20
jdbc.minEvictableIdleTimeMillis=300000

3、建立spring-mybatis.xml配置文件主要是自动注入以及配置数据库等

spring-mybatis.xml

  
  
      
      
      
      
          
      
  
    
          
          
          
          
          
          
          
          
          
          
          
          
          
          
      
  
     
        
        
        
     
         
     
  
      
      
          
          
      
  
      
      
          
      
  
  


4、建立SqlMapConfig.xml  放置实体对应的xml文件



	
		
	


5、log4j的配置 log4j.properties

log4j.rootLogger=INFO,Console,File  
log4j.appender.Console=org.apache.log4j.ConsoleAppender  
log4j.appender.Console.Target=System.out  
log4j.appender.Console.layout = org.apache.log4j.PatternLayout  
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n  
  
log4j.appender.File = org.apache.log4j.RollingFileAppender  
log4j.appender.File.File = logs/ssm.log  
log4j.appender.File.MaxFileSize = 10MB  

log4j.appender.File.Threshold = ALL  
log4j.appender.File.layout = org.apache.log4j.PatternLayout  
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n  


以上我们已经完成了spring+mybatis的配置了。可以适当的自己测试一下spring+mybatis是否配置成功。暂时这里不做测试 继续配置springMVC


6、spring-mav.xml
  
  
      
      
      
      
          
              
                text/html;charset=UTF-8  
              
          
    
      
      
          
              
                   
            
        
    
    
      
          
        
        
      
      
  

7、配置web.xml文件

这里面对spring-mybatis.xml的引入以及配置的spring-mvc的Servlet就是为了完成SSM整合



  Archetype Created Web Application
  
    contextConfigLocation
    classpath:/henry/config/spring-mybatis.xml
  
    
      
        default  
        /js/*  
        /css/*  
        /jsp/*  
        /images/*  
        /fonts/*  
    
  
    encodingFilter
    org.springframework.web.filter.CharacterEncodingFilter
    true
    
      encoding
      UTF-8
    
  
  
    encodingFilter
    /*
  
  
    org.springframework.web.context.ContextLoaderListener
  
  
    org.springframework.web.util.IntrospectorCleanupListener
  
  
    SpringMVC
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:/henry/config/spring-mvc.xml
    
    1
    true
  
  
    SpringMVC
    /
  
  
    /login.jsp
  


至此已经完成了SSM框架的整合了,接下来就是测试咯,如果成功了那么恭喜你,如果失败了。就继续调试吧,座位程序猿就是要不停的和bug做斗争啊!


8、建立loginController类

loginController.java 控制器

package com.henry.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

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

import com.henry.bean.BlogUser;
import com.henry.service.blogUserService;

@Controller
@RequestMapping("login")
public class loginController {
	@Resource
	private blogUserService blogUser;
	
	@RequestMapping("/login")
    public String toIndex(HttpServletRequest request,Model model,BlogUser user){  
        user = this.blogUser.checkLogin(user);
        if(user != null){
            return "index";
        }
        //model.addAttribute("user", user);
        return "";
    }  
}

9、建立blogUserServiceImpl

blogUserServiceImpl.java

package com.henry.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.henry.bean.BlogUser;
import com.henry.dao.blogUserMapper;
import com.henry.service.blogUserService;

@Service
public class blogUserServiceImpl implements blogUserService{
	@Resource(name = "blogUserMapping")
	private blogUserMapper blogUser;
	
	/**
     * 验证用户是否存在
     * */
	public BlogUser checkLogin(BlogUser user){
		return blogUser.checkLogin(user);
	}
}


抛出俩层  dao自己去写吧, 自己测试吧!


最后抛出一张项目层级图

MyEclipse SSM(Spring+SpringMVC+MyBatis)框架搭建_第1张图片

你可能感兴趣的:(java)