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配置:

<servlet>
      <servlet-name>dispatcher</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>classpath:springmvc.xml</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/</url-pattern>
</servlet-mapping>

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全部内容:


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

<display-name>Springtestdisplay-name>

<listener>
<listener-class>org.springframework.web.util.WebAppRootListenerlistener-class>
listener>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>WebAppRootKeyparam-name>
<param-value>mywebrootparam-value>
context-param>

<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mybatis.xmlparam-value>
context-param>

<servlet>
<servlet-name>dispatcherservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>

<servlet-mapping>
<servlet-name>dispatcherservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>



<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>

web-app>

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


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/mvc  
           http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd">

    
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter" >    
        <property name="messageConverters">    
            <list>  
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">  
                    <property name="supportedMediaTypes">  
                        <list>  
                            <value>text/plain;charset=UTF-8value>  
                        list>  
                    property>  
            bean>  
          list>  
        property>    
   bean>        

<mvc:annotation-driven/>

<mvc:resources location="/resources/css/" mapping="/css/**">mvc:resources>
<mvc:resources location="/resources/js/" mapping="/js/**">mvc:resources>
<mvc:resources location="/resources/images/" mapping="/images/**">mvc:resources>

<context:component-scan base-package="com.xuduo.controller" />



<mvc:view-controller path="/" view-name="index" />
<bean class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView">property>

<property name="prefix" value="/WEB-INF/pages/">property>

<property name="suffix" value=".jsp">property>
bean>
<import resource="service.xml"/>
beans>  

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


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.2.xsd 
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<context:property-placeholder location="classpath:db.properties" />

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
bean>

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource" />


<property name="configLocation" value="classpath:mybatis.xml" />
bean>

<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xuduo.dao">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
bean>

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
beans>

db.properties文件:

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

mybatis.xml文件:



<configuration>
<typeAliases>
typeAliases>

configuration>

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


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/mvc  
           http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd">


beans>

源文件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;
 }
}

你可能感兴趣的:(常用代码整理)