spring+springMVC+mybatis 框架整合

配置文件结构图如下:

applicatioContext-dao.xml :

<!-- 第一步:配置数据源 -->
	<context:property-placeholder location="classpath:resource/jdbc.properties" />
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxPoolSize" value="10"></property>
	</bean>

	<!-- 第二步:创建sqlSessionFactory。生产sqlSession -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
	<property name="dataSource" ref="dataSource"></property>
	<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
	</bean>
	<!-- 配置mybatis接口代理开发
		* 接口类名和映射文件必须同名
		* 接口类和映射文件必须在同一个目录 下
		* 映射文件namespace名字必须是接口的全类路径名
		* 接口的方法名必须和映射Statement的id一致
	 -->
	 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
	 <property name="basePackage" value="com.fly.mapper"></property>
	 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
	 </bean>

applicationContext-service.xml:

<!-- 扫描包,加载service类 -->
	<context:component-scan base-package="com.fly.service"></context:component-scan>

springmvc.xml:

<!-- 扫描包,加载controller类 -->
	<context:component-scan base-package="com.fly.controller" />
	<!-- 加载注解 -->
	<mvc:annotation-driven/>
       
	<!-- 拼接返回的视图  /?.jsp -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	
	<!-- 静态资源映射 -->
	<mvc:resources mapping="/res/**" location="/res/" /> 
	<mvc:resources mapping="/common/**" location="/common/" /> 
	<mvc:resources mapping="/image/**" location="/image/" /> 
	
	<!-- 定义文件上传解析器 -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 设定默认编码 -->
		<property name="defaultEncoding" value="UTF-8"></property>
		<!-- 设定文件上传的最大值5MB,5*1024*1024 -->
		<property name="maxUploadSize" value="5242880"></property>
	</bean>
	
	<!-- 拦截器-->
	<mvc:interceptors>
		<mvc:interceptor>
			<mvc:mapping path="/user/**.html"/>
			<mvc:exclude-mapping path="/user/login.html"/>	
			<mvc:exclude-mapping path="/user/reg.html"/>		
			<bean class="com.fly.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>  

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/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	id="fly" version="2.5">
	<display-name>fly-manager</display-name>
	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
		<welcome-file>index.htm</welcome-file>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>default.html</welcome-file>
		<welcome-file>default.htm</welcome-file>
		<welcome-file>default.jsp</welcome-file>
	</welcome-file-list>
	
	
	<!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/applicationContext*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 解决post乱码 -->
	<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>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>


	<!-- springmvc的前端控制器 -->
	<servlet>
		<servlet-name>fly-manager</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<!-- contextConfigLocation不是必须的, 如果不配置contextConfigLocation, springmvc的配置文件默认在:WEB-INF/servlet的name+"-servlet.xml" -->
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>fly-manager</servlet-name>
		<url-pattern>*.html</url-pattern>
	</servlet-mapping>
</web-app>

jdbc.properties:

jdbc.url = jdbc\:mysql\:///fly?useUnicode=true&characterEncoding=UTF-8
jdbc.driver = com.mysql.jdbc.Driver
jdbc.username= root
jdbc.password= 

猜你喜欢

转载自blog.csdn.net/qq_22200097/article/details/75174227