springmvc+ibatis框架搭建(xml配置)

来源:互联网 发布:怎么把mac视频拷到u盘 编辑:程序博客网 时间:2024/05/16 05:41

搭建环境需要的jar包:

commons-logging.jar, log4j-1.2.15.jar, servlet-api.jar, spring.jar, spring-web.jar, spring-webmvc.jar,ibatis-2.3.4.jar,ibatis-dao-2.jar,ibatis-common.jar,ibatis-sqlmap.2.jar,mysql-connector-java-5.1.7-bin.jar

web.xml详细配置:

<?xml version="1.0" encoding="UTF-8"?>
<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/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <context-param>
    <param-name>appName</param-name>
    <param-value>就业系统</param-value>
  </context-param>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:spring-mvc.xml</param-value>
  </context-param>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.xml</param-value>
  </context-param>
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:spring-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  <servlet>
    <description>初始化就业系统</description>
    <servlet-name>initServlet</servlet-name>
    <servlet-class>servlet.InitServlet</servlet-class>
    <load-on-startup>90000</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>initServlet</servlet-name>
    <url-pattern>/servlet/initServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
</web-app>

spring-mvc.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    <import resource="classpath*:conf/applicationContext-*.xml" />
 <bean id="mysqlDataSource" class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />
  <property name="username" value="uniframework" />
  <property name="password" value="uniframework" />
 </bean>
  <context:component-scan base-package="controller" use-default-filters="false" >
  <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
 </context:component-scan>
 <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation" value="classpath:sqlmap-config.xml" />
  <property name="dataSource" ref="mysqlDataSource" />
 </bean>
 
  <!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />   -->
   
  <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />      
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="suffix" value=".jsp"></property>
        <property name="prefix" value="/WEB-INF/jsp/"></property>
    
      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <!--要是有多个配置文件,只需在这里继续添加即可 -->
            </list>
        </property>
    </bean>      
</beans>

sqlmap.config配置:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
       "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="xml/User_SqlMap.xml"/> 
<sqlMap resource="xml/Notice_SqlMap.xml"/> 
<sqlMap resource="xml/Transaction_SqlMap.xml"/> 
<sqlMap resource="xml/Activity_SqlMap.xml"/>
<sqlMap resource="xml/Positions_SqlMap.xml"/> 
<sqlMap resource="xml/SysModule_SqlMap.xml"/> 
</sqlMapConfig>

控制类相应配置(举例):

package controller.index;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/index.do")
public class IndexController {
 @RequestMapping(params="method=toIndex")
    public String toIndex(HttpServletRequest request){
        return "index";
    }

}

访问页面地址 http://localhost:8080/xxxx/index.do?method=toIndex

 

0 0
原创粉丝点击