SSM整合

1.项目pom.xml文件导入依赖

 
      junit
      junit
      4.11
      test
    
    
    
      junit
      junit
      4.12
      test
    
    
    
      org.mybatis
      mybatis
      3.4.5
    
    
    
      mysql
      mysql-connector-java
      5.1.43
    
    
    
      org.springframework
      spring-webmvc
      4.3.10.RELEASE
    
    
    
      org.springframework
      spring-jdbc
      4.3.10.RELEASE
    
    
    
      org.springframework
      spring-aspects
      4.3.10.RELEASE
    
    
    
      org.springframework
      spring-context
      4.3.10.RELEASE
    
    
    
      org.mybatis
      mybatis-spring
      1.3.1
    
    
    
      com.mchange
      c3p0
      0.9.5.1
    
    
    
      javax.servlet
      javax.servlet-api
      4.0.0
      provided
    

    
      
          log4j
          log4j
          1.2.17
      
  

2.resources文件夹中新建applictioncntest-cont.xml与springmvc.xml和log4j.properties

applictioncntest-cont.xml;




    
    
        
        
        
        
        
        
    

    
    
        
        
        
        
        
    

    
    
        
    

    
    
        
    

    
    
        
            
            
            
            
        
    

    
    
        
        
    

springmvc.xml:




    
    

    
    
        
        
     

log4j.properties:

# Global logging configuration
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# %时间 [%线程] %log级别(DEBUG,INFO,WARN,ERROR,FATAL,5代表格式化长度,不足用空格填补) %完整类名 -(自定义分隔符,也可以是':',随你) %消息%换行
# log4j.appender.A1.layout.ConversionPattern=%d [%t] %5p %c - %m%n
log4j.appender.stdout.layout.ConversionPattern=%5p %d %c: %m%n

3.web.xml文件中打开spring管理和svlete核心跟过滤器等




    Archetype Created Web Application
    
    
    contextConfigLocation
    classpath:applictioncontest.xml
    

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

    
        CharacterEncodingFilter
        /*
    


    
    
    org.springframework.web.context.ContextLoaderListener
    

    
    springmvc
    org.springframework.web.servlet.DispatcherServlet
    
      contextConfigLocation
      classpath:springmvc.xml
    
  

  
    springmvc
    *.action
  


4.在java文件夹中新建com.ssm.controller、com.ssm.mappen、com.ssm.mode、com.ssm.services包

1.controller包中键入XX类:

package com.zl.controller;

import com.zl.mode.Zuoze;
import com.zl.services.ZuozeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class ZuozeController {
    @Autowired
    private ZuozeServices zuozeServices;

    @RequestMapping("test")
    public String test(){
        List li=zuozeServices.findAllZuoze();
        for (Zuoze zuoze : li) {
            System.out.println(zuoze);
        }
        return "index";
    }

}

2.mode包中键入实体类与映射文件
3.mappen包中键入接口:

package com.zl.mappen;


import com.zl.mode.Zuoze;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public interface ZuozeMappen {
    @Select("select * from zuoze") //这里直接是受用的注解方式
    public List findAllZuoze();

}

4.在servce包中新建接口和imp包(接口实现类)在实现类中调用mappne和接口方法完成事物与业逻辑
imp实现类:

package com.zl.services.imp;

import com.zl.mappen.ZuozeMappen;
import com.zl.mode.Zuoze;
import com.zl.services.ZuozeServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.util.List;
@Service("ZuozeServices")
public class ZuozeServicesImp implements ZuozeServices {
    //创建mappen的引用
    @Autowired
    private ZuozeMappen zuozeMappen;
    @Override
    public List findAllZuoze() {

        return zuozeMappen.findAllZuoze();
    }
}

你可能感兴趣的:(SSM)