spring ibatis 事务

1)applicationContext-resources.xml 配数据源:

<beans>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
        <property name="url" value="jdbc:oracle:thin:@192.168.0.1:1521:AGMSC"/>
        <property name="username" value="test"/>
        <property name="password" value="test"/>
        <property name="maxActive" value="100"/>
        <property name="maxIdle" value="30"/>
        <property name="maxWait" value="1000"/>
        <property name="defaultAutoCommit" value="true"/>
        <property name="removeAbandoned" value="true"/>
        <property name="removeAbandonedTimeout" value="60"/>
    </bean>
</beans>

(2)applicationContext-ibatis.xml  符合Ibatis的DAO配置:

<beans>
    <!-- Transaction manager for a single JDBC DataSource -->
    <bean id="transactionManager" 
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- SqlMap setup for iBATIS Database Layer -->
    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
        <property name="configLocation">
            <value>classpath:/com/aigomusic/dao/ibatis/sql-map-config.xml</value>
        </property>
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- Generic Dao - can be used when doing standard CRUD -->
    <bean id="dao" class="com.aigomusic.dao.ibatis.BaseDaoIbatis">
        <property name="dataSource" ref="dataSource"/>
        <property name="sqlMapClient" ref="sqlMapClient"/> 
    </bean> 
    
    <!-- ammRingtoneDao: iBatis implementation --> 
    <bean id="ammRingtoneDao" class="com.aigomusic.dao.ibatis.AmmRingtoneDaoIbatis"> 
        <property name="dataSource" ref="dataSource"/>
        <property name="sqlMapClient" ref="sqlMapClient"/> 
    </bean> 
    
    <!-- ammImageDao: iBatis implementation --> 
    <bean id="ammImageDao" class="com.aigomusic.dao.ibatis.AmmImageDaoIbatis"> 
        <property name="dataSource" ref="dataSource"/>
        <property name="sqlMapClient" ref="sqlMapClient"/> 
    </bean> 
</beans>

(3)AmmImageDaoIbatis.java  符合Ibatis的DAO实现代码:

public class AmmImageDaoIbatis extends BaseDaoIbatis implements AmmImageDao {
 /**
  * 测试成功
  */
 public AmmImage getAmmImage(String fringmodelid) {
  return (AmmImage) getSqlMapClientTemplate().queryForObject(
    "getAmmImage", fringmodelid);
 }

 /**
  * 测试成功
  */
 public AmmImagePage getAmmImages(AmmImagePage ammImagePage) {
  ammImagePage.handlePage(Integer
    .parseInt((String) getSqlMapClientTemplate().queryForObject(
      "getAmmImagesCount", ammImagePage)),
    ammImagePage.getPageNo(), ammImagePage.getPageSize());
  ammImagePage.setItemList(getSqlMapClientTemplate().queryForList(
    "getAmmImages", ammImagePage));
  return ammImagePage;
 }

 
 /**
  * 测试成功
  */ 
 public AmmImagePage getDownloadRate(AmmImagePage ammImagePage) {
  if(ammImagePage==null){ammImagePage=new AmmImagePage();}
  ammImagePage.handlePage(ammImagePage.getItemCount(),ammImagePage.getPageNo(), ammImagePage.getPageSize());
  ammImagePage.setItemList(getSqlMapClientTemplate().queryForList(
    "getAmmImages_ByDownloadRate", ammImagePage));
  return ammImagePage;
 }

 public AmmImagePage getComsumeRate() {
  AmmImagePage ammImagePage = new AmmImagePage();
  ammImagePage.setItemList(getSqlMapClientTemplate().queryForList(
    "getMobileNumbers_ByComsumeRate", null));
  return ammImagePage;
 }

}

(4)applicationContext-service.xml  业务接口配置文件

<beans>

 <!-- Generic manager that can be used to do basic CRUD operations on any objects -->
 <bean id="manager" class="com.aigomusic.service.impl.BaseManager">
  <property name="dao" ref="dao" />
 </bean>

 <bean id="ammRingtoneManager"
  class="com.aigomusic.service.impl.AmmRingtoneManagerImpl">
  <property name="ammRingtoneDao" ref="ammRingtoneDao" />
 </bean>

 <bean id="ammImageManager"
  class="com.aigomusic.service.impl.AmmImageManagerImpl">
  <property name="ammImageDao" ref="ammImageDao" />
 </bean>

 <!-- Add new Managers here -->
</beans>

(5)AmmImageManagerImpl.java  MANAGER实现代码:

public class AmmImageManagerImpl extends BaseManager implements
  AmmImageManager {
 private AmmImageDao ammImageDao;

 public void setAmmImageDao(AmmImageDao ammImageDao) {
  this.ammImageDao = ammImageDao;
 }

 public AmmImage getAmmImage(String fringmodelid) {
  return (AmmImage)ammImageDao.getAmmImage(fringmodelid);
 }

 public AmmImagePage getAmmImages(AmmImagePage ammImagePage) {
  return (AmmImagePage)ammImageDao.getAmmImages(ammImagePage);
 }

 public AmmImagePage getDownloadRate(AmmImagePage ammImagePage) {
  return (AmmImagePage)ammImageDao.getDownloadRate(ammImagePage);
 }

 public AmmImagePage getComsumeRate() {
  return (AmmImagePage)ammImageDao.getComsumeRate();
 }

}

你可能感兴趣的:(DAO,spring,oracle,bean,ibatis)