Spring+MyBatis框架搭建

引入包:

spring:http://repo.spring.io/libs-release-local/org/springframework/spring/4.2.1.RELEASE/spring-framework-4.2.1.RELEASE-dist.zip
Apache Commons Logging 1.2: http://apache.fayea.com//commons/logging/binaries/commons-logging-1.2-bin.zip

web.xml配置:


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



      dispatcher
      /

servlet-name非常重要,默认情况下DispatcherSerlvet会自动尝试读取WEB-INF目录下名为servlet-name-servlet.xml的文件来加载应用程序上下文,这里对应的就是WEB-INF目录下的dispatcher-servlet.xml,如果配置
了contextConfigLocation(名字貌似是固定的)参数,设置上下文配置文件路径,自搭建为classpath:springmvc.xml,如果此参数不设置,默认加载WEB-INF路径下的application-Context.xml文件。

web.xml全部内容:




Springtest


org.springframework.web.util.WebAppRootListener



org.springframework.web.context.ContextLoaderListener


WebAppRootKey
mywebroot



contextConfigLocation
classpath:spring-mybatis.xml



dispatcher
org.springframework.web.servlet.DispatcherServlet

contextConfigLocation
classpath:springmvc.xml

1



dispatcher
/





encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8


forceEncoding
true



encodingFilter
/*



spring上下文配置文件dispatcher-servlet.xml,此例子对应classpath:springmvc.xml:



    
    
        
            
              
              
                      
                          
                            text/plain;charset=UTF-8  
                          
                      
              
            
            
           




















  

spring-mybatis.xml配置文件内容,根元素也为,此文件加载了db.properties和mybatis.xml全局配置文件:


































db.properties文件:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=xuduo
password=1234

mybatis.xml文件:








service.xml文件,配置所有的服务bean内容:



           
            

源文件MainPageController.java:

package com.xuduo.controller;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import org.springframework.stereotype.Controller;  
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MainPageController {
 
 @RequestMapping("travelDesign")
 @ResponseBody
 public ModelAndView ToTravelDesgin(){
  ModelAndView travelView = new ModelAndView("TravelDesign");
  return travelView;
 
 }
 
 @RequestMapping("getJsonData")
 @ResponseBody  
 public String GetJsonData()  {
  String JsonContext = ReadFile(System.getProperty("webapp.root")+"resources\\json\\music.json"); 
  return JsonContext;
 }
 
 private String ReadFile(String Path)
 {
  BufferedReader reader = null;
  String laststr="";
  try
  {
   FileInputStream fileInputStream = new FileInputStream(Path);
   InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"UTF-8");
   reader = new BufferedReader(inputStreamReader);
   String tempString = null;
   while((tempString = reader.readLine())!=null){
    laststr += tempString;
   }
   reader.close();
  }catch(IOException e){
   e.printStackTrace();
  }
  finally{
   if(reader!=null){
    try{reader.close();}catch(IOException e){
     e.printStackTrace();
    }
   }
  }
  return laststr;
 }
}

你可能感兴趣的:(Spring+MyBatis框架搭建)