springMVC(非注解)+iBATIS整合 (1)

<span style="font-family: Arial, Helvetica, sans-serif;">.</span><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">首先看环境的搭建</span>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<!-- 装载spring 的配置文件包括应用层,DAO层 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml,classpath:daoContext.xml
		</param-value>
	</context-param>
	<!-- 装载log4j的配置内容 -->
    <context-param>
 		  <param-name>log4jConfigLocation</param-name>
 		  <!-- 此处必须注意地址标志位置,以免错误!! 默认编译后class文件存储位置-->
  			 <param-value>classpath:log4j.properties</param-value>
 	 </context-param>
 	 <!-- 当多个项目时每个项目的webAppRootKey必须不一样 -->
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>MyTestProject.root</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>GBK</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping> 


	<!-- SpringMVC 分派器及相关映射 默认去找WEB-NIF目录下的dispatcher-servelt.xml文件-->
	<!-- 即收到请求后去找那个文件 -->
	<servlet>
		<servlet-name>dispatcher</servlet-name>
		<servlet-class>
			org.springframework.web.servlet.DispatcherServlet
		</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>dispatcher</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>

	<!--Spring ApplicationContext 载入 通过此监听器启动加载spring配置文件 -->
	<listener>
		<listener-class>
			org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<!-- Spring 刷新Introspector防止内存泄露 -->
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>

	<!-- 支持session scope的Spring bean -->
	<listener>
		<listener-class>
			org.springframework.web.context.request.RequestContextListener
		</listener-class>
	</listener>

	<listener>
		<listener-class>
			org.springframework.web.util.Log4jConfigListener
		</listener-class>
	</listener>
	
 
</web-app>

然后是

daoContext.xml.和dispathcer.xml的配置

daoContext.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
	
		
	<!-- 此bean用于从外部属性文件载入属性,并使用这些属性值替换spring配置文件中的占位符变量 -->
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	      <property name="locations">    
            <list>     
	           <value>classpath:properties/jdbc.properties</value>    
	        </list>   
	      </property>  
    </bean>						
	<!-- 定义数据源 -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName">
			<value>${driver}</value>
		</property>
		<property name="url">
			<value>${url}</value>
		</property>
		<property name="username">
			<value>${username}</value>
		</property>
		<property name="password">
			<value>${password}</value>
		</property>
	</bean>	
	
   <!--  定义sqlMapClient -->								
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
         <!-- 加载ibatis的配置文件 -->
		<property name="configLocation">
			<value>classpath:sql-map-config.xml</value>
		</property>
		<!-- 加载数据源 -->
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
   </bean>
   
   <!-- 各个实现增删改查的bean -->
   <bean id="CustomerDao" class="com.test.dao.CustomerDaoTemplate">
     <property name="sqlMapClient">
       <ref bean="sqlMapClient" />
     </property>
   </bean>
   
   <bean id="MchtOrderDao" class="com.test.dao.MchtOrderDaoTemplate">
     <property name="sqlMapClient">
       <ref bean="sqlMapClient"/>
     </property>
   </bean>
   
   <bean id="BankCardDao" class="com.test.dao.BankCardDaoTemplate" >
     <property name="sqlMapClient">
      <ref bean="sqlMapClient" />
     </property>
   </bean>
</beans>

dispathcer.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"
	default-autowire="byName">
	<!-- SpringMVC相关Bean配置 -->

	<!-- View Resolver 视图解析器-->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView" />
		<property name="prefix" value="/WEB-INF/view/" />
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 将不同的URL映射到对应的控制器 -->
	<bean id="simpleUrlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="mappings">
			<props>
			    <prop key="bank.action">bankController</prop>
			</props>
		</property>
	</bean>
	
</beans>

今天先写到这,明天继续补,,,,



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值