最初我是用spring的aop来实现spring与orbroker“结合”的,在befor的时候把orbroker和trans注进
service去,after的时候commit,如果出现exception由rollback。怎么看怎么别扭,还是 江南白衣
这个“刨虫”提醒了一下,说是spring module的cvs上有orbrokerTemplate。
有现成的东西,俺就不用现学木匠造轮子了,省事省心,还顺眼。
从spring module的cvs上下源码,把org.springmodules.orm.orbroker、org.springmodules.orm.orbroker.support两个package的东西拷进project(或者是打成jar扔进去)。
与ibatis、hibernate一样,java代码部分很简单,我做的测试代码就这么几行
public interface ResultDAO {
List find(String parentId, String catalogId);
}
public class ResultDAOORBroker extends BrokerDaoSupport implements ResultDAO {
public List find(String parentId, String catalogId) {
return this.getBrokerTemplate().selectMany("find",
new String[]{"parentId", "catalogId"},
new String[]{parentId, catalogId});
}
}
public interface ResultManager {
List<Result> find(String parentId, String catalogId);
}
public class ResultManagerImpl implements ResultManager {
@SuppressWarnings("unchecked")
public List<Result> find(String parentId, String catalogId) {
return resultDAO.find(parentId, catalogId);
}
private ResultDAO resultDAO;
public void setResultDAO(ResultDAO resultDAO) {
this.resultDAO = resultDAO;
}
}
最主要的还是xml的配置文件,与用ibatis大体上差不了多少。
DataSource、TransactionManager是必须的
<bean id="TransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="DataSource" />
</bean>
<bean id="DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${marekt.jdbc.driver}" />
<property name="user" value="${marekt.jdbc.user}" />
<property name="password" value="${marekt.jdbc.password}" />
<property name="jdbcUrl" value="${marekt.jdbc.url}" />
<property name="initialPoolSize" value="2" />
</bean>
<bean id="PlaceholderConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:jdbc.properties" />
</bean>
然后是一个Broker的Factory
<bean id="BrokerFactory" class="org.springmodules.orm.orbroker.BrokerFactoryBean">
<property name="dataSource" ref="DataSource" />
<property name="configLocation" value="classpath:orbroker/orbroker.xml" />
</bean>
剩下的事情就是DAO和Manager了。
<bean id="ResultDAO" class="dao.impl.ResultDAOORBroker">
<property name="broker" ref="BrokerFactory" />
</bean>
<bean id="ResultManager"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="TransactionManager" />
<property name="transactionAttributes">
<props>
<prop key="*">PROPAGATION_REQUIRED</prop>
</props>
</property>
<property name="target">
<bean class="services.impl.ResultManagerImpl">
<property name="resultDAO" ref="ResultDAO" />
</bean>
</property>
</bean>