eclipse整合spring+springMVC+Mybatis

一、新建Maven项目

 点击菜单栏File项,选择New-》Project,选中Maven Project,如下图:

eclipse整合spring+springMVC+Mybatis_第1张图片

二、配置pom.xml

"1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    4.0.0

    com.newProject.framework
    CreateFramework
    1.0-SNAPSHOT
    
        
        4.2.6.RELEASE
        
        3.4.0
        
        1.7.7
        1.2.17
        
        2.7.1
    

    
    
        junit
        junit
        4.11
        
        
    
        
        
            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.3.0
        

        
            javax.mail
            mail
            1.4
        

        
        
            mysql
            mysql-connector-java
            5.1.30
        

        
            com.oracle
            ojdbc14
            10.2.0.2
        

        
        
            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}
        
        

        
        
            com.fasterxml.jackson.core
            jackson-core
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-databind
            ${jackson.version}
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            ${jackson.version}
        

    

 三、配置springMVC

在resources文件夹下建立spring-mvc.xml,如下图

eclipse整合spring+springMVC+Mybatis_第2张图片

具体内部配置如下:



    
    
        
        
    

    
    
        
            
                text/html;charset=UTF-8
            
        
    

    
    
        
        
            
                
                
                
            
        
    


    
    
        
        
        
        
    
    
    
    
   
    
    

    
    

四、整合mybatis

在resource文件夹下建立spring-mybatis.xml文件夹,配置内容如下:

  



    
    
        
    

    
    
        
    
    
    
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    

    
    
        
        
        
    

    
    
         
        
    

    
    
        
    
    
    

 五、配置属性文件

在此总共有两个属性文件,一个是log4j.properties,另一哥是数据库连接文件jdbc.properties;

log4j.properties文件内容如下:

#定义LOG输出级别
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
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
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

 jdbc.properties文件如下:

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.1.213:1521:orcl
jdbc.username=KYYS
jdbc.password=kyys
#定义初始连接数
jdbc.initialSize=0
#定义最大连接数
jdbc.maxActive=20
#定义最大空闲
jdbc.maxIdle=20
#定义最小空闲
jdbc.minIdle=1
#定义最长等待时间
jdbc.maxWait=60000

 在webapp/WEB-INF/下web.xml文件内容如下:

"1.0" encoding="UTF-8"?>
"http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    Archetype Created Web Application
    
    
        contextConfigLocation
        classpath:spring-mybatis.xml
    

    
    
        encodingFilter
        class>org.springframework.web.filter.CharacterEncodingFilterclass>
        <async-supported>trueasync-supported>
        
            encoding
            UTF-8
        
    
    
        encodingFilter
        /*
    
    
    
  

    
    
        org.springframework.web.context.ContextLoaderListener
    
    
    
        org.springframework.web.util.IntrospectorCleanupListener
    

    
    
        SpringMVC
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:spring-mvc.xml
        
        
        1
        true
    
    
        SpringMVC
        /
    

   
    
        /index.jsp
     
   

 

六、程序内容

程序目录结构如下如:

eclipse整合spring+springMVC+Mybatis_第3张图片

 

index.jsp页面内容:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


测试框架登录
    

  Controller控制器

package fv.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/home")
public class HomeController {

	@RequestMapping(value = "/gohome", method = RequestMethod.GET)
    @ResponseBody
    public String  goToHome(@RequestParam("id") String id) {
        return "this is my home";//Integer.parseInt(id)
    }
	
	@RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index()
    {
    	return "/admin";
    }
}

  

package fv.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import fv.Service.TestService;

@Controller
@RequestMapping("/test")
public class TestController {
	   
	    //@Autowired
	    private TestService testServiceImp;//    

	    @RequestMapping(value = "/get", method = RequestMethod.GET)
	    @ResponseBody
	    public String  getTestByName(@RequestParam("id") String id) {
	        return testServiceImp.getByName(0L);//Integer.parseInt(id)
	    }

	    @RequestMapping(value = "/hello", method = RequestMethod.GET)
	    @ResponseBody
	    public String  HellowWorld(@RequestParam("id") String id) {
	        return "Hello World";//Integer.parseInt(id)
	    }
	    
	    @RequestMapping(value = "/index",method = RequestMethod.GET)
	    public String index()
	    {
	    	return "/admin";
	    }
	    @RequestMapping(value = "/indexPost")
	    public String indexPost()
	    {
	    	return "/admin";
	    }
}

 dao层Mapper接口:

package fv.dao;

public interface HomeMapper {
	String getByName(Long id);
}

  

package fv.dao;

public interface TestMapper {
	 String getByName(Long id);
}

 mapping文件xml





   
 
    

  





   
 
    

 服务层

package fv.Service;

public interface HomeService {
	String getByName(Long id);
}

  

package fv.Service;

import org.springframework.beans.factory.annotation.Autowired;

import fv.dao.HomeMapper;

public class HomeServiceImp {
	@Autowired(required = false)
    private HomeMapper testMapper=null;
    public String getByName(Long id) {
        return testMapper.getByName(id);
    }
}

  

package fv.Service;

public interface TestService {
    String getByName(Long id);
}

 

package fv.Service;

import org.springframework.beans.factory.annotation.Autowired;

import fv.dao.TestMapper;

public class TestServiceImp implements TestService {
	@Autowired(required = false)
    private TestMapper testMapper=null;
    public String getByName(Long id) {
        return testMapper.getByName(id);
    }
}

  

类目录如下图:

eclipse整合spring+springMVC+Mybatis_第4张图片

 

admin.jsp页面如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




Insert title here


登录成功!测试方法<%-- ${dynamicURL} --%>
测试重定向


 目录结构如下图:

 eclipse整合spring+springMVC+Mybatis_第5张图片

 

 

  

你可能感兴趣的:(eclipse整合spring+springMVC+Mybatis)