??xml version="1.0" encoding="utf-8" standalone="yes"?>国产a久久精品一区二区三区,一区二区中文字,一本色道久久综合亚洲精品酒店http://www.aygfsteel.com/libin2722/category/27870.html虚其心,可解天下之问Q专其心Q可d下之学;静其心,可悟天下之理Q恒其心Q可成天下之业?/description>zh-cnMon, 19 Oct 2009 07:01:39 GMTMon, 19 Oct 2009 07:01:39 GMT60iBatis 新增时候主键的获取(MySql)http://www.aygfsteel.com/libin2722/articles/297835.htmlC物C物Mon, 12 Oct 2009 00:45:00 GMThttp://www.aygfsteel.com/libin2722/articles/297835.htmlhttp://www.aygfsteel.com/libin2722/comments/297835.htmlhttp://www.aygfsteel.com/libin2722/articles/297835.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/297835.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/297835.html        insert into usersrcaccount
        (
            USER_ID,
            SOURCETYPE,
            ACCOUNT,
            PASSWORD
        )
        values
        (
            #userId#,
            #sourceType#,
            #account#,
            #password#
        )
        <selectKey resultClass="java.lang.Long" keyProperty="id" >
            SELECT @@IDENTITY AS id
        </selectKey>
    </insert>

C物 2009-10-12 08:45 发表评论
]]>
ibatis中由SELECT语句自动生成COUNT语句http://www.aygfsteel.com/libin2722/articles/197816.htmlC物C物Fri, 02 May 2008 16:31:00 GMThttp://www.aygfsteel.com/libin2722/articles/197816.htmlhttp://www.aygfsteel.com/libin2722/comments/197816.htmlhttp://www.aygfsteel.com/libin2722/articles/197816.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/197816.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/197816.html
很多时候我们需要执行select语句对应的count语句Q例如分|询时要得到结果的记录敎ͼ但在ibatis的映文件中我们只想写一条select语句Q而count语句直接p条语句生成,q可以省d多不必要的语句关联,下面的代码可以实现这一炏V?br />
CountStatementUtil.java

java 代码
package com.aladdin.dao.ibatis.ext;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import com.aladdin.util.ReflectUtil;
import com.ibatis.common.jdbc.exception.NestedSQLException;
import com.ibatis.sqlmap.client.event.RowHandler;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
import com.ibatis.sqlmap.engine.mapping.result.AutoResultMap;
import com.ibatis.sqlmap.engine.mapping.result.BasicResultMap;
import com.ibatis.sqlmap.engine.mapping.result.ResultMap;
import com.ibatis.sqlmap.engine.mapping.sql.Sql;
import com.ibatis.sqlmap.engine.mapping.statement.ExecuteListener;
import com.ibatis.sqlmap.engine.mapping.statement.MappedStatement;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.mapping.statement.SelectStatement;
import com.ibatis.sqlmap.engine.scope.ErrorContext;
import com.ibatis.sqlmap.engine.scope.RequestScope;

public class CountStatementUtil {

public static MappedStatement createCountStatement(MappedStatement selectStatement) {
return new CountStatement((SelectStatement) selectStatement);
}

public static String getCountStatementId(String selectStatementId) {
return "__" + selectStatementId + "Count__";
}

}

class CountStatement extends SelectStatement {

public CountStatement(SelectStatement selectStatement) {
super();
setId(CountStatementUtil.getCountStatementId(selectStatement
.getId()));
setResultSetType(selectStatement.getResultSetType());
setFetchSize(1);
setParameterMap(selectStatement.getParameterMap());
setParameterClass(selectStatement.getParameterClass());
setSql(selectStatement.getSql());
setResource(selectStatement.getResource());
setSqlMapClient(selectStatement.getSqlMapClient());
setTimeout(selectStatement.getTimeout());
List executeListeners = (List) ReflectUtil.getFieldValue(
selectStatement, "executeListeners", List.class);
if (executeListeners != null) {
for (Object listener : executeListeners) {
addExecuteListener((ExecuteListener) listener);
}
}
BasicResultMap resultMap = new AutoResultMap(
((ExtendedSqlMapClient) getSqlMapClient()).getDelegate(), false);
resultMap.setId(getId() + "-AutoResultMap");
resultMap.setResultClass(Long.class);
resultMap.setResource(getResource());
setResultMap(resultMap);

}

protected void executeQueryWithCallback(RequestScope request,
Connection conn, Object parameterObject, Object resultObject,
RowHandler rowHandler, int skipResults, int maxResults)
throws SQLException {
ErrorContext errorContext = request.getErrorContext();
errorContext
.setActivity("preparing the mapped statement for execution");
errorContext.setObjectId(this.getId());
errorContext.setResource(this.getResource());

try {
parameterObject = validateParameter(parameterObject);

Sql sql = getSql();

errorContext.setMoreInfo("Check the parameter map.");
ParameterMap parameterMap = sql.getParameterMap(request,
parameterObject);

errorContext.setMoreInfo("Check the result map.");
ResultMap resultMap = getResultMap(request, parameterObject, sql);

request.setResultMap(resultMap);
request.setParameterMap(parameterMap);

errorContext.setMoreInfo("Check the parameter map.");
Object[] parameters = parameterMap.getParameterObjectValues(
request, parameterObject);

errorContext.setMoreInfo("Check the SQL statement.");
String sqlString = getSqlString(request, parameterObject, sql);

errorContext.setActivity("executing mapped statement");
errorContext
.setMoreInfo("Check the SQL statement or the result map.");
RowHandlerCallback callback = new RowHandlerCallback(resultMap,
resultObject, rowHandler);
sqlExecuteQuery(request, conn, sqlString, parameters, skipResults,
maxResults, callback);

errorContext.setMoreInfo("Check the output parameters.");
if (parameterObject != null) {
postProcessParameterObject(request, parameterObject, parameters);
}

errorContext.reset();
sql.cleanup(request);
notifyListeners();
} catch (SQLException e) {
errorContext.setCause(e);
throw new NestedSQLException(errorContext.toString(), e
.getSQLState(), e.getErrorCode(), e);
} catch (Exception e) {
errorContext.setCause(e);
throw new NestedSQLException(errorContext.toString(), e);
}
}

private String getSqlString(RequestScope request, Object parameterObject,
Sql sql) {
String sqlString = sql.getSql(request, parameterObject);
int start = sqlString.toLowerCase().indexOf("from");
if (start >= 0) {
sqlString = "SELECT COUNT(*) AS c " + sqlString.substring(start);
}
return sqlString;
}

private ResultMap getResultMap(RequestScope request,
Object parameterObject, Sql sql) {
return getResultMap();
}

}

上面代码中的getSqlStringҎ可以Ҏ自己pȝselect语句的复杂程度完善,q里l出的是最单的实现?br />
使用上面的类卛_由select语句生成count语句Q下面是通过spring使用的代码:

BaseDaoiBatis.java

java 代码
//...
public abstract class BaseDaoiBatis extends SqlMapClientDaoSupport {

//...

protected long getObjectTotal(String selectQuery, Object parameterObject) {
prepareCountQuery(selectQuery);
//...
return (Long) getSqlMapClientTemplate().queryForObject(
CountStatementUtil.getCountStatementId(selectQuery),
parameterObject);
}

protected void prepareCountQuery(String selectQuery) {

String countQuery = CountStatementUtil.getCountStatementId(selectQuery);
if (logger.isDebugEnabled()) {
logger.debug("Convert " + selectQuery + " to " + countQuery);
}
SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
if (sqlMapClient instanceof ExtendedSqlMapClient) {
SqlMapExecutorDelegate delegate = ((ExtendedSqlMapClient) sqlMapClient)
.getDelegate();
try {
delegate.getMappedStatement(countQuery);
} catch (SqlMapException e) {
delegate.addMappedStatement(CountStatementUtil
.createCountStatement(delegate
.getMappedStatement(selectQuery)));
}

}
}

//...
}

C物 2008-05-03 00:31 发表评论
]]>
对ibatis分页功能的改q? http://www.aygfsteel.com/libin2722/articles/192504.htmlC物C物Sun, 13 Apr 2008 00:51:00 GMThttp://www.aygfsteel.com/libin2722/articles/192504.htmlhttp://www.aygfsteel.com/libin2722/comments/192504.htmlhttp://www.aygfsteel.com/libin2722/articles/192504.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/192504.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/192504.html阅读全文

C物 2008-04-13 08:51 发表评论
]]>
Zstruts+spring+ibatis?J2EE 开?2)http://www.aygfsteel.com/libin2722/articles/165550.htmlC物C物Wed, 05 Dec 2007 08:04:00 GMThttp://www.aygfsteel.com/libin2722/articles/165550.htmlhttp://www.aygfsteel.com/libin2722/comments/165550.htmlhttp://www.aygfsteel.com/libin2722/articles/165550.html#Feedback1http://www.aygfsteel.com/libin2722/comments/commentRss/165550.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/165550.html

 

此文章收藏到豌豆网

2.4. 代码剖析

下面p我们开始进一步分析JpetStore4.0的源代码Qؓ下面的改造铺路。BeanAction.java是唯一一个Struts actionc,位于com.ibatis.struts包下。正如上文所aQ它是一个通用的控制类Q利用反机Ӟ把控制{Udform bean的某个方法来处理。详l处理过E参考其源代码,单明晰?br />
Form beancM于com.ibatis.jpetstore.presentation包下Q命名规则ؓ***Bean。Form beancd部承于BaseBeanc,而BaseBeancd际承于ActionFormQ因此,Form beancd是Struts?ActionFormQForm beancȝ属性数据就由struts框架自动填充。而实际上QJpetStore4.0扩展了struts中ActionForm的应用: Form beanc还h行ؓQ更像一个BO,其行为(ҎQ由BeanActionҎ配置Qstruts-config.xmlQ的URL来调用。虽然如此,我们q是把Form beancd位于表现层。Struts-config.xml的配|里?U映方式,来告诉BeanAction把控制{到哪个form bean对象的哪个方法来处理。以q个hq接Zhttp://localhost/jpetstore4/shop/viewOrder.do

1. URL Pattern

<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
                        name="orderBean" scope="session"
                        validate="false">
                        <forward name="success" path="/order/ViewOrder.jsp"/>
                        </action>


此种方式表示Q控制将被{发到"orderBean"q个form bean对象 ?viewOrder"ҎQ行为)来处理。方法名?path"参数的以"/"分隔的最后一部分?

2. Method Parameter

<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
                        name="orderBean" parameter="viewOrder" scope="session"
                        validate="false">
                        <forward name="success" path="/order/ViewOrder.jsp"/>
                        </action>


此种方式表示Q控制将被{发到"orderBean"q个form bean对象?viewOrder"ҎQ行为)来处理。配|中?parameter"参数表示form beancM的方法?parameter"参数优先?path"参数?

3. No Method call

<action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction"
                        name="orderBean" parameter="*" scope="session"
                        validate="false">
                        <forward name="success" path="/order/ViewOrder.jsp"/>
                        </action>


此种方式表示Qform bean上没有Q何方法被调用。如果存?name"属性,则struts把表单参数等数据填充到form bean对象后,把控制{发到"success"。否则,如果name为空Q则直接转发控制?success"。这q当于struts内置的org.apache.struts.actions.ForwardAction的功?

<action path="/shop/viewOrder" type="org.apache.struts.actions.ForwardAction"
                        parameter="/order/ViewOrder.jsp " scope="session" validate="false">
                        </action>


ServicecM于com.ibatis.jpetstore.service包下Q属于业务层。这些类装了业务以及相应的事务控制。Servicecȝform beancL调用?

com.ibatis.jpetstore.persistence.iface包下的类是DAO接口Q属于业务层Q其屏蔽了底层的数据库操作,供具体的ServicecL调用。DaoConfigcL工具c(DAO工厂c)QServicec通过DaoConfigcL获得相应的DAO接口Q而不用关心底层的具体数据库操作,实现了如?中{耦合2}的解耦?

com.ibatis.jpetstore.persistence.sqlmapdao包下的类是对应DAO接口的具体实玎ͼ在JpetStore4.0中采用了ibatis来实现ORM。这些实现类l承BaseSqlMapDaoc,而BaseSqlMapDaocdl承ibatis DAO 框架中的SqlMapDaoTemplatecRibatis的配|文件存攑֜com.ibatis.jpetstore.persistence.sqlmapdao.sql目录下。这些类和配|文件位于数据层

DomaincM于com.ibatis.jpetstore.domain包下Q是普通的javabean。在q里用作数据传输对象QDTOQ,贯穿视图层、业务层和数据层Q用于在不同层之间传输数据。剩下的部分比较简单了Q请看具体的源代码,非常清晰?


C物 2007-12-05 16:04 发表评论
]]>
Zstruts+spring+ibatis?J2EE 开?3)http://www.aygfsteel.com/libin2722/articles/165549.htmlC物C物Wed, 05 Dec 2007 08:03:00 GMThttp://www.aygfsteel.com/libin2722/articles/165549.htmlhttp://www.aygfsteel.com/libin2722/comments/165549.htmlhttp://www.aygfsteel.com/libin2722/articles/165549.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/165549.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/165549.html
JpetStore4.0的关键就在struts Actioncdform beancMQ这也是其精华之一Q虽然该实现方式是试验性,待扩充和验证Q,在此ơ改造中我们要保留下来,x制层一点不变,表现层获取相应业务类的方式变了(要加载spring环境Q,其它保持不变。要特别x的改动是业务层和持久层,q运的是JpetStore4.0设计非常好,需要改动的地方非常,而且由模式可循,如下Q?br />
1. 业务层和数据层用Spring BeanFactory机制理?br /> 2. 业务层的事务由spring 的aop通过声明来完成?br /> 3. 表现层(form beanQ获取业务类的方法改p定义工厂cL实现Q加载spring环境Q?br />
3. JPetStore的改?br />
3.1. 攚w后的架?br />


其中U色部分是要增加的部分,蓝色部分是要修改的部分。下面就让我们逐一剖析?br />
3.2. Spring Context的加?br /> Z在Struts中加载Spring ContextQ一般会在struts-config.xml的最后添加如下部分:

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
            <set-property property="contextConfigLocation"
            value="/WEB-INF/applicationContext.xml" />
            </plug-in>


Spring在设计时充分考虑C与Struts的协同工作,通过内置的Struts Plug-in在两者之间提供了良好的结合点。但是,因ؓ在这里我们一点也不改动JPetStore的控制层(q是JpetStore4.0的精华之一)Q所以本文不准备采用此方式来加蝲ApplicationContext。我们利用的是spring framework 的BeanFactory机制,采用自定义的工具c(bean工厂c)来加载spring的配|文Ӟ从中可以看出Spring有多灉|Q它提供了各U不同的方式来用其不同的部?层次Q您只需要用你想用的Q不需要的部分可以不用?

具体的来_是在com.ibatis.spring包下创徏CustomBeanFactoryc,spring的配|文件applicationContext.xml也放在这个目录下。以下就是该cȝ全部代码Q很单:

public final class CustomBeanFactory {
            static XmlBeanFactory factory = null;
            static {
            Resource is = new
            InputStreamResource( CustomBeanFactory.class.getResourceAsStream("applicationContext.xml"));
            factory = new XmlBeanFactory(is);
            }
            public static Object getBean(String beanName){
            return factory.getBean(beanName);
            }
            }


实际上就是封装了Spring 的XMLBeanFactory而已Qƈ且Spring的配|文件只需要加载一ơ,以后可以直接用CustomBeanFactory.getBean("someBean")来获得需要的对象?例如someBean)Q而不需要知道具体的cRCustomBeanFactorycȝ于{耦合1}的解耦。CustomBeanFactorycd本文中只用于表现层的form bean对象获得servicecȝ对象Q因为我们没有把form bean对象配置在applicationContext.xml中。但是,Z么不把表现层的form beancM配置h呢,q样q不着qCustomBeanFactory个类了,Spring会帮助我们创建需要的一切?问题的答案就在于form beancLstruts的ActionFormc!如果大家熟悉strutsQ就会知道ActionFormcLstruts自动创徏的:在一ơ请求中Qstruts判断Q如果ActionForm实例不存在,创Z个ActionForm对象Q把客户提交的表单数据保存到ActionForm对象中。因此formbeancȝ对象׃能由spring来创建,但是servicecM及数据层的DAOcd以,所以只有他们在spring中配|。所以,很自然的Q我们就创徏了CustomBeanFactoryc,在表现层来衔接struts和spring。就q么单,实现了另一U方式的{耦合一}的解耦?

3.3. 表现?

面分析到Qstruts和spring是在表现层衔接v来的Q那么表现层p做稍微的更改Q即所需要的servicecȝ对象创徏上。以表现层的AccountBeancMؓ例:原来的源代码如下

private static final AccountService accountService
            = AccountService.getInstance();
            private static final CatalogService catalogService
            = CatalogService.getInstance();


攚w后的源代码如下

private static final AccountService accountService
            = (AccountService)CustomBeanFactory.getBean("AccountService");
            private static final CatalogService catalogService
            = (CatalogService)CustomBeanFactory.getBean("CatalogService");


其他的几个presentationcM同样方式攚w。这P表现层就完成了。关于表现层的其它部分如JSP{一概不动。也许您会说Q没有看Z么特别之处的好处啊?你还是额外实C一个工厂类。别着急,帷幕刚刚开启,spring是在表现层引入,但您发没发现Q?

presentationcM仅面向servicecȝ接口~程Q具?AccountService"是哪个实现类QpresentationcM知道Q是在spring的配|文仉配置。(本例中,Z最大限度的保持原来的代码不作变化,没有抽象出接口)。Spring鼓励面向接口~程Q因为是如此的方便和自然Q当然您也可以不q么做?

CustomBeanFactoryq个工厂cMؓ什么会如此单,因ؓ其直接用了Spring的BeanFactory。Spring从其核心而言Q是一个DI容器Q其设计哲学是提供一U无侵入式的高扩展性的框架。ؓ了实现这个目标,Spring 大量引入了Java 的Reflection机制Q通过动态调用的方式避免编码方式的U束Qƈ在此基础上徏立了其核心组件BeanFactoryQ以此作为其依赖注入机制的实现基。org.springframework.beans包中包括了这些核心组件的实现c,核心中的核心为BeanWrapper和BeanFactorycR?


C物 2007-12-05 16:03 发表评论
]]>
Zstruts+spring+ibatis?J2EE 开?4)http://www.aygfsteel.com/libin2722/articles/165548.htmlC物C物Wed, 05 Dec 2007 08:02:00 GMThttp://www.aygfsteel.com/libin2722/articles/165548.htmlhttp://www.aygfsteel.com/libin2722/comments/165548.htmlhttp://www.aygfsteel.com/libin2722/articles/165548.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/165548.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/165548.html


在上文中Q我们把iface包下的DAO接口归ؓ业务层,在这里不需要做修改。ibatis的sql配置文g也不需要改。要改的是DAO实现c,q在spring的配|文件中配置h?br />
1、修改基c?br />
所有的DAO实现c都l承于BaseSqlMapDaocR修改BaseSqlMapDaocd下:

public class BaseSqlMapDao extends SqlMapClientDaoSupport
            {
            protected static final int PAGE_SIZE = 4;
            protected SqlMapClientTemplate smcTemplate
            = this.getSqlMapClientTemplate();
            public BaseSqlMapDao()
            {
            }
            }


使BaseSqlMapDaocL为承于Spring提供的SqlMapClientDaoSupportc,q定义了一个保护属性smcTemplateQ其cd为SqlMapClientTemplate?

2、修改DAO实现c?

所有的DAO实现c还是承于BaseSqlMapDaoc,实现相应的DAO接口Q但其相应的DAO操作委托SqlMapClientTemplate来执行,以AccountSqlMapDaocMؓ例,部分代码如下Q?

public List getUsernameList()
            {
            return smcTemplate.queryForList("getUsernameList", null);
            }
            public Account getAccount(String username, String password)
            {
            Account account = new Account();
            account.setUsername(username);
            account.setPassword(password);
            return (Account)
            smcTemplate.queryForObject
            ("getAccountByUsernameAndPassword", account);
            }
            public void insertAccount(Account account)
            {
            smcTemplate.update("insertAccount", account);
            smcTemplate.update("insertProfile", account);
            smcTemplate.update("insertSignon", account);
            }


p么简单,所有函数的{֐都是一LQ只需要查找替换就可以了!


C物 2007-12-05 16:02 发表评论
]]>
Zstruts+spring+ibatis?J2EE 开?1)http://www.aygfsteel.com/libin2722/articles/165547.htmlC物C物Wed, 05 Dec 2007 08:02:00 GMThttp://www.aygfsteel.com/libin2722/articles/165547.htmlhttp://www.aygfsteel.com/libin2722/comments/165547.htmlhttp://www.aygfsteel.com/libin2722/articles/165547.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/165547.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/165547.html1. 前言

Struts 是目前Java Web MVC框架中不争的王者。经q长达五q的发展QStruts已经逐渐成长Z个稳定、成熟的框架Qƈ且占有了MVC框架中最大的市场份额。但是Struts某些技术特性上已经落后于新兴的MVC框架。面对Spring MVC、Webwork2 q些设计更精密,扩展性更强的框架QStruts受到了前所未有的挑战。但站在产品开发的角度而言QStruts仍然是最E_的选择。本文的原型例子JpetStore 4.0是ZStruts开发的Q但是不拘惔于Struts的传l固定用法,例如只用了一个自定义Actionc,q且在form beancȝ定义上也是开创性的Qo目一斎ͼE后具体剖析一下?br />
Spring Framework 实际上是Expert One-on-One J2EE Design and Development 一书中所阐述的设计思想的具体实现。Spring Framework的功能非常多。包含AOP、ORM、DAO、Context、Web、MVC{几个部分组成。Web、MVC暂不用考虑QJpetStore 4.0用的是更成熟的Struts和JSPQDAO׃目前Hibernate、JDO、ibatis的流行,也不考虑QJpetStore 4.0用的是ibatis。因此最需要用的是AOP、ORM、Context。Context中,最重要的是BeanfactoryQ它能将接口与实现分开Q非常强大。目前AOP应用最成熟的还是在事务理上?br />
ibatis 是一个功能强大实用的SQL Map工具Q不同于其他ORM工具Q如hibernateQ,它是SQL语句映射成Java对象Q而对于ORM工具Q它的SQL语句是根据映定义生成的。ibatis 以SQL开发的工作量和数据库移植性上的让步,为系l设计提供了更大的自q间。有ibatis代码生成的工P可以ҎDDL自动生成ibatis代码Q能减少很多工作量?br />
2. JpetStoreq?br />
2.1. 背景

最初是Sun公司的J2EE petstoreQ其最主要目的是用于学习J2EEQ但是其~点也很明显Q就是过度设计了。接着Oracle用J2EE petstore来比较各应用服务器的性能。微软推ZZ.Netq_?Pet shopQ用于竞争J2EE petstore。而JpetStore则是l过改良的基于struts的轻便框架J2EE web应用E序Q相比来_JpetStore设计和架构更优良Q各层定义清晎ͼ使用了很多最佛_践和模式Q避免了很多"反模?Q如使用存储q程Q在java代码中嵌入SQL语句Q把HTML存储在数据库中等{。最新版本是JpetStore 4.0?br />
2.2. JpetStore开发运行环境的建立

1、开发环?br />
Java SDK 1.4.2
Apache Tomcat 4.1.31
Eclipse-SDK-3.0.1-win32
HSQLDB 1.7.2

2、Eclipse插g

EMF SDK 2.0.1QEclipse建模框架Qlomboz插g需要,可以使用runtime版本?br /> lomboz 3.0QJ2EE插gQ用来在Eclipse中开发J2EE应用E序
Spring IDE 1.0.3QSpring Bean配置理插g
xmlbuddy_2.0.10Q编辑XMLQ用免费版功能即?br /> tomcatPluginV3Qtomcat理插g
Properties EditorQ编辑java的属性文?q可以预览以及自动存盘ؓUnicode格式。免M手工或者ANT调用native2ascii的麻烦?br /> 2.3. 架构



? JpetStore架构?/center>

? 是JPetStore架构图。参照这个架构图Q让我们E微剖析一下源代码Q得出JpetStore 4.0的具体实现图Q图2Q,思\一下子p然开朗了。前a中提到的非传l的struts开发模式,关键在struts Actioncdform beancM?br />
struts Actioncd有一个:BeanAction。没错,实是一个!与传l的struts~程方式很不同。再仔细研究BeanActionc,发现它其实是一个通用c,利用反射原理Q根据URL来决定调用formbean的哪个方法。BeanAction大大化了struts的编E模式,降低了对struts的依赖(与struts以及WEB容器有关的几个类都放在com.ibatis.struts包下Q其它的c都可以直接复用Q。利用这U模式,我们会很Ҏ的把它移植到新的框架如JSFQspring?br />
q样重心p{Udform bean上了Q它已经不是普通意义上的form bean了。查看源代码Q可以看到它不仅仅有数据和校?重置ҎQ而且已经h了行为,从这个意义上来说Q它更像一个BO(Business Object)。这是前文讲到的,BeanActioncd用反原理,ҎURL来决定调用form bean的哪个方法(行ؓQ。form bean的这些方法的{֐很简单,例如Q?br />
public String myActionMethod()
            {
            //..work
            return "success";
            }


Ҏ的返回值直接就是字W串Q对应的是forward的名Uͼ而不再是ActionForward对象Q创建ActionForward对象的Q务已l由BeanActioncM劳了。另外,E序q提供了ActionContext工具c,该工L装了request 、response、form parameters、request attributes、session attributes?application attributes中的数据存取操作Q简单而线E安全,form beancM用该工具cd以进一步从表现层框架解耦。在q里需要特别指出的是,BeanActioncL对struts扩展的一个有益尝试,虽然提供了非常好的应用开发模式,但是它还非常斎ͼ一直在发展中?



? JpetStore 4.0具体实现


C物 2007-12-05 16:02 发表评论
]]>Zstruts+spring+ibatis?J2EE 开?5)http://www.aygfsteel.com/libin2722/articles/165533.htmlC物C物Wed, 05 Dec 2007 07:41:00 GMThttp://www.aygfsteel.com/libin2722/articles/165533.htmlhttp://www.aygfsteel.com/libin2722/comments/165533.htmlhttp://www.aygfsteel.com/libin2722/articles/165533.html#Feedback0http://www.aygfsteel.com/libin2722/comments/commentRss/165533.htmlhttp://www.aygfsteel.com/libin2722/services/trackbacks/165533.html

 

此文章收藏到豌豆网

3、除d厂类以及相应的配|文?br />
除去DaoConfig.javaq个DAO工厂cd相应的配|文件dao.xmlQ因为DAO的获取现在要用spring来管理?br />
4、DAO在Spring中的配置QapplicationContext.xmlQ?br />
<bean id="dataSource"
                        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                        <property name="driverClassName">
                        <value>org.hsqldb.jdbcDriver</value>
                        </property>
                        <property name="url">
                        <value>jdbc:hsqldb:hsql://localhost/xdb</value>
                        </property>
                        <property name="username">
                        <value>sa</value>
                        </property>
                        <property name="password">
                        <value></value>
                        </property>
                        </bean>
                        <!-- ibatis sqlMapClient config -->
                        <bean id="sqlMapClient"
                        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
                        <property name="configLocation">
                        <value>
                        classpath:com\ibatis\jpetstore\persistence\sqlmapdao\sql\sql-map-config.xml
                        </value>
                        </property>
                        <property name="dataSource">
                        <ref bean="dataSource"/>
                        </property>
                        </bean>
                        <!-- Transactions -->
                        <bean id="TransactionManager"
                        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
                        <property name="dataSource">
                        <ref bean="dataSource"/>
                        </property>
                        </bean>
                        <!-- persistence layer -->
                        <bean id="AccountDao"
                        class="com.ibatis.jpetstore.persistence.sqlmapdao.AccountSqlMapDao">
                        <property name="sqlMapClient">
                        <ref local="sqlMapClient"/>
                        </property>
                        </bean>


1. 我们首先创徏一个数据源dataSourceQ在q里配置的是hsqldb数据库。如果是ORACLE数据库,driverClassName的值是"oracle.jdbc.driver.OracleDriver"QURL的值类g"jdbc:oracle:thin:@wugfMobile:1521:cdcf"。数据源现在由spring来管理,那么现在我们可以去掉properties目录下database.propertiesq个配置文g了;q有不要忘记修改sql-map-config.xmlQ去? 对它的引用?

2. sqlMapClient节点。这个是针对ibatis SqlMap的SqlMapClientFactoryBean配置。实际上配置了一个sqlMapClient的创建工厂类。configLocation属性配|了ibatis映射文g的名U。dataSource属性指向了使用的数据源Q这h有用sqlMapClient的DAO都默认用了该数据源Q除非在DAO的配|中另外昑ּ指定?

3. TransactionManager节点。定义了事务Q用的是DataSourceTransactionManager?

4. 下面可以定义DAO节点了,如AccountDaoQ它的实现类是com.ibatis.jpetstore.persistence.sqlmapdao.AccountSqlMapDaoQ用的SQL配置从sqlMapClient中读取,数据库连接没有特别列出,那么是默认使用sqlMapClient配置的数据源datasource?

q样Q我们就把持久层攚w完了,其他的DAO配置cM于AccountDao。怎么P单吧。这ơ有接口了:Q?AccountDao接口Q?gt;AccountSqlMapDao实现?

3.5. 业务?

业务层的位置以及相关c,如下图所C:在这个例子中只有3个业务类Q我们以OrderServicecMؓ例来攚w,q个cL最复杂的,其中涉及了事务?

1、在ApplicationContext配置文g中增加bean的配|:

<bean id="OrderService"
                        class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
                        <property name="transactionManager">
                        <ref local="TransactionManager"></ref>
                        </property>
                        <property name="target">
                        <bean class="com.ibatis.jpetstore.service.OrderService">
                        <property name="itemDao">
                        <ref bean="ItemDao"/>
                        </property>
                        <property name="orderDao">
                        <ref bean="OrderDao"/>
                        </property>
                        <property name="sequenceDao">
                        <ref bean="SequenceDao"/>
                        </property>
                        </bean>
                        </property>
                        <property name="transactionAttributes">
                        <props>
                        <prop key="insert*">PROPAGATION_REQUIRED</prop>
                        </props>
                        </property>
                        </bean>


定义了一个OrderServiceQ还是很Ҏ懂的。ؓ了简单v见,使用了嵌套beanQ其实现cLcom.ibatis.jpetstore.service.OrderServiceQ分别引用了ItemDaoQOrderDaoQSequenceDao。该bean的insert*实现了事务管?AOP方式)。TransactionProxyFactoryBean自动创徏一个事务advisorQ?该advisor包括一个基于事务属性的pointcut,因此只有事务性的Ҏ被拦截?

2、业务类的修改,以OrderServiceZQ?

public class OrderService {
                        /* Private Fields */
                        private ItemDao itemDao;
                        private OrderDao orderDao;
                        private SequenceDao sequenceDao;
                        /* Constructors */
                        public OrderService() {
                        }
                        /**
                        * @param itemDao 要设|的 itemDao?
                        */
                        public final void setItemDao(ItemDao itemDao) {
                        this.itemDao = itemDao;
                        }
                        /**
                        * @param orderDao 要设|的 orderDao?
                        */
                        public final void setOrderDao(OrderDao orderDao) {
                        this.orderDao = orderDao;
                        }
                        /**
                        * @param sequenceDao 要设|的 sequenceDao?
                        */
                        public final void setSequenceDao(SequenceDao sequenceDao) {
                        this.sequenceDao = sequenceDao;
                        }
                        //剩下的部?
                        …….
                        }


U色部分Z攚w分。Spring采用的是Type2的设|依赖注入,所以我们只需要定义属性和相应的设值函数就可以了,ItemDaoQOrderDaoQSequenceDao的值由spring在运行期间注入。构造函数就可以为空了,另外也不需要自q写代码处理事务了Q事务在配置中声明)QdaoManager.startTransaction();{与事务相关的语句也可以L了。和原来的代码比较一下,是不是处理精了很多!可以更关注业务的实现?

4. l束?

ibatis是一个功能强大实用的SQL Map工具Q可以直接控制SQL,为系l设计提供了更大的自q间。其提供的最新示例程序JpetStore 4.0,设计优雅Q应用了q今为止很多最佛_践和设计模式Q非帔R于学习以及在此基础上创量的J2EE WEB应用E序。JpetStore 4.0是基于struts的,本文在此基础上,最大程度保持了原有设计的精华以及最的代码改动量,在业务层和持久化层引入了Spring。在您阅M本文以及攚w后的源代码后,会深切的感受到Spring带来的种U好处:自然的面向接口的~程Q业务对象的依赖注入Q一致的数据存取框架和声明式的事务处理,l一的配|文?#8230;更重要的是Spring既是全面的又是模块化的,Spring有分层的体系l构Q这意味着您能选择仅仅使用它Q何一个独立的部分Q就像本文,而它的架构又是内部一致?


C物 2007-12-05 15:41 发表评论
]]>
Zstruts+spring+ibatis的轻量J2EE开?/title><link>http://www.aygfsteel.com/libin2722/articles/165532.html</link><dc:creator>C物</dc:creator><author>C物</author><pubDate>Wed, 05 Dec 2007 07:40:00 GMT</pubDate><guid>http://www.aygfsteel.com/libin2722/articles/165532.html</guid><wfw:comment>http://www.aygfsteel.com/libin2722/comments/165532.html</wfw:comment><comments>http://www.aygfsteel.com/libin2722/articles/165532.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/libin2722/comments/commentRss/165532.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/libin2722/services/trackbacks/165532.html</trackback:ping><description><![CDATA[<blockquote>大多数IT l织都必解决三个主要问题:1Q帮助组l减成?2Q增加ƈ且保持客?3Q加快业务效率。完成这些问题一般都需要实现对多个业务pȝ的数据和业务逻辑的无~访问,也就是说Q要实施pȝ集成工程Q以便联l业务流E、实现数据的讉K与共享?/blockquote><!--start RESERVED FOR FUTURE USE INCLUDE FILES--><!-- include java script once we verify teams wants to use this and it will work on dbcs and cyrillic characters --><!--end RESERVED FOR FUTURE USE INCLUDE FILES--> <p>JpetStore 4.0是ibatis的最新示例程序,ZStruts MVC框架Q注Q非传统Struts开发模式)Q以ibatis作ؓ持久化层。该CZE序设计优雅Q层ơ清晎ͼ可以学习以及作ؓ一个高效率的编E模型参考。本文是在其基础上,采用Spring对其中间层(业务层)q行攚w。开发量q一步减,同时又拥有了Spring的一些好?#8230;</p> <p><a name="N10042"><span id="wmqeeuq" class="atitle">1. 前言</span></a></p> <p>JpetStore 4.0是ibatis的最新示例程序。ibatis是开源的持久层品,包含SQL Maps 2.0 ?Data Access Objects 2.0 框架。JpetStoreCZE序很好的展CZ如何利用ibatis来开发一个典型的J2EE web应用E序。JpetStore有如下特点:</p> <ul> <li>ibatis数据? <li>POJO业务? <li>POJO领域c? <li>Struts MVC <li>JSP 表示?</li> </ul> <p>以下是本文用到的关键技术介l,本文假设您已l对StrutsQSpringFramewokQibatis有一定的了解Q如果不是,请首先查阅附录中的参考资料?/p> <ul> <li>Struts 是目前Java Web MVC框架中不争的王者。经q长达五q的发展QStruts已经逐渐成长Z个稳定、成熟的框架Qƈ且占有了MVC框架中最大的市场份额。但是Struts某些技术特性上已经落后于新兴的MVC框架。面对Spring MVC、Webwork2 q些设计更精密,扩展性更强的框架QStruts受到了前所未有的挑战。但站在产品开发的角度而言QStruts仍然是最E_的选择。本文的原型例子JpetStore 4.0是ZStruts开发的Q但是不拘惔于Struts的传l固定用法,例如只用了一个自定义Actionc,q且在form beancȝ定义上也是开创性的Qo目一斎ͼE后具体剖析一下? <li>Spring Framework 实际上是Expert One-on-One J2EE Design and Development 一书中所阐述的设计思想的具体实现。Spring Framework的功能非常多。包含AOP、ORM、DAO、Context、Web、MVC{几个部分组成。Web、MVC暂不用考虑QJpetStore 4.0用的是更成熟的Struts和JSPQDAO׃目前Hibernate、JDO、ibatis的流行,也不考虑QJpetStore 4.0用的是ibatis。因此最需要用的是AOP、ORM、Context。Context中,最重要的是BeanfactoryQ它能将接口与实现分开Q非常强大。目前AOP应用最成熟的还是在事务理上? <li>ibatis 是一个功能强大实用的SQL Map工具Q不同于其他ORM工具Q如hibernateQ,它是SQL语句映射成Java对象Q而对于ORM工具Q它的SQL语句是根据映定义生成的。ibatis 以SQL开发的工作量和数据库移植性上的让步,为系l设计提供了更大的自q间。有ibatis代码生成的工P可以ҎDDL自动生成ibatis代码Q能减少很多工作量?</li> </ul> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td><img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /><br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /></td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td><img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /><br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="middle"><img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /> </td> <td valign="top" align="right"><a class="fbox" ><strong>回页?/strong></a></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p><a name="N1006C"><span id="wmqeeuq" class="atitle">2. JpetStoreq?/span></a></p> <p><a name="N10072"><span id="wmqeeuq" class="smalltitle">2.1. 背景</span></a></p> <p>最初是Sun公司的J2EE petstoreQ其最主要目的是用于学习J2EEQ但是其~点也很明显Q就是过度设计了。接着Oracle用J2EE petstore来比较各应用服务器的性能。微软推ZZ.Netq_?Pet shopQ用于竞争J2EE petstore。而JpetStore则是l过改良的基于struts的轻便框架J2EE web应用E序Q相比来_JpetStore设计和架构更优良Q各层定义清晎ͼ使用了很多最佛_践和模式Q避免了很多"反模?Q如使用存储q程Q在java代码中嵌入SQL语句Q把HTML存储在数据库中等{。最新版本是JpetStore 4.0?/p> <p><a name="N1007B"><span id="wmqeeuq" class="smalltitle">2.2. JpetStore开发运行环境的建立</span></a></p> <p>1、开发环?/p> <ul> <li>Java SDK 1.4.2 <li>Apache Tomcat 4.1.31 <li>Eclipse-SDK-3.0.1-win32 <li>HSQLDB 1.7.2 </li> </ul> <p>2、Eclipse插g</p> <ul> <li>EMF SDK 2.0.1QEclipse建模框架Qlomboz插g需要,可以使用runtime版本? <li>lomboz 3.0QJ2EE插gQ用来在Eclipse中开发J2EE应用E序 <li>Spring IDE 1.0.3QSpring Bean配置理插g <li>xmlbuddy_2.0.10Q编辑XMLQ用免费版功能即? <li>tomcatPluginV3Qtomcat理插g <li>Properties EditorQ编辑java的属性文?q可以预览以及自动存盘ؓUnicode格式。免M手工或者ANT调用native2ascii的麻烦?</li> </ul> <p>3、示例源E序</p> <ul> <li>ibatisCZE序JpetStore 4.0 http://www.ibatis.com/jpetstore/jpetstore.html <li>攚w后的源E序Q?springQ(源码链接Q?</li> </ul> <p><a name="N100B7"><span id="wmqeeuq" class="smalltitle">2.3. 架构</span></a></p> <br /> <a name="N100BF"><strong>? JpetStore架构?/strong></a><br /> <img height="279" alt="? JpetStore架构? src="http://www.ibm.com/developerworks/cn/java/j-s-s-i/images/image002.jpg" width="553" border="0" /> <br /> <p>? 是JPetStore架构图,更详l的内容请参见JPetStore的白皮书。参照这个架构图Q让我们E微剖析一下源代码Q得出JpetStore 4.0的具体实现图Q见?Q,思\一下子p然开朗了。前a中提到的非传l的struts开发模式,关键在struts Actioncdform beancM?/p> <p>struts Actioncd有一个:BeanAction。没错,实是一个!与传l的struts~程方式很不同。再仔细研究BeanActionc,发现它其实是一个通用c,利用反射原理Q根据URL来决定调用formbean的哪个方法。BeanAction大大化了struts的编E模式,降低了对struts的依赖(与struts以及WEB容器有关的几个类都放在com.ibatis.struts包下Q其它的c都可以直接复用Q。利用这U模式,我们会很Ҏ的把它移植到新的框架如JSFQspring?/p> <p>q样重心p{Udform bean上了Q它已经不是普通意义上的form bean了。查看源代码Q可以看到它不仅仅有数据和校?重置ҎQ而且已经h了行为,从这个意义上来说Q它更像一个BO(Business Object)。这是前文讲到的,BeanActioncd用反原理,ҎURL来决定调用form bean的哪个方法(行ؓQ。form bean的这些方法的{֐很简单,例如Q?/p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> public String myActionMethod() { //..work return "success"; } </pre> </td> </tr> </tbody> </table> <br /> <p>Ҏ的返回值直接就是字W串Q对应的是forward的名Uͼ而不再是ActionForward对象Q创建ActionForward对象的Q务已l由BeanActioncM劳了?/p> <p>另外Q程序还提供了ActionContext工具c,该工L装了request 、response、form parameters、request attributes、session attributes?application attributes中的数据存取操作Q简单而线E安全,form beancM用该工具cd以进一步从表现层框架解耦?/p> <p>在这里需要特别指出的是,BeanActioncL对struts扩展的一个有益尝试,虽然提供了非常好的应用开发模式,但是它还非常斎ͼ一直在发展中?/p> <br /> <a name="N100EC"><strong>? JpetStore 4.0具体实现</strong></a><br /> <img height="588" alt="? JpetStore 4.0具体实现" src="http://www.ibm.com/developerworks/cn/java/j-s-s-i/images/image004.gif" width="582" border="0" /> <br /> <p><a name="N100FC"><span id="wmqeeuq" class="smalltitle">2.4. 代码剖析</span></a></p> <p>下面p我们开始进一步分析JpetStore4.0的源代码Qؓ下面的改造铺路?/p> <ul> <li>BeanAction.java是唯一一个Struts actionc,位于com.ibatis.struts包下。正如上文所aQ它是一个通用的控制类Q利用反机Ӟ把控制{Udform bean的某个方法来处理。详l处理过E参考其源代码,单明晰? <li> <p>Form beancM于com.ibatis.jpetstore.presentation包下Q命名规则ؓ***Bean。Form beancd部承于BaseBeanc,而BaseBeancd际承于ActionFormQ因此,Form beancd是Struts?ActionFormQForm beancȝ属性数据就由struts框架自动填充。而实际上QJpetStore4.0扩展了struts中ActionForm的应用: Form beanc还h行ؓQ更像一个BO,其行为(ҎQ由BeanActionҎ配置Qstruts-config.xmlQ的URL来调用。虽然如此,我们q是把Form beancd位于表现层?/p> <p>Struts-config.xml的配|里?U映方式,来告诉BeanAction把控制{到哪个form bean对象的哪个方法来处理?/p> <p>以这个请求连接ؓ例http://localhost/jpetstore4/shop/viewOrder.do</p> <p>1. URL Pattern</p> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> <action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction" name="orderBean" scope="session" validate="false"> <forward name="success" path="/order/ViewOrder.jsp"/> </action> </pre> </td> </tr> </tbody> </table> <br /> <p>此种方式表示Q控制将被{发到"orderBean"q个form bean对象 ?viewOrder"ҎQ行为)来处理。方法名?path"参数的以"/"分隔的最后一部分?/p> <p>2. Method Parameter</p> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> <action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction" name="orderBean" parameter="viewOrder" scope="session" validate="false"> <forward name="success" path="/order/ViewOrder.jsp"/> </action> </pre> </td> </tr> </tbody> </table> <br /> <p>此种方式表示Q控制将被{发到"orderBean"q个form bean对象?viewOrder"ҎQ行为)来处理。配|中?parameter"参数表示form beancM的方法?parameter"参数优先?path"参数?/p> <p>3. No Method call</p> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> <action path="/shop/viewOrder" type="com.ibatis.struts.BeanAction" name="orderBean" parameter="*" scope="session" validate="false"> <forward name="success" path="/order/ViewOrder.jsp"/> </action> </pre> </td> </tr> </tbody> </table> <br /> <p>此种方式表示Qform bean上没有Q何方法被调用。如果存?name"属性,则struts把表单参数等数据填充到form bean对象后,把控制{发到"success"。否则,如果name为空Q则直接转发控制?success"?/p> <p>q就相当于struts内置的org.apache.struts.actions.ForwardAction的功?/p> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> <action path="/shop/viewOrder" type="org.apache.struts.actions.ForwardAction" parameter="/order/ViewOrder.jsp " scope="session" validate="false"> </action> </pre> </td> </tr> </tbody> </table> <br /> <li>ServicecM于com.ibatis.jpetstore.service包下Q属于业务层。这些类装了业务以及相应的事务控制。Servicecȝform beancL调用? <li>com.ibatis.jpetstore.persistence.iface包下的类是DAO接口Q属于业务层Q其屏蔽了底层的数据库操作,供具体的ServicecL调用。DaoConfigcL工具c(DAO工厂c)QServicec通过DaoConfigcL获得相应的DAO接口Q而不用关心底层的具体数据库操作,实现了如?中{耦合2}的解耦? <li>com.ibatis.jpetstore.persistence.sqlmapdao包下的类是对应DAO接口的具体实玎ͼ在JpetStore4.0中采用了ibatis来实现ORM。这些实现类l承BaseSqlMapDaoc,而BaseSqlMapDaocdl承ibatis DAO 框架中的SqlMapDaoTemplatecRibatis的配|文件存攑֜com.ibatis.jpetstore.persistence.sqlmapdao.sql目录下。这些类和配|文件位于数据层 <li>DomaincM于com.ibatis.jpetstore.domain包下Q是普通的javabean。在q里用作数据传输对象QDTOQ,贯穿视图层、业务层和数据层Q用于在不同层之间传输数据?/li> </ul> <p>剩下的部分就比较单了Q请看具体的源代码,非常清晰?/p> <p><a name="N10149"><span id="wmqeeuq" class="smalltitle">2.5. 需要改造的地方</span></a></p> <p>JpetStore4.0的关键就在struts Actioncdform beancMQ这也是其精华之一Q虽然该实现方式是试验性,待扩充和验证Q,在此ơ改造中我们要保留下来,x制层一点不变,表现层获取相应业务类的方式变了(要加载spring环境Q,其它保持不变。要特别x的改动是业务层和持久层,q运的是JpetStore4.0设计非常好,需要改动的地方非常,而且由模式可循,如下Q?/p> <p>1. 业务层和数据层用Spring BeanFactory机制理?/p> <p>2. 业务层的事务由spring 的aop通过声明来完成?/p> <p>3. 表现层(form beanQ获取业务类的方法改p定义工厂cL实现Q加载spring环境Q?/p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td><img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /><br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /></td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td><img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /><br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="middle"><img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /> </td> <td valign="top" align="right"><a class="fbox" ><strong>回页?/strong></a></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p><a name="N1015B"><span id="wmqeeuq" class="atitle">3. JPetStore的改?/span></a></p> <p><a name="N10161"><span id="wmqeeuq" class="smalltitle">3.1. 攚w后的架?/span></a></p> <br /> <img height="640" alt="" src="http://www.ibm.com/developerworks/cn/java/j-s-s-i/images/image005.gif" width="582" border="0" /> <br /> <p>其中U色部分是要增加的部分,蓝色部分是要修改的部分。下面就让我们逐一剖析?/p> <p><a name="N1017B"><span id="wmqeeuq" class="smalltitle">3.2. Spring Context的加?/span></a></p> <p>Z在Struts中加载Spring ContextQ一般会在struts-config.xml的最后添加如下部分:</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"><plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml" /> </plug-in> </pre> </td> </tr> </tbody> </table> <br /> <p>Spring在设计时充分考虑C与Struts的协同工作,通过内置的Struts Plug-in在两者之间提供了良好的结合点。但是,因ؓ在这里我们一点也不改动JPetStore的控制层(q是JpetStore4.0的精华之一)Q所以本文不准备采用此方式来加蝲ApplicationContext。我们利用的是spring framework 的BeanFactory机制,采用自定义的工具c(bean工厂c)来加载spring的配|文Ӟ从中可以看出Spring有多灉|Q它提供了各U不同的方式来用其不同的部?层次Q您只需要用你想用的Q不需要的部分可以不用?/p> <p>具体的来_是在com.ibatis.spring包下创徏CustomBeanFactoryc,spring的配|文件applicationContext.xml也放在这个目录下。以下就是该cȝ全部代码Q很单:</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode">public final class CustomBeanFactory { static XmlBeanFactory factory = null; static { Resource is = new InputStreamResource( CustomBeanFactory.class.getResourceAsStream("applicationContext.xml")); factory = new XmlBeanFactory(is); } public static Object getBean(String beanName){ return factory.getBean(beanName); } } </pre> </td> </tr> </tbody> </table> <br /> <p>实际上就是封装了Spring 的XMLBeanFactory而已Qƈ且Spring的配|文件只需要加载一ơ,以后可以直接用CustomBeanFactory.getBean("someBean")来获得需要的对象?例如someBean)Q而不需要知道具体的cRCustomBeanFactorycȝ于{耦合1}的解耦?/p> <p>CustomBeanFactorycd本文中只用于表现层的form bean对象获得servicecȝ对象Q因为我们没有把form bean对象配置在applicationContext.xml中。但是,Z么不把表现层的form beancM配置h呢,q样q不着qCustomBeanFactory个类了,Spring会帮助我们创建需要的一切?问题的答案就在于form beancLstruts的ActionFormc!如果大家熟悉strutsQ就会知道ActionFormcLstruts自动创徏的:在一ơ请求中Qstruts判断Q如果ActionForm实例不存在,创Z个ActionForm对象Q把客户提交的表单数据保存到ActionForm对象中。因此formbeancȝ对象׃能由spring来创建,但是servicecM及数据层的DAOcd以,所以只有他们在spring中配|?/p> <p>所以,很自然的Q我们就创徏了CustomBeanFactoryc,在表现层来衔接struts和spring。就q么单,实现了另一U方式的{耦合一}的解耦?/p> <p><a name="N101A5"><span id="wmqeeuq" class="smalltitle">3.3. 表现?/span></a></p> ? <p>面分析到Qstruts和spring是在表现层衔接v来的Q那么表现层p做稍微的更改Q即所需要的servicecȝ对象创徏上。以表现层的AccountBeancMؓ例:</p> <p>原来的源代码如下</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> private static final AccountService accountService = AccountService.getInstance(); private static final CatalogService catalogService = CatalogService.getInstance(); </pre> </td> </tr> </tbody> </table> <br /> <p>攚w后的源代码如下</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> private static final AccountService accountService = (AccountService)CustomBeanFactory.getBean("AccountService"); private static final CatalogService catalogService = (CatalogService)CustomBeanFactory.getBean("CatalogService"); </pre> </td> </tr> </tbody> </table> <br /> <p>其他的几个presentationcM同样方式攚w。这P表现层就完成了。关于表现层的其它部分如JSP{一概不动。也许您会说Q没有看Z么特别之处的好处啊?你还是额外实C一个工厂类。别着急,帷幕刚刚开启,spring是在表现层引入,但您发没发现Q?/p> <ul> <li>presentationcM仅面向servicecȝ接口~程Q具?AccountService"是哪个实现类QpresentationcM知道Q是在spring的配|文仉配置。(本例中,Z最大限度的保持原来的代码不作变化,没有抽象出接口)。Spring鼓励面向接口~程Q因为是如此的方便和自然Q当然您也可以不q么做? <li>CustomBeanFactoryq个工厂cMؓ什么会如此单,因ؓ其直接用了Spring的BeanFactory。Spring从其核心而言Q是一个DI容器Q其设计哲学是提供一U无侵入式的高扩展性的框架。ؓ了实现这个目标,Spring 大量引入了Java 的Reflection机制Q通过动态调用的方式避免编码方式的U束Qƈ在此基础上徏立了其核心组件BeanFactoryQ以此作为其依赖注入机制的实现基。org.springframework.beans包中包括了这些核心组件的实现c,核心中的核心为BeanWrapper和BeanFactorycR?</li> </ul> <p><a name="N101D2"><span id="wmqeeuq" class="smalltitle">3.4. 持久?/span></a></p> <p>在讨Z务层之前Q我们先看一下持久层Q如下图所C:</p> <br /> <img height="416" alt="" src="http://www.ibm.com/developerworks/cn/java/j-s-s-i/images/image007.jpg" width="320" border="0" /> <br /> <p>在上文中Q我们把iface包下的DAO接口归ؓ业务层,在这里不需要做修改。ibatis的sql配置文g也不需要改。要改的是DAO实现c,q在spring的配|文件中配置h?/p> <p>1、修改基c?/p> <p>所有的DAO实现c都l承于BaseSqlMapDaocR修改BaseSqlMapDaocd下:</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode">public class BaseSqlMapDao extends SqlMapClientDaoSupport { protected static final int PAGE_SIZE = 4; protected SqlMapClientTemplate smcTemplate = this.getSqlMapClientTemplate(); public BaseSqlMapDao() { } } </pre> </td> </tr> </tbody> </table> <br /> <p>使BaseSqlMapDaocL为承于Spring提供的SqlMapClientDaoSupportc,q定义了一个保护属性smcTemplateQ其cd为SqlMapClientTemplate。关于SqlMapClientTemplatecȝ详细说明请参照附录中?Spring中文参考手?</p> <p>2、修改DAO实现c?/p> <p>所有的DAO实现c还是承于BaseSqlMapDaoc,实现相应的DAO接口Q但其相应的DAO操作委托SqlMapClientTemplate来执行,以AccountSqlMapDaocMؓ例,部分代码如下Q?/p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> public List getUsernameList() { return smcTemplate.queryForList("getUsernameList", null); } public Account getAccount(String username, String password) { Account account = new Account(); account.setUsername(username); account.setPassword(password); return (Account) smcTemplate.queryForObject("getAccountByUsernameAndPassword", account); } public void insertAccount(Account account) { smcTemplate.update("insertAccount", account); smcTemplate.update("insertProfile", account); smcTemplate.update("insertSignon", account); } </pre> </td> </tr> </tbody> </table> <br /> <p>p么简单,所有函数的{֐都是一LQ只需要查找替换就可以了!</p> <p>3、除d厂类以及相应的配|文?/p> <p>除去DaoConfig.javaq个DAO工厂cd相应的配|文件dao.xmlQ因为DAO的获取现在要用spring来管理?/p> <p>4、DAO在Spring中的配置QapplicationContext.xmlQ?/p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>org.hsqldb.jdbcDriver</value> </property> <property name="url"> <value>jdbc:hsqldb:hsql://localhost/xdb</value> </property> <property name="username"> <value>sa</value> </property> <property name="password"> <value></value> </property> </bean> <!-- ibatis sqlMapClient config --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> <property name="configLocation"> <value> classpath:com\ibatis\jpetstore\persistence\sqlmapdao\sql\sql-map-config.xml </value> </property> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- Transactions --> <bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource"> <ref bean="dataSource"/> </property> </bean> <!-- persistence layer --> <bean id="AccountDao" class="com.ibatis.jpetstore.persistence.sqlmapdao.AccountSqlMapDao"> <property name="sqlMapClient"> <ref local="sqlMapClient"/> </property> </bean> </pre> </td> </tr> </tbody> </table> <br /> <p>具体的语法请参照附录中的"Spring中文参考手?。在q里只简单解释一下:</p> <p>1. 我们首先创徏一个数据源dataSourceQ在q里配置的是hsqldb数据库。如果是ORACLE数据库,driverClassName的值是"oracle.jdbc.driver.OracleDriver"QURL的值类g"jdbc:oracle:thin:@wugfMobile:1521:cdcf"。数据源现在由spring来管理,那么现在我们可以去掉properties目录下database.propertiesq个配置文g了;q有不要忘记修改sql-map-config.xmlQ去?lt;properties resource="properties/database.properties"/>对它的引用?/p> <p>2. sqlMapClient节点。这个是针对ibatis SqlMap的SqlMapClientFactoryBean配置。实际上配置了一个sqlMapClient的创建工厂类。configLocation属性配|了ibatis映射文g的名U。dataSource属性指向了使用的数据源Q这h有用sqlMapClient的DAO都默认用了该数据源Q除非在DAO的配|中另外昑ּ指定?/p> <p>3. TransactionManager节点。定义了事务Q用的是DataSourceTransactionManager?/p> <p>4. 下面可以定义DAO节点了,如AccountDaoQ它的实现类是com.ibatis.jpetstore.persistence.sqlmapdao.AccountSqlMapDaoQ用的SQL配置从sqlMapClient中读取,数据库连接没有特别列出,那么是默认使用sqlMapClient配置的数据源datasource?/p> <p>q样Q我们就把持久层攚w完了,其他的DAO配置cM于AccountDao。怎么P单吧。这ơ有接口了:Q?AccountDao接口Q?gt;AccountSqlMapDao实现?/p> <p><a name="N10237"><span id="wmqeeuq" class="smalltitle">3.5. 业务?/span></a></p> <p>业务层的位置以及相关c,如下图所C:</p> <br /> <img height="240" alt="" src="http://www.ibm.com/developerworks/cn/java/j-s-s-i/images/image009.jpg" width="259" border="0" /> <br /> <p>在这个例子中只有3个业务类Q我们以OrderServicecMؓ例来攚w,q个cL最复杂的,其中涉及了事务?/p> <p>1、在ApplicationContext配置文g中增加bean的配|:</p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode"> <bean id="OrderService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref local="TransactionManager"></ref> </property> <property name="target"> <bean class="com.ibatis.jpetstore.service.OrderService"> <property name="itemDao"> <ref bean="ItemDao"/> </property> <property name="orderDao"> <ref bean="OrderDao"/> </property> <property name="sequenceDao"> <ref bean="SequenceDao"/> </property> </bean> </property> <property name="transactionAttributes"> <props> <prop key="insert*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </pre> </td> </tr> </tbody> </table> <br /> <p>定义了一个OrderServiceQ还是很Ҏ懂的。ؓ了简单v见,使用了嵌套beanQ其实现cLcom.ibatis.jpetstore.service.OrderServiceQ分别引用了ItemDaoQOrderDaoQSequenceDao。该bean的insert*实现了事务管?AOP方式)。TransactionProxyFactoryBean自动创徏一个事务advisorQ?该advisor包括一个基于事务属性的pointcut,因此只有事务性的Ҏ被拦截?/p> <p>2、业务类的修?/p> <p>以OrderServiceZQ?/p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="code-outline"> <pre class="displaycode">public class OrderService { /* Private Fields */ private ItemDao itemDao; private OrderDao orderDao; private SequenceDao sequenceDao; /* Constructors */ public OrderService() { } /** * @param itemDao 要设|的 itemDao? */ public final void setItemDao(ItemDao itemDao) { this.itemDao = itemDao; } /** * @param orderDao 要设|的 orderDao? */ public final void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } /** * @param sequenceDao 要设|的 sequenceDao? */ public final void setSequenceDao(SequenceDao sequenceDao) { this.sequenceDao = sequenceDao; } //剩下的部? ……. } </pre> </td> </tr> </tbody> </table> <br /> <p>U色部分Z攚w分。Spring采用的是Type2的设|依赖注入,所以我们只需要定义属性和相应的设值函数就可以了,ItemDaoQOrderDaoQSequenceDao的值由spring在运行期间注入。构造函数就可以为空了,另外也不需要自q写代码处理事务了Q事务在配置中声明)QdaoManager.startTransaction();{与事务相关的语句也可以L了。和原来的代码比较一下,是不是处理精了很多!可以更关注业务的实现?/p> <br /> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td><img height="1" alt="" src="http://www.ibm.com/i/v14/rules/blue_rule.gif" width="100%" /><br /> <img height="6" alt="" src="http://www.ibm.com/i/c.gif" width="8" border="0" /></td> </tr> </tbody> </table> <table class="no-print" cellspacing="0" cellpadding="0" align="right"> <tbody> <tr align="right"> <td><img height="4" alt="" src="http://www.ibm.com/i/c.gif" width="100%" /><br /> <table cellspacing="0" cellpadding="0" border="0"> <tbody> <tr> <td valign="middle"><img height="16" alt="" src="http://www.ibm.com/i/v14/icons/u_bold.gif" width="16" border="0" /><br /> </td> <td valign="top" align="right"><a class="fbox" ><strong>回页?/strong></a></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <br /> <br /> <p><a name="N10275"><span id="wmqeeuq" class="atitle">4. l束?/span></a></p> <p>ibatis是一个功能强大实用的SQL Map工具Q可以直接控制SQL,为系l设计提供了更大的自q间。其提供的最新示例程序JpetStore 4.0,设计优雅Q应用了q今为止很多最佛_践和设计模式Q非帔R于学习以及在此基础上创量的J2EE WEB应用E序。JpetStore 4.0是基于struts的,本文在此基础上,最大程度保持了原有设计的精华以及最的代码改动量,在业务层和持久化层引入了Spring。在您阅M本文以及攚w后的源代码后,会深切的感受到Spring带来的种U好处:自然的面向接口的~程Q业务对象的依赖注入Q一致的数据存取框架和声明式的事务处理,l一的配|文?#8230;更重要的是Spring既是全面的又是模块化的,Spring有分层的体系l构Q这意味着您能选择仅仅使用它Q何一个独立的部分Q就像本文,而它的架构又是内部一致?/p> <br /> <br /> <p><a name="resources"><span id="wmqeeuq" class="atitle">参考资?</span></a></p> <ul> <li>jpetstore相关各种资料和源E序 <a >http://www.ibatis.com/jpetstore/jpetstore.html</a><br /> <br /> <li>Spring中文参考手?a >http://www.jactiongroup.net/reference/html/index.html</a><br /> <br /> <li>Spring 开发指?夏昕<br /> <br /> <li>Struts <a >http://struts.apache.org/</a><br /> </li> </ul> <img src ="http://www.aygfsteel.com/libin2722/aggbug/165532.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/libin2722/" target="_blank">C物</a> 2007-12-05 15:40 <a href="http://www.aygfsteel.com/libin2722/articles/165532.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>_NSpring——Java轻量U架构开发实?/title><link>http://www.aygfsteel.com/libin2722/articles/165531.html</link><dc:creator>C物</dc:creator><author>C物</author><pubDate>Wed, 05 Dec 2007 07:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/libin2722/articles/165531.html</guid><wfw:comment>http://www.aygfsteel.com/libin2722/comments/165531.html</wfw:comment><comments>http://www.aygfsteel.com/libin2722/articles/165531.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/libin2722/comments/commentRss/165531.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/libin2722/services/trackbacks/165531.html</trackback:ping><description><![CDATA[<div style="font-size: 15px; text-align: center"><a >10.3 Spring对IBatis的支?nbsp; </a></div> <div style="font-size: 15px; text-align: center"><a >http://book.csdn.net/</a> 2006-8-15 16:27:00 </div> <div style="border-right: #0b5f98 1px solid; border-top: #0b5f98 1px solid; float: left; margin: 0px auto; border-left: #0b5f98 1px solid; width: 600px; border-bottom: #0b5f98 1px solid"> <div style="padding-right: 1px; padding-left: 1px; float: left; padding-bottom: 1px; width: 16px; color: white; padding-top: 1px; background-color: #0b5f98">图书D </div> <div style="padding-left: 2px; float: right; width: 570px; line-height: 16pt; text-align: left"> <h1 id="divCurrentNode" style="padding-left: 2px; font-size: 12px; width: 100%; color: #b83507; text-align: left">当前章节:<a ><font color="red">10.3 Spring对IBatis的支?/font></a></h1> <div class="wmqeeuq" id="divRelateNode" style="padding-left: 2px"> <div style="float: left; width: 49%">·<a >4.1 模式QPatternQ入?/a></div> <div style="float: right; width: 49%">·<a >4.2 工厂模式QDesign PatternQFactory MethodQ的_N</a></div> <div style="float: left; width: 49%">·<a >4.3 单例模式QDesign PatternQSingletonQ?/a></div> <div style="float: right; width: 49%">·<a >10.4 Spring对Hibernate的支?/a></div> <div style="float: left; width: 49%">·<a >10.5 结</a></div> <div style="float: right; width: 49%">·<a >18.1 模仿对象</a></div> </div> </div> </div> <div class="wmqeeuq" id="main"> <div class="wmqeeuq" id="text"> <div class="wmqeeuq" id="csdn_zhaig_ad_yahoo_2"> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td> <dl> <dt><a target="_blank">IT 游戏 开发h才专业招聘网?/a></dt> <dd>每日上万个IT历更?快捷有效的招聘求?</dd> <dd><a target="_blank">www.jobg.cn</a></dd> <dt><a target="_blank">非常4Q?大学生实训计划,首付6800</a></dt> <dd>4个月的全套Java技术体pȝ实训, 1个月的企业大型真实项目的实战开发?</dd> <dd><a target="_blank">www.kettas.com.cn/</a></dd> <dt><a target="_blank">q距L触开源ERP</a></dt> <dd>开源带来了哪些斚w的好处? 开源后的运营方向又是怎样Q?</dd> <dd><a target="_blank">live.csdn.net</a></dd></dl></td> </tr> </tbody> </table> </div> <p style="text-indent: 20.4pt">Spring<span style="font-family: 宋体">?/span>IBatis<span style="font-family: 宋体">提供了完善的内徏支持。?/span>Spring<span style="font-family: 宋体">提供?/span>IBatis<span style="font-family: 宋体">辅助c,可以大大化原有的</span>IBatis<span style="font-family: 宋体">讉K代码。这些辅助类位于</span>org.springframework.orm.ibatis<span style="font-family: 宋体">包下Q目?/span>Spring<span style="font-family: 宋体">可同时支?/span>IBatis1.3.x<span style="font-family: 宋体">?/span>2.0<span style="font-family: 宋体">?/span> <span style="font-family: 宋体">此外Q针?/span>IBatis<span style="font-family: 宋体">Q?/span>Spring<span style="font-family: 宋体">也提供了?/span>JdbcTemplate<span style="font-family: 宋体">一致的异常处理方式</span></p> <h3 style="margin: 15pt 0cm 15pt 48pt; vertical-align: baseline; text-indent: -48pt; line-height: 15.7pt"><span style="font-weight: normal; font-size: 13pt; font-family: Arial">10.3.1<span style="font: 7pt 'Times New Roman'">         </span></span><span style="font-weight: normal; font-size: 13pt; font-family: 黑体">标准</span><span style="font-weight: normal; font-size: 13pt; font-family: Arial">JavaBean</span><span style="font-weight: normal; font-size: 13pt; font-family: 黑体">实体和映?/span></h3> <p style="text-indent: 20.4pt">Spring<span style="font-family: 宋体">宠物店非常典型的展现?/span>Spring<span style="font-family: 宋体">?/span>IBatis<span style="font-family: 宋体">的整合,下文围l宠物店展开介绍?/span></p> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">首先来看宠物店中的一个领域对象(它是一个标准的</span>JavaBean<span style="font-family: 宋体">Q和它的映射文gQ如代码</span>10.13~10.14<span style="font-family: 宋体">所C?/span></p> <div style="border-right: windowtext 1pt solid; padding-right: 4pt; border-top: windowtext 1pt solid; padding-left: 4pt; padding-bottom: 1pt; margin-left: 20.4pt; border-left: windowtext 1pt solid; margin-right: 0cm; padding-top: 1pt; border-bottom: windowtext 1pt solid"> <p style="border-right: medium none; padding-right: 0cm; border-top: medium none; padding-left: 0cm; padding-bottom: 0cm; margin-left: 0cm; border-left: medium none; padding-top: 0cm; border-bottom: medium none"><span style="font-family: 黑体">代码</span>10.13  Product.java</p> </div> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">public class Product implements Serializable {</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  private String productId;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  private String categoryId;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  private String name;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  private String description;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"> <span style="font-family: 宋体">省略</span><span style="font-family: 'Courier New'">getter/setter...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">}</span></p> <div style="border-right: windowtext 1pt solid; padding-right: 4pt; border-top: windowtext 1pt solid; padding-left: 4pt; padding-bottom: 1pt; margin-left: 20.4pt; border-left: windowtext 1pt solid; margin-right: 0cm; padding-top: 1pt; border-bottom: windowtext 1pt solid"> <p style="border-right: medium none; padding-right: 0cm; border-top: medium none; padding-left: 0cm; padding-bottom: 0cm; margin-left: 0cm; border-left: medium none; padding-top: 0cm; border-bottom: medium none"><span style="font-family: 黑体">代码</span>10.14  Product.xml</p> </div> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><sqlMap namespace="Product"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <resultMap id="result"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"> class="org.springframework.samples.jpetstore.domain.Product"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <result property="productId" column="productid" columnIndex="1"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </resultMap></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <select id="getProduct" resultMap="result"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    select productid, name, descn, category from product where productid = #value#</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </select></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <select id="getProductListByCategory" resultMap="result"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    select productid, name, descn, category from product where category = #value#</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </select></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"></sqlMap></span></p> <h3 style="margin: 15pt 0cm; vertical-align: baseline; text-indent: 0cm; line-height: 15.7pt"><span style="font-weight: normal; font-size: 13pt; font-family: Arial">10.3.2</span> <span style="font-weight: normal; font-size: 13pt; font-family: 黑体">衔接</span><span style="font-weight: normal; font-size: 13pt; font-family: Arial">IBatis</span><span style="font-weight: normal; font-size: 13pt; font-family: 黑体">配置?/span><span style="font-weight: normal; font-size: 13pt; font-family: Arial">DAO</span><span style="font-weight: normal; font-size: 13pt; font-family: 黑体">实现</span></h3> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">接着l出</span>IBatis<span style="font-family: 宋体">的基本配|文Ӟ如代?/span>10.15<span style="font-family: 宋体">所C?/span></p> <div style="border-right: windowtext 1pt solid; padding-right: 4pt; border-top: windowtext 1pt solid; padding-left: 4pt; padding-bottom: 1pt; margin-left: 20.4pt; border-left: windowtext 1pt solid; margin-right: 0cm; padding-top: 1pt; border-bottom: windowtext 1pt solid"> <p style="border-right: medium none; padding-right: 0cm; border-top: medium none; padding-left: 0cm; padding-bottom: 0cm; margin-left: 0cm; border-left: medium none; padding-top: 0cm; border-bottom: medium none"><span style="font-family: 黑体">代码</span>10.15  sql-map-config.xml</p> </div> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><sqlMapConfig></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <sqlMap resource="org/springframework/samples/jpetstore/dao/ibatis/maps/<strong>Product.xml</strong>"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"></sqlMapConfig></span></p> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">在宠物店中,该文件仅包含了所有领域对象的映射文gQ而挪C关于</span>IBatis<span style="font-family: 宋体">的事务和数据源配|(?/span>IBatis<span style="font-family: 宋体">配置文g中的</span>transactionManager<span style="font-family: 宋体">元素和它的子元素</span>dataSource<span style="font-family: 宋体">Q?/span></p> <p style="margin: 7.65pt 0cm"><strong><span style="font-family: 楷体_GB2312">注意Q?/span></strong><span style="font-family: 楷体_GB2312">在稍后将要给出的</span>Spring<span style="font-family: 楷体_GB2312">配置文g中接手了q些配置Q这是一个整合点?/span></p> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">在宠物店中,持久和数据访问都是通过</span>DAO<span style="font-family: 宋体">来实现的。对?/span>Product<span style="font-family: 宋体">Q存在一个与其对应的</span>SqlMapProductDao<span style="font-family: 宋体">Q如代码</span>10.16<span style="font-family: 宋体">所C?/span></p> <div style="border-right: windowtext 1pt solid; padding-right: 4pt; border-top: windowtext 1pt solid; padding-left: 4pt; padding-bottom: 1pt; margin-left: 20.4pt; border-left: windowtext 1pt solid; margin-right: 0cm; padding-top: 1pt; border-bottom: windowtext 1pt solid"> <p style="border-right: medium none; padding-right: 0cm; border-top: medium none; padding-left: 0cm; padding-bottom: 0cm; margin-left: 0cm; border-left: medium none; padding-top: 0cm; border-bottom: medium none"><span style="font-family: 黑体">代码</span>10.16  SqlMapProductDao.java</p> </div> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">package org.springframework.samples.jpetstore.dao.ibatis;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import java.util.ArrayList;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import java.util.List;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import java.util.StringTokenizer;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import org.springframework.dao.DataAccessException;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import org.springframework.samples.jpetstore.dao.ProductDao;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">import org.springframework.samples.jpetstore.domain.Product;</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">public class SqlMapProductDao</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  extends <strong>SqlMapClientDaoSupport</strong></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  implements ProductDao {</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  public List getProductListByCategory(String categoryId)</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    throws DataAccessException {</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    return <strong>getSqlMapClientTemplate().queryForList</strong>("getProductListByCategory",</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"> categoryId);</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  }</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  public Product getProduct(String productId)</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    throws DataAccessException {</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    return (Product) <strong>getSqlMapClientTemplate().queryForObject</strong>("getProduct", productId);</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  }</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">}</span></p> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">上述代码中出C</span>Spring<span style="font-family: 宋体">提供?/span>IBatis DAO<span style="font-family: 宋体">支持cd获取</span>SqlMapClientTemplate<span style="font-family: 宋体">的父cL板方法,q和</span>JdbcDaoSupport<span style="font-family: 宋体">?/span>JdbcTemplate<span style="font-family: 宋体">的用具有一致的概念。ƈ且,q些操作都统一的抛?/span>Spring<span style="font-family: 宋体">的通用数据讉K异常</span>DataAccessException<span style="font-family: 宋体">?/span></p> <p style="margin: 7.65pt 0cm"><strong><span style="font-family: 楷体_GB2312">注意Q?/span></strong><span style="font-family: 楷体_GB2312">在早期的</span>IBatis1.3.x<span style="font-family: 楷体_GB2312">版本?/span>Dao<span style="font-family: 楷体_GB2312">支持cd模板cd别被命名?/span>SqlMapDaoSupport<span style="font-family: 楷体_GB2312">?/span>SqlMapTemplate<span style="font-family: 楷体_GB2312">Q在使用时不要؜淆?/span></p> <h3 style="margin: 15pt 0cm; vertical-align: baseline; text-indent: 0cm; line-height: 15.7pt"><span style="font-weight: normal; font-size: 13pt; font-family: Arial">10.3.3</span> <span style="font-weight: normal; font-size: 13pt; font-family: 黑体">关键整合点:</span><span style="font-weight: normal; font-size: 13pt; font-family: Arial">Spring</span><span style="font-weight: normal; font-size: 13pt; font-family: 黑体">配置文g</span></h3> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">有了以上?/span>DAO<span style="font-family: 宋体">lg后,来看一?/span>Spring<span style="font-family: 宋体">的配|,q是一个关键的整合点,如代?/span>10.17<span style="font-family: 宋体">所C?/span></p> <div style="border-right: windowtext 1pt solid; padding-right: 4pt; border-top: windowtext 1pt solid; padding-left: 4pt; padding-bottom: 1pt; margin-left: 20.4pt; border-left: windowtext 1pt solid; margin-right: 0cm; padding-top: 1pt; border-bottom: windowtext 1pt solid"> <p style="border-right: medium none; padding-right: 0cm; border-top: medium none; padding-left: 0cm; padding-bottom: 0cm; margin-left: 0cm; border-left: medium none; padding-top: 0cm; border-bottom: medium none"><span style="font-family: 黑体">代码</span>10.17  dataAccessContext-local.xml</p> </div> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><?xml version="1.0" encoding="UTF-8"?></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">"http://www.springframework.org/dtd/spring-beans.dtd"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><beans></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <!-- </span><span style="font-family: 宋体">相关数据源和事务理的定?/span><span style="font-family: 'Courier New'"> --></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <bean id="<strong>dataSource</strong>"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="driverClassName" value="${jdbc.driverClassName}"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="url" value="${jdbc.url}"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="username" value="${jdbc.username}"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="password" value="${jdbc.password}"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <!-- Transaction manager for a single JDBC DataSource --></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <bean id="<strong>transactionManager</strong>"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="dataSource" ref="dataSource"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <!-- Spring</span><span style="font-family: 宋体">提供?/span><span style="font-family: 'Courier New'">iBatis</span><span style="font-family: 宋体">?/span><span style="font-family: 'Courier New'">SqlMap</span><span style="font-family: 宋体">配置</span><span style="font-family: 'Courier New'">--></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><strong><span style="font-family: 'Courier New'">  <bean id="sqlMapClient"</span></strong></p> <p style="margin-left: 20.4pt; line-height: 10pt"><strong><span style="font-family: 'Courier New'">class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"></span></strong></p> <p style="margin-left: 20.4pt; line-height: 10pt"><strong><span style="font-family: 'Courier New'">    <property name="configLocation" value="WEB-INF/sql-map-config.xml"/></span></strong></p> <p style="margin-left: 20.4pt; line-height: 10pt"><strong><span style="font-family: 'Courier New'">    <property name="dataSource" ref="dataSource"/></span></strong></p> <p style="margin-left: 20.4pt; line-height: 10pt"><strong><span style="font-family: 'Courier New'">  </bean></span></strong></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <!-- DAO</span><span style="font-family: 宋体">定义</span><span style="font-family: 'Courier New'">--></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <bean id="productDao"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">class="org.springframework.samples.jpetstore.dao.ibatis.SqlMapProductDao"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="<strong>sqlMapClient</strong>" ref="sqlMapClient"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"></beans></span></p> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">可以发现Q?/span>Spring<span style="font-family: 宋体">在上q文件中分别配置了数据源和事务管理的{略Q其中挪M原先?/span>IBatis<span style="font-family: 宋体">文g中的配置?/span></p> <p style="margin: 7.65pt 0cm"><strong><span style="font-family: 楷体_GB2312">说明Q?/span></strong><span style="font-family: 楷体_GB2312">q样做的好处是可以通过</span>Spring IoC<span style="font-family: 楷体_GB2312">容器l一的管理资源,在稍后还可以看到Q?/span>Spring<span style="font-family: 楷体_GB2312">提供的声明性事务管理就是借助于统一的数据源和事务管理配|?/span></p> <p style="text-indent: 20.4pt">SqlMapClientFactoryBean<span style="font-family: 宋体">又是一个工?/span>bean<span style="font-family: 宋体">Q它暴露了两个关键属性用于注?/span>IBatis<span style="font-family: 宋体">配置文g和相关的数据源。在工厂内部Q通过d</span>IBatis<span style="font-family: 宋体">配置文gQ?/span>Spring<span style="font-family: 宋体">会创建出</span>IBatis<span style="font-family: 宋体">的核心组?/span>SqlMapClient<span style="font-family: 宋体">Qƈ向相关的</span>DAO<span style="font-family: 宋体">q行注射?/span></p> <p style="text-indent: 20.4pt">SqlMapProductDao<span style="font-family: 宋体">l承?/span>SqlMapClientDaoSupport<span style="font-family: 宋体">Q后者暴露出一?/span>sqlMapClient<span style="font-family: 宋体">属性,用于接受</span>Spring<span style="font-family: 宋体">的注?/span>SqlMapClientDaoSupport<span style="font-family: 宋体">会对其中装?/span>SqlMapClientTemplate<span style="font-family: 宋体">做相应的讄Q所?/span>DAO<span style="font-family: 宋体">子类便可在取?/span>SqlMapClientTemplate<span style="font-family: 宋体">时正常地工作了?/span></p> <h3 style="margin: 15pt 0cm; vertical-align: baseline; text-indent: 0cm; line-height: 15.7pt"><span style="font-weight: normal; font-size: 13pt; font-family: Arial">10.3.4</span> <span style="font-weight: normal; font-size: 13pt; font-family: 黑体">d声明式事务管?/span></h3> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">以上?/span>IBatis DAO<span style="font-family: 宋体">可以很自方便地被注射到相应的业务对象Qƈ参与?/span>Spring<span style="font-family: 宋体">提供的声明性事务中Q配|如代码</span>10.18<span style="font-family: 宋体">所C?/span></p> <div style="border-right: windowtext 1pt solid; padding-right: 4pt; border-top: windowtext 1pt solid; padding-left: 4pt; padding-bottom: 1pt; margin-left: 20.4pt; border-left: windowtext 1pt solid; margin-right: 0cm; padding-top: 1pt; border-bottom: windowtext 1pt solid"> <p style="border-right: medium none; padding-right: 0cm; border-top: medium none; padding-left: 0cm; padding-bottom: 0cm; margin-left: 0cm; border-left: medium none; padding-top: 0cm; border-bottom: medium none"><span style="font-family: 黑体">代码</span>10.18   applicationContext.xml</p> </div> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><?xml version="1.0" encoding="UTF-8"?></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">"http://www.springframework.org/dtd/spring-beans.dtd"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"><beans></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <!-- </span><span style="font-family: 宋体">通用属性文件定?/span><span style="font-family: 'Courier New'"> --></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <bean id="propertyConfigurer"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="locations"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">      <list></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">         ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">        <value>WEB-INF/jdbc.properties</value></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">      </list></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    </property></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <!-- </span><span style="font-family: 宋体">业务对象定义</span><span style="font-family: 'Courier New'"> --></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <bean id="baseTransactionProxy"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">abstract="true"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="transactionManager" ref="<strong>transactionManager</strong>"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="transactionAttributes"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">      <props></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">        <prop key="insert*">PROPAGATION_REQUIRED</prop></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">        <prop key="update*">PROPAGATION_REQUIRED</prop></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">        <prop key="*">PROPAGATION_REQUIRED,readOnly</prop></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">      </props></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    </property></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  <bean id="petStore" parent="baseTransactionProxy"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">    <property name="target"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">      <bean class="org.springframework.samples.jpetstore.domain.logic.PetStoreImpl"></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">       ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">       <property name="<strong>productDao</strong>" ref="productDao"/></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">        ...</span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">      </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'">  </bean></span></p> <p style="margin-left: 20.4pt; line-height: 10pt"><span style="font-family: 'Courier New'"></beans></span></p> <p style="text-indent: 20.4pt"><span style="font-family: 宋体">x基本完成了</span>Spring IoC<span style="font-family: 宋体">?/span>IBatis<span style="font-family: 宋体">的整合了Q当然也可以通过~程的方式来使用</span>Spring<span style="font-family: 宋体">所提供的模板和支持cR?/span></p> </div> </div> <img src ="http://www.aygfsteel.com/libin2722/aggbug/165531.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/libin2722/" target="_blank">C物</a> 2007-12-05 15:39 <a href="http://www.aygfsteel.com/libin2722/articles/165531.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title> springl合ibatis的一点经验,同网友分?/title><link>http://www.aygfsteel.com/libin2722/articles/165529.html</link><dc:creator>C物</dc:creator><author>C物</author><pubDate>Wed, 05 Dec 2007 07:39:00 GMT</pubDate><guid>http://www.aygfsteel.com/libin2722/articles/165529.html</guid><wfw:comment>http://www.aygfsteel.com/libin2722/comments/165529.html</wfw:comment><comments>http://www.aygfsteel.com/libin2722/articles/165529.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/libin2722/comments/commentRss/165529.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/libin2722/services/trackbacks/165529.html</trackback:ping><description><![CDATA[1? Spring要结合Ibatis要在Spring的应用程序上下文中配|:<br /> <table cellspacing="1" cellpadding="4" width="98%" align="center" bgcolor="#bad5ef" border="0"> <form> <tbody> <tr> <td style="font-size: 12px" bgcolor="#e6eef7" height="25">E序代码Q?/td> </tr> <tr> <td style="font-size: 12px" bgcolor="#ffffff"><bean id=<span style="color: #2a00ff">"dataSource"</span><br /> class=<span style="color: #2a00ff">"org.apache.commons.dbcp.BasicDataSource"</span><br /> destroy-method=<span style="color: #2a00ff">"close"</span>><br /> <property name=<span style="color: #2a00ff">"driverClassName"</span><br /> value=<span style="color: #2a00ff">"${jdbc.driverClassName}"</span> /><br /> <property name=<span style="color: #2a00ff">"url"</span> value=<span style="color: #2a00ff">"${jdbc.url}"</span> /><br /> <property name=<span style="color: #2a00ff">"username"</span> value=<span style="color: #2a00ff">"${jdbc.username}"</span> /><br /> <property name=<span style="color: #2a00ff">"password"</span> value=<span style="color: #2a00ff">"${jdbc.password}"</span> /><br /> </bean><br /> <br /> <!-- Spring iBatis SqlMapClient --><br /> <bean id=<span style="color: #2a00ff">"sqlMapClient"</span><br /> class=<span style="color: #2a00ff">"org.springframework.orm.ibatis.SqlMapClientFactoryBean"</span>><br /> <property name=<span style="color: #2a00ff">"configLocation"</span><br /> value=<span style="color: #2a00ff">"classpath:resource/sqlmap-config.xml"</span> /><br /> <property name=<span style="color: #2a00ff">"dataSource"</span> ref=<span style="color: #2a00ff">"dataSource"</span> /><br /> </bean></td> </tr> </form> </tbody> </table> <br /> 数据源也可以在sqlmap-config中配|?br /> 2、如果DAO的实现类是实现SqlMapClientDaoSupport的,则在应用E序上下文中中配|如下:<br /> <table cellspacing="1" cellpadding="4" width="98%" align="center" bgcolor="#bad5ef" border="0"> <form> <tbody> <tr> <td style="font-size: 12px" bgcolor="#e6eef7" height="25">E序代码Q?/td> </tr> <tr> <td style="font-size: 12px" bgcolor="#ffffff"><bean id=<span style="color: #2a00ff">"userInfoSqlMapDaoImpl"</span><br /> class=<span style="color: #2a00ff">"com.sample.persistence.sqlmap.ibatis.UserInfoSqlMapDaoImpl"</span>><br /> <property name=<span style="color: #2a00ff">"sqlMapClient"</span> ref=<span style="color: #2a00ff">"sqlMapClient"</span> /><br /> </bean><br /> </td> </tr> </form> </tbody> </table> <br /> 如果实现cL有其他的属性可以象上面的配|,如果q有其他的属性,则还要加其他的属性? <img src ="http://www.aygfsteel.com/libin2722/aggbug/165529.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/libin2722/" target="_blank">C物</a> 2007-12-05 15:39 <a href="http://www.aygfsteel.com/libin2722/articles/165529.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank">ʯ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ͨ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƺ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˫Ѽɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ӣɳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ӳ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank">Ȩ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʡ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˳</a>| <a href="http://" target="_blank">˻</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ͳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">˫</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>