Spring MVC+ibaties

在 src下分别建立applicationContext.xml,info.xml,SqlMapConfig.xml

 在WEB-INF下分别配置web.xml,建立view.xml.

applicationContext.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 id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName"
   value="com.microsoft.jdbc.sqlserver.SQLServerDriver">
  </property>
  <property name="url"
   value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=pubs;SelectMethod=Cursor">
  </property>
  <property name="username" value="sa"></property>
  <property name="password" value="sa"></property>
 </bean>
 <!-- sql映射的客户端 -->
 <bean id="sqlMapClient"
  class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
  <property name="configLocation">
    <value>WEB-INF/classes/SqlMapConfig.xml</value>
   <!--<value>WEB-INF/SqlMapConfig.xml</value>-->
  </property>
 </bean>
<!-- Spring对Jdbc事务管理器的定义 --><!--
 <bean id="transactionManager"
  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
 </bean>
 
 --><!-- 处理的实现类 -->
 <bean id="infodao" class="com.yang.dao.ImInfo">
  <property name="dataSource">
   <ref local="dataSource" />
  </property>
  <property name="sqlMapClient">
   <ref local="sqlMapClient" />
  </property>
 </bean>
<!-- 事务代理工厂TransactionProxyFactoryBean包含了事务拦截器,目标代理和事务属性设置--><!--
 <bean id="userDAOProxy"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager">
   <ref bean="transactionManager" />
  </property>
   以内部类的方式定义代理的目标对象
  <property name="target">
   <ref local="infodao" />
  </property>
   设置事务性的方法和属性
  <property name="transactionAttributes">
   <props>
    <prop key="insert*">PROPAGATION_REQUIRED</prop>  
   </props>
  </property>
 </bean>
     --><bean id="SelectAction" class="com.yang.controller.ShowController">
      <property name="info">
        <ref bean="infodao"/>
      </property>   
    </bean> 
</beans>

info.xml

  <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>
    <typeAlias alias="infodao" type="com.yang.pojo.Info"/>
    <resultMap id="InfoBean" class="com.yang.pojo.Info">  
        <result property="id" column="id"/>  
        <result property="username" column="username"/>      
    </resultMap>
 <insert id="isnertdao" parameterClass="infodao">
      insert into info(username) values(#username#)
              <selectKey keyProperty="id" resultClass="int">  
            SELECT SELECT SCOPE_IDENTITY() AS ID
        </selectKey> 
 </insert>
 
  <select id="selectAllAccounts" resultClass="infodao">
          select * from info
     </select>
  <select id="selectpageAccounts" resultMap="InfoBean">
          select * from info
     </select>
</sqlMap>

SqlMapConfig.xml

     <?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="info.xml"/>
</sqlMapConfig>
view.xml

   <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" >
<beans>
<!-- 视图控制器 -->
   <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <!-- MVC标签 -->
       <property name="viewClass">
           <value>org.springframework.web.servlet.view.JstlView</value>
       </property>
       <property name="prefix">
         <value>/</value>
       </property>
       <property name="suffix">
          <value>.jsp</value>
       </property>
   </bean>
  
   <!-- 映射控制器的处理类 -->
   <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
     <property name="mappings">
          <props>      
           <prop key="/select.do">SelectAction</prop>
        </props>
     </property>  
   </bean>
</beans>

web.xml

   <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
      <servlet>
       <servlet-name>dispatcherServelet</servlet-name>
       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/view.xml,/WEB-INF/classes/applicationContext.xml</param-value>
      </init-param>
    </servlet>
      <servlet-mapping>
        <servlet-name>dispatcherServelet</servlet-name>
         <url-pattern>*.do</url-pattern>
     </servlet-mapping>
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值