首页 > 代码库 > springmvc+mybatis整合

springmvc+mybatis整合

web.xml

<!DOCTYPE web-app PUBLIC
	 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
	 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Archetype Created Web Application</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath*:applicationContext-mybatis.xml
		</param-value>
	</context-param>
	   
	<context-param>  
		<param-name>log4jConfigLocation</param-name>  
		<param-value>classpath:log4j.properties</param-value>  
	</context-param>  
      
	<context-param>  
		<param-name>log4jRefreshInterval</param-name>  
		<param-value>60000</param-value>  
	</context-param>  
   
	<!-- 解决工程编码过滤器 -->
	<filter>  
		<filter-name>CharacterEncodingFilter</filter-name>  
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
		<init-param>  
			<param-name>encoding</param-name>  
			<param-value>utf-8</param-value>  
		</init-param>  
	</filter>  
	<filter-mapping>  
		<filter-name>CharacterEncodingFilter</filter-name>  
		<url-pattern>/*</url-pattern>  
	</filter-mapping>
	<!--以下是对静态资源的过滤-->
	<servlet-mapping> 
		<servlet-name>default</servlet-name> 
		<url-pattern>*.css</url-pattern> 
	</servlet-mapping> 
  
	<servlet-mapping> 
		<servlet-name>default</servlet-name> 
		<url-pattern>*.gif</url-pattern> 
	</servlet-mapping> 
  
	<servlet-mapping> 
		<servlet-name>default</servlet-name> 
		<url-pattern>*.jpg</url-pattern> 
	</servlet-mapping> 
  
	<servlet-mapping> 
		<servlet-name>default</servlet-name> 
		<url-pattern>*.js</url-pattern> 
	</servlet-mapping> 
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>  
		<listener-class>  
            org.springframework.web.util.Log4jConfigListener  
		</listener-class>  
	</listener>  
	<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-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>  
	</servlet>  
  
	<servlet-mapping>  
		<servlet-name>SpringMVC</servlet-name>  
		<url-pattern>/</url-pattern>  
	</servlet-mapping>
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

applicationContext-mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>
<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: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.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-3.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
	>
	<aop:aspectj-autoproxy proxy-target-class="true" />
	<context:annotation-config />
	<context:component-scan base-package="*"></context:component-scan>
	
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">	
			<value>classpath:jdbc.properties</value>
		</property>
	</bean>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
		<property name="driverClassName">
			<value>${jdbc.oracle.driverClassName}</value>
		</property> 
		<property name="url">
			<value>${jdbc.oracle.url}</value>
		</property> 
		<property name="username">
			<value>${jdbc.oracle.username}</value> 
		</property> 
		<property name="password"> 
			<value>${jdbc.oracle.password}</value>
		</property>
	</bean>
	
	<!-- MyBatis配置 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!--<property name="configLocation" value=http://www.mamicode.com/"classpath:mybatis-config.xml" />-->>
spring-servlet.xml(springmvc配置文件)

<?xml version="1.0" encoding="UTF-8"?>  
<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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
	xmlns:mvc="http://www.springframework.org/schema/mvc"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans  
	http://www.springframework.org/schema/beans/spring-beans-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  
	http://www.springframework.org/schema/mvc   
	http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"  
	>
	
	<!-- HandlerMapping -->  
	<bean  
		class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />  
  
	<!-- HandlerAdapter -->  
	<bean  
		class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />  
	<!-- ViewResolver -->  
	<bean  
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
		<property name="viewClass"  
			value=http://www.mamicode.com/"org.springframework.web.servlet.view.JstlView" />  >mybatis.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	
</configuration>

pom.xml

<project xmlns="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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.lin.project</groupId>
	<artifactId>mybatis</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>mybatis Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>3.8.1</version>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>commons-logging</groupId>
			<artifactId>commons-logging</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.7</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.0.6.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.2</version>
		</dependency>
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.32</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>mybatis</finalName>
	</build>
</project>


附件:http://download.csdn.net/detail/linzhichao12345/7795985