Spring ApplicationContext 配置文件:
- XML code
<!-- 定义DataSource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@10.132.224.203:1521:myorcl" />
<property name="username" value="*****" />
<property name="password" value="*****" />
</bean>
<bean id="sqlmapclient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:com/test/ibatis/sqlmap/sqlMapConfig.xml </value>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Ibatis DAO 实现类 -->
<bean id="iBatisDao" class="com.test.dao.impl.UserImpl">
<property name="sqlMapClient">
<ref local="sqlmapclient" />
</property>
</bean>
<!-- Struts Action类 注入业务类 -->
<bean id="useraction" class="com.test.struts.action.UserAction">
<property name="user" ref="user_service"> </property>
</bean>
<!-- 业务类 注入dao实现类 -->
<bean id="user_service" class="com.test.struts.service.UserService">
<property name="dao" ref="iBatisDao"> </property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManage"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"> </property>
</bean>
<!-- 装配事务 -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="target" ref="user_service"> </property>
<property name="transactionManager" ref="transactionManage"> </property>
<property name="transactionAttributes">
<props>
<prop key="get*">PROPAGATION_REQUIRED,readOnly </prop>
<prop key="*">PROPAGATION_REQUIRED,-MyException,-RemoteException,-SocketException </prop>
</props>
</property>
</bean>
其中涉及的重要类:
UserImpl 数据层实现类:
- Java code
public class UserImpl extends SqlMapClientDaoSupport implements IUserDao { public void addUserInfo() { UserModel umodel = new UserModel(); umodel.setId("4"); umodel.setName("lalala"); umodel.setPassword("123456789"); umodel.setState(1); this.getSqlMapClientTemplate().insert(StaticSqlString.getADD_USER(), umodel); //故意再插入一条数据导致错误 this.getSqlMapClientTemplate().insert(StaticSqlString.getADD_USER(), umodel); } public List getUserInfo() { List list = null; list = this.getSqlMapClientTemplate().queryForList(StaticSqlString.getSELECT_ALL_USER()); System.out.println(list.size()); return list; }}