<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 配置 ~~~ <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!--掃描包--> <context:component-scan base-package="com.like"/> <!--配置JDBCTemplate--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置數據源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.10.10:3306/jdbc"/> <property name="username" value="homestead"/> <property name="password" value="secret"/> </bean> <!-- spring中基于XML的聲明式事務控制配置步驟 1.配置事務管理器 2.配置事務的通知 此時我們需要導入事務的約束,tx名稱空間和約束,同時也需要AOP的 使用tx:advice標簽配置事務通知 屬性: id:給事務起唯一標識 transaction-manager:給事務通知聽一個事務管理器飲引用 3.配置AOP中的通用切入點表達式 4.建立事務通知和切入點表達式的對應關系 5.配置事務的屬性 在事務tx:advice標簽的內部 --> <!-- 1.配置事務管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!--配置事務的通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--transfer表示業務實現層的一個方,它需要有一些屬性 isolation:用于指定事務的隔離級別.默認值是default,表示使用數據庫的默認隔離級別 propagation:用于指定事務的傳播行為,默認值是REQUIRED,表示一定會有事務,增刪改的選擇.查詢方法可以選擇SUPPORTS read-only:用于指定事務是否可讀.只有查詢方法才能設置為true.默認值是false,表示讀寫. timeout:用于指定事務的超時時間,默認值是-1,表示永不超時.如果指定了數值,以秒為單位. rollback-for:用于指定一個異常,當產生該異常時,事務回滾,產生其他異常時,事務不回滾,沒有默認值,表示任何異常都回滾. no-rollback-for:用于指定一個異常,當產生該異常時,事務不回滾,產生其他異常時,事務回滾.沒有默認值,表示任何異常都回滾. --> <tx:method name="transfer" propagation="REQUIRED" read-only="false"/> <!--針對以find開頭的方法使用以下配置--> <!-- <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>--> </tx:attributes> </tx:advice> <!--配置AOP--> <aop:config> <aop:pointcut id="pt1" expression="execution(* com.like.service.impl.*ServiceImpl.*(..))"/> <!--建立事務通知和切入點表達式的對應關系--> <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/> </aop:config> </beans> ~~~ ## 代碼 ~~~ package com.like.domain; import java.io.Serializable; public class Account implements Serializable { private Integer id; private String name; private Float money; @Override public String toString() { return "Account{" + "id=" + id + ", name='" + name + '\'' + ", money=" + money + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Float getMoney() { return money; } public void setMoney(Float money) { this.money = money; } } ~~~ ~~~ package com.like.dao; import com.like.domain.Account; public interface IAccountDao { Account findAccountByName(String name); void updateAccount(Account account); } ~~~ ~~~ package com.like.dao.impl; import com.like.dao.IAccountDao; import com.like.domain.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; import java.util.List; @Repository public class AccountDaoImpl implements IAccountDao { @Autowired private JdbcTemplate jt; public Account findAccountByName(String name) { List<Account> accounts = jt.query("select * from account where name = ?", new BeanPropertyRowMapper<Account>(Account.class), name); if (accounts.isEmpty()) { return null; } if (accounts.size() > 1) { throw new RuntimeException("結果集不唯一"); } return accounts.get(0); } public void updateAccount(Account account) { jt.update("update account set money = ? where name = ?", account.getMoney(), account.getName()); } } ~~~ ~~~ package com.like.service; public interface IAccountService { void transfer(String sourceName,String targetName,Float money); } ~~~ ~~~ package com.like.service.impl; import com.like.dao.IAccountDao; import com.like.domain.Account; import com.like.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class AccountServiceImpl implements IAccountService { @Autowired private IAccountDao accountDao; public void transfer(String sourceName, String targetName, Float money) { Account source = accountDao.findAccountByName(sourceName); Account target = accountDao.findAccountByName(targetName); source.setMoney(source.getMoney() - money); target.setMoney(target.getMoney() + money); accountDao.updateAccount(source); int a = 1/0; accountDao.updateAccount(target); } } ~~~ ## 測試 ~~~ ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); IAccountService accountServiceImpl = (IAccountService)context.getBean("accountServiceImpl"); accountServiceImpl.transfer("milan","jack",500f); ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看