Let's go inside

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

          struts spring ibatis學(xué)習(xí)筆記

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

          這是對(duì)上一個(gè)示例的升級(jí)。

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

          ?

          不同:

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

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

          ?

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

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

          ?

          3 SqlMapConfig.xml 同前一個(gè)例子,只是將 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 屬性一模一樣

          ?

          在項(xiàng)目中引入 spring ,就以上 5 個(gè)步驟。一半的工作在于 spring.xml 的編寫。

          ?

          其它:

          把一個(gè)已有的工程引入到 Eclipse 下的方式,首先把已有的項(xiàng)目復(fù)制到你的 workspace 下,然后新建一個(gè)同名的 java 工程,

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

          ?

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

          因此此處加入 servelet-api.jar classpath OK
          主站蜘蛛池模板: 宁陕县| 甘孜| 宜丰县| 正阳县| 荣成市| 普安县| 长子县| 望奎县| 东莞市| 铜川市| 和平县| 五指山市| 宁陵县| 安义县| 晋江市| 黑山县| 彭阳县| 弥渡县| 尉氏县| 新绛县| 织金县| 定远县| 深州市| 汕头市| 固原市| 芜湖县| 甘泉县| 新密市| 中方县| 大关县| 平阳县| 永昌县| 嘉善县| 淄博市| 江津市| 株洲市| 丹寨县| 宜黄县| 克山县| 泊头市| 弥勒县|