Let's go inside

          this blog is deprecated as a result of laziness.
          posts - 59, comments - 2, trackbacks - 0, articles - 0

          struts spring ibatis學習筆記

          Posted on 2006-08-27 16:38 Earth 閱讀(3525) 評論(0)  編輯  收藏 所屬分類: Java

          這是對上一個示例的升級。

          基本上不用改任何代碼。只說不一樣的地方。

          ?

          不同:

          1.? 使用 IoC :本例為 Type3 (Constructor Injection),

          方法: 先用 Eclipse Generate Construtor Using Fields 功能 , 快速生成構造函數 , 再使用 Spring IoC ,將以前代碼中寫死的依賴關系以配置的方式注入

          ?

          2 .使用 org.springframework.orm.ibatis.SqlMapClientTemplate

          所有的 dao 類都繼承這個方法,它只是對 iBATIS-DAO 做了簡單的封裝,將 CRUD 方法中拋出的 SQLException 變成了 unchecked exception.

          ?

          3 SqlMapConfig.xml 同前一個例子,只是將 DB 的部分去掉。

          ?

          4 Spring Struts 的集成,

          struts-config.xml 中所有 action type 都要變成

          org.springframework.web.struts.DelegatingActionProxy

          然后是增加 spring 插件

          ? <plug-in className= "org.springframework.web.struts.ContextLoaderPlugIn" >

          ??????? <set-property

          ??????????????? property= "contextConfigLocation"

          ??????????????? value= "/WEB-INF/classes/spring.xml" />

          </plug-in>

          這便是 Struts Spring 的集成方法 J

          ?

          5 Spring 的配置文件 ( 名稱,位置任意 )

          ?1?<?xml?version="1.0"?encoding="UTF-8"?>
          ?2?<!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"
          ?3?????????"http://www.springframework.org/dtd/spring-beans.dtd">
          ?4?<beans>
          ?5?????<bean?id="propertyConfigurer"?class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          ?6?????????<property?name="location"?value="classpath:jdbc.properties"/>
          ?7?????</bean>
          ?8?
          ?9?????<bean?name="/employee"?class="ibatis.demo.action.EmployeeAction">
          10?????????<constructor-arg?index="0"?ref="employeeService"/>
          11?????????<constructor-arg?index="1"?ref="departmentService"/>
          12?????</bean>
          13?
          14?????<bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource">
          15?????????<property?name="driverClassName"?value="${driver}"/>
          16?????????<property?name="url"?value="${jdbcURL}"/>
          17?????????<property?name="username"?value="${username}"/>
          18?????????<property?name="password"?value="${password}"/>
          19?????</bean>
          20?
          21?????<bean?id="sqlMapClient"????class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
          22?????????<property?name="configLocation">??<value>classpath:ibatis/demo/dao/SqlMapConfig.xml</value>
          23?????????</property>
          24?????????<property?name="useTransactionAwareDataSource">
          25?????????????<value>true</value>
          26?????????</property>
          27?????????<property?name="dataSource">
          28?????????????<ref?bean="dataSource"/>
          29?????????</property>
          30?????</bean>
          31?
          32?????<bean?id="sqlMapClientTemplate"?????????class="org.springframework.orm.ibatis.SqlMapClientTemplate">
          33?????????<property?name="sqlMapClient">
          34?????????????<ref?bean="sqlMapClient"/>
          35?????????</property>
          36?????</bean>
          37?
          38?????<bean?id="employeeDao"?class="ibatis.demo.dao.EmployeeDaoIbatis">
          39?????????<property?name="sqlMapClient">
          40????????????<ref?bean="sqlMapClient"/>
          41?????????</property>
          42?????</bean>
          43?
          44?????<bean?id="employeeService"?class="ibatis.demo.service.EmployeeServiceImpl">
          45?????????<constructor-arg?index="0"?ref="employeeDao"/>
          46?????</bean>
          47?
          48??????<bean?id="departmentDao"?class="ibatis.demo.dao.DepartmentDaoIbatis">
          49?????????????????<property?name="sqlMapClient">
          50?????????????<ref?bean="sqlMapClient"/>
          51?????????</property>
          52?????</bean>
          53?
          54?????<bean?id="departmentService"?class="ibatis.demo.service.DepartmentServiceImpl">
          55?????????<constructor-arg?index="0"?ref="departmentDao"/>
          56?????</bean>
          57?
          58?</beans>
          59?

          ?

          特別注意的是: struts-config.xml-> action ->path 屬性 一定要和 spring.xml->bean ->name 屬性一模一樣

          ?

          在項目中引入 spring ,就以上 5 個步驟。一半的工作在于 spring.xml 的編寫。

          ?

          其它:

          把一個已有的工程引入到 Eclipse 下的方式,首先把已有的項目復制到你的 workspace 下,然后新建一個同名的 java 工程,

          方法是 File -> New Project -> Java Project -> 輸入工程名 ( 用相同的名字 ) 。

          ?

          The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from required .class files. 這個錯誤表示的意思還不太清楚,而且奇怪的是該錯誤提示總是在代碼的最前面 ( 第一行位置 ) ,解決辦法是加入相關類的 jar 包到你的 classpath

          因此此處加入 servelet-api.jar classpath , OK 。
          主站蜘蛛池模板: 图们市| 调兵山市| 芷江| 西充县| 正定县| 噶尔县| 定西市| 社旗县| 城步| 濮阳县| 盘锦市| 舒城县| 江都市| 雅安市| 长垣县| 磐安县| 延寿县| 赤峰市| 墨玉县| 婺源县| 太康县| 仙桃市| 阿克| 汪清县| 怀化市| 泽普县| 丰城市| 泾源县| 大丰市| 洞口县| 沾益县| 古交市| 荣成市| 仪陇县| 拉孜县| 大渡口区| 新源县| 富民县| 开江县| 夏邑县| 吉水县|