版权声明:如有转载请求,请注明出处:http://blog.csdn.net/yzhz 杨争
前缀
|
例子
|
说明
|
classpath:
|
classpath:com/myapp/config.xml
|
从
classpath
中载入。
|
file:
|
file:/data/config.xml
|
从文件系统载入。
|
http:
|
http://myserver/logo.png
|
作为
URL
载入。
|
(none)
|
/data/config.xml
|
取决于当前的
ApplicationContext
|
org.springframework.jdbc.support
提供了SQLException的转换类和相关的工具类。
Spring
将所有jdbc处理中抛出的异常转换为spring dao包中定义的异常,这些异常是unchecked exception,我们可以对这些异常有选择的捕获。这也是Spring提供的一个很重要的特性。
看例子:
2
、DataSource可以JNDI,或者apache提供的
dbcp,或者spring自带的DriverManagerDataSource。
3
、需要了解的知识:
(2)
我们如果要获得connection,需要调用DataSourceUtils.getConncetion(),这个类会把SQLException的异常转换为spring的unchecked Exception,并且如果我们使用DataSourceTransactionManager的话,该connection会绑定到当前线程中。
JdbcTemplate
就是通过这种方式获得数据库连接的。
六、spring的ORM
Spring
的ORM提供了对常用ORM Mapping工具的封装,包括Hibernate、JDO、ibatis
等,ibatis由于使用和jdbc sql的访问方式相似,同时sql语句可控,便于DBA优化,所以深受普遍欢迎。
批量操作:
public class SqlMapAccountDao extends SqlMapClientDaoSupport implements AccountDao {
public void insertAccount(Account account) throws DataAccessException {
getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
executor.startBatch();
executor.update("insertAccount", account);
executor.update("insertAddress", account.getAddress());
executor.executeBatch();
}
});
}
}
七、Spring的事务管理
1
、全局事务和本地事务
2
、spring的事务
Spring
使用
PlatformTransactionManager来管理事务。定义如下:
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
3
、声明式事务管理
声明式事务管理,开发者不用编写事务代码就可以实现事务处理。
Spring
的事务管理是通过spring AOP来实现的。
我们通过TransactionProxyFactoryBean设置Spring事务代理,然后将目标对象包装在事务代理中。当定义TransactionProxyFactoryBean时,必须提供一个相关的PlatformTransactionManager的引用和事务属性。
<bean id="petStoreTarget">
</bean>
<bean id="petStore"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager"><ref bean="transactionManager"/></property>
<property name="target"><ref bean="petStoreTarget"/></property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED,-MyCheckedException</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
4
、编程式事务管理
Object result = transactionTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
updateOperation1();
return resultOfUpdateOperation2();
}
});
Spring
参考文献:
http://spring.jactiongroup.net/ spring
中文论坛
http://www.jactiongroup.net/reference2/html/ Spring 2.0
中文开发手册
http://www.springframework.org/ Spring
官方网址