Spring與Mybatis的整合

首先Spring和Mybatis整合,需要將兩個對象交給spring容器管理:SQLSessionFactoryBean和mapper代理
配置式
1.spring配置文件,相應的mybatis中只需要註冊映射文件就好

<!--加載jdbc屬性文件-->
    <context:property-placeholder location="jdbc.properties"/>
    <!--註冊C3P0數據源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--註冊SQLSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="mybatis.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--獲取mapper代理對象 MapperScannerConfigurer-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
        <property name="basePackage" value="com.bjsxt.mapper"></property>
    </bean>
    <!--註冊Service-->
    <bean id="fundServiceImpl" class="com.bjsxt.service.impl.FundServiceImpl">
        <property name="accountaccountMapper" ref="accountMapper"></property>
        <property name="fundMapper" ref="fundMapper"></property>
    </bean>
    <!--註冊事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--註冊事務通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <!--設置事務屬性-->
        <tx:attributes>
            <!--連接點-->
            <tx:method name="buyFund" isolation="DEFAULT" propagation="REQUIRED" rollback-for="FundException"/>
        </tx:attributes>
    </tx:advice>
    <!--AOP配置-->
    <aop:config>
        <aop:pointcut id="fundPC" expression="execution(* *..service.*.buyFund(..))"></aop:pointcut>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="fundPC"></aop:advisor>
    </aop:config>

註解式
spring配置文件

<!--加載jdbc屬性文件-->
    <context:property-placeholder location="jdbc.properties"/>

    <!--註冊C3P0數據源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--註冊SQLSessionFactoryBean-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="mybatis.xml"></property>
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--獲取mapper代理對象 MapperScannerConfigurer-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"></property>
        <property name="basePackage" value="com.bjsxt.mapper"></property>
    </bean>

    <!--註冊Service-->
    <bean id="fundServiceImpl" class="com.bjsxt.service.impl.FundServiceImpl">
        <property name="accountaccountMapper" ref="accountMapper"></property>
        <property name="fundMapper" ref="fundMapper"></property>
    </bean>

    <!--註冊事務管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--註冊事物註解驅動-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

mapper 接口中要註解不再使用mapper配置文件

public interface AccountMapper {
    @Update("update account set balance=balance-#{balance} where aname=#{aname}")
    //修改
    void updateAccount(Account account);
}

業務層進行註解

@Service("fundServiceImpl")
public class FundServiceImpl implements FundService {
    @Autowired
    private AccountMapper accountaccountMapper;
    @Autowired
    private FundMapper fundMapper;

    public AccountMapper getAccountaccountMapper() {
        return accountaccountMapper;
    }

    public void setAccountaccountMapper(AccountMapper accountaccountMapper) {
        this.accountaccountMapper = accountaccountMapper;
    }

    public FundMapper getFundMapper() {
        return fundMapper;
    }

    public void setFundMapper(FundMapper fundMapper) {
        this.fundMapper = fundMapper;
    }

    @Override
    public void buyFund(Account account, Fund fund) throws Exception {
        accountaccountMapper.updateAccount(account);
        fundMapper.updateFund(fund);
    }
}

OK了

發佈了45 篇原創文章 · 獲贊 9 · 訪問量 2463
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章