Spring-Mybatis

spring-mybatis整合

PropertyPlaceholderConfigurer

SqlSessionFactoryBean

作用:用來(lái)創(chuàng)建SqlsessionFactory,并且掃描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

創(chuàng)建一個(gè)bean類,在dao層里使用接口,在mapper層里用xml文件寫xml文件即用mybatis開(kāi)發(fā),service層里用spring 方式注入對(duì)象

  1. 編寫Mapper接口
//這里的代理對(duì)象由mybatis創(chuàng)建,不能使用@Service
public interface UserDao {
    //使用@Param("username")取別名
    User selectUser(@Param("username")String username,@Param("password")String password);
}
  1. 編寫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>
  1. 編寫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;
    }
}
  1. 編寫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ù)

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

2.事務(wù)特性:ACID
-原子性:強(qiáng)調(diào)事務(wù)的不可分割
-一致性:事物的執(zhí)行的前后數(shù)據(jù)的完整性保持一致(轉(zhuǎn)賬)
-隔離性:一個(gè)事務(wù)執(zhí)行的過(guò)程中,不應(yīng)該受到其他事務(wù)的干擾
-持久性:事務(wù)一旦結(jié)束,數(shù)據(jù)就持久到數(shù)據(jù)庫(kù)

3.如果不考慮隔離性引發(fā)安全性問(wèn)題
-臟讀:一個(gè)事務(wù)讀到了另一個(gè)事務(wù)未提交的數(shù)據(jù)
-不可重復(fù)讀 :一個(gè)事務(wù)讀到了另一個(gè)事務(wù)已經(jīng)提交的update的數(shù)據(jù)導(dǎo)致多次查詢結(jié)果不一致.
-虛讀(幻讀) :一個(gè)事務(wù)讀到了另一個(gè)事務(wù)已經(jīng)提交的insert的數(shù)據(jù)導(dǎo)致多次查詢結(jié)果不一致.

4.解決讀問(wèn)題:設(shè)置事務(wù)隔離級(jí)別(每種數(shù)據(jù)庫(kù)的隔離級(jí)別不一樣)
-未提交讀 :臟讀,不可重復(fù)讀,虛讀都有可能發(fā)生
-已提交讀:避免臟讀。但是不可重復(fù)讀和虛讀有可能發(fā)生(sqlserver,orcale)
-可重復(fù)讀:避免臟讀和不可重復(fù)讀.但是虛讀有可能發(fā)生.(mysql)
串行化的 :避免以上所有讀問(wèn)題.

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

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>

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

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

  @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));
    }

注:@Transactional只能運(yùn)用在public修飾的方法上

2、通過(guò)XML配置AOP方式解決事務(wù)問(wèn)題

 <!-- 配置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ù)就用,沒(méi)有就創(chuàng)建 SUPPORTS:有就用,沒(méi)有就非事務(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ù)

最后編輯于
?著作權(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)容