spring學(xué)習(xí)筆記四-spring-mybatis整合

[TOC]

spring-mybatis整合0

PropertyPlaceholderConfigurer

  • location

SqlSessionFactoryBean

作用:用來(lái)創(chuàng)建SqlSessionFacotry,并且掃描xml文件
  • dataSource:數(shù)據(jù)源
  • typeAliasesPackage:掃描某個(gè)包,并將下面的類配置別名
  • configLocation:加載mybatis主配置文件
  • mapperLocations:掃描某個(gè)包下所有mapper.xml

MapperScannerConfigurer

作用:用來(lái)掃描mapper接口
  • basePackage:掃描某個(gè)包下所有mapper接口
  • sqlSessionFactoryBeanName:注入sqlSessionFactory,注意是用value,不是ref的方式注入

spring-mybatis整合步驟

導(dǎo)包:1、mybatis.jar 2、mybatis-spring.jar

1、編寫Mapper接口

//這里的代理對(duì)象由mybatis創(chuàng)建,不能使用@Service
public interface UserDao {
    //使用@Param("username")取別名
    User selectUser(@Param("username")String username,@Param("password")String password);
}

2、編寫Mapper.xml

<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <mapper namespace="com.hemi.dao.UserDao">
    <select id="selectUser" resultType="User">
        select * from user where username=#{username} and password=#{password}
    </select>
  </mapper>

3、編寫service

//不要忘了使用注解創(chuàng)建對(duì)象
@Service
public class UserService {
    //不要忘了使用注解注入對(duì)象
    @Autowired
    private UserDao userDao;
    public User selectUser(String username,String password){
        User user = userDao.selectUser(username, password);
        return user;
    }
}

4、編寫spring-mybatis.xml

<!-- 配置數(shù)據(jù) -->
    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="dataSource">
        <!-- 幫你調(diào)用set方法 setDriverClass() -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/bank"></property>
        <property name="user" value="root"></property>
        <property name="password" value=""></property>
    </bean>         
<!-- 配置SqlSessionFactoryBean:用來(lái)創(chuàng)建SqlSessionFacotry,并且掃描xml文件-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFacotry">
        <!--注入數(shù)據(jù)源-->
        <property name="dataSource" ref="dataSource"></property>
        <!--配置別名,如果在下面的mybatis配置文件中有配置別名,那么這句可以不用-->
        <property name="typeAliasesPackage" value="com.hemi.bean"></property>
        <!--加載mybatis配置文件-->
        <property name="configLocation" value="classpath:mybatis-config.xml"></property>
        <!-- 掃描mapper.xml -->
        <property name="mapperLocations" value="classpath:com/hemi/mapper/*.xml"></property>
    </bean>
<!-- 配置MapperScannerConfigurer:用來(lái)掃描mapper接口 --> 
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 掃描mapper接口 -->
        <property name="basePackage" value="com.hemi.dao"/>
        <!--注入sqlSessionFacotry-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFacotry"></property>
    </bean>

事務(wù)

什么是事務(wù)

事務(wù)邏輯上的一組操作,組成這組操作的各個(gè)邏輯單元,要么一起成功,要么一起失敗。

1、使用Aspectj注解方式解決事務(wù)問題

1、添加約束文檔

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd

2、xml配置事務(wù)管理器

<bean id="transactionManager" 
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<!-- 開啟事務(wù):通過aop的方式,對(duì)方法進(jìn)行事務(wù)的增強(qiáng) -->
<tx:annotation-driven transaction-manager="transactionManager"/>

3、在任意希望進(jìn)行事務(wù)增強(qiáng)的方法上面添加@Transactional注解

@Transactional
public int getMoney(int uid,float money,int typeid){    
    moneyDao.getMoney(uid, money);
    int a=1/0;//這里報(bào)錯(cuò),會(huì)進(jìn)行事務(wù)回滾
    return moneyDao.insertReocrd(new Record(uid, money, typeid));
}

2、通過XML配置AOP解決所有的事務(wù)問題

<!-- 配置AOP -->
<aop:config>
    <!-- 配置切入點(diǎn):指定哪些方法要執(zhí)行事務(wù)增強(qiáng) -->
    <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.hemi.service.*.*(..))"/>
</aop:config>

<!-- 配置增強(qiáng) -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <!-- 細(xì)粒度控制某個(gè)方法上事務(wù)的屬性:例如事務(wù)傳遞,事務(wù)回滾等等 -->
    <!-- REQUIRED:必須使用事務(wù) SUPPORTS:有就用,沒有就非事務(wù)執(zhí)行 -->
    <tx:attributes>
        <!-- no-rollback-for:當(dāng)出現(xiàn)了ArithmeticException異常時(shí),也不回滾已提交的事務(wù) -->
        <tx:method name="*Money" propagation="REQUIRED"  no-rollback-for="ArithmeticException"/>
        <tx:method name="delect*" propagation="REQUIRED"/>
        <tx:method name="update*" propagation="REQUIRED"/>
        <tx:method name="insert*" propagation="REQUIRED"/>      
        <tx:method name="select*" propagation="SUPPORTS"/>
    </tx:attributes>
</tx:advice>
注意

推薦使用第二種方式,可以控制所有的方法是否使用事務(wù)!

事務(wù)補(bǔ)充知識(shí)點(diǎn)

事務(wù)的特性

事務(wù)4.png

1、事務(wù)回滾控制

控制遇到哪些異常才要回滾事務(wù)

事務(wù)1.png
// 在需要增強(qiáng)的方法上面,添加該注解,就可以使用事務(wù)
//noRollbackFor:如果出現(xiàn)了ArithmeticException,忽略
@Transactional(noRollbackFor={ArithmeticException.class})
public int getMoney(int uid, float money, int typeid) {
    moneyDao.getMoney(uid, money);
    int a = 1 / 0;//雖然這里拋出異常,但是上面的操作依然生效,事務(wù)不會(huì)回滾
    return moneyDao.insertReocrd(new Record(uid, money, typeid));
}

2、事務(wù)隔離級(jí)別

事務(wù)隔離概念:解決并發(fā)問題

不同的數(shù)據(jù)庫(kù)支持不同的隔離級(jí)別,一般不用修改

事務(wù)3.png

3、事務(wù)傳遞

事務(wù)傳遞概念:兩個(gè)方法相互嵌套,事務(wù)是否公用,例如:方法A的事務(wù)是否可以傳遞給方法B所使用

事務(wù)傳遞有多種情況,如下:

事務(wù)2.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容