<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 概述 三層思想:高內聚(把相同的功能寫在一起),低耦合(別人的事,不要參和,減少功能交叉). ### 不同層次使用不同包表示 * com.xxx 公司域名倒寫. * com.xxx.dao :dao層 * com.xxx.service :service層 * com.xxx.domain : javabean * com.xxx.utils : 工具類 * com.xxx.web/view : 視圖層 dao層 ~~~ package com.like.dao; import com.like.utils.ConnectionManager; import org.apache.commons.dbutils.QueryRunner; import java.sql.Connection; import java.sql.SQLException; public class AccountDao { //轉賬出去 public void fromAccount(String name, double money) throws SQLException { Connection connection = ConnectionManager.getConnection(); QueryRunner queryRunner = new QueryRunner(); int res = queryRunner.update(connection, "update account set money = money - ? where name = ?", money, name); } //收錢回來 public void toAccount(String name, double money) throws SQLException { Connection connection = ConnectionManager.getConnection(); QueryRunner queryRunner = new QueryRunner(); int res = queryRunner.update(connection, "update account set money = money + ? where name = ?", money, name); } } ~~~ 工具類 ~~~ package com.like.utils; import java.sql.Connection; import java.sql.SQLException; //連接管理類 //主要負責獲取連接,開啟事務,提交事務,回滾事務 public class ConnectionManager { //定義一個ThreadLocal來保存當前當前線程的連接 private static ThreadLocal<Connection> tl = new ThreadLocal<>(); //獲取連接 public static Connection getConnection() throws SQLException { Connection conn = tl.get(); //判斷是否為空,如果是空,說明是service層第一次獲取 if (conn == null) { conn = C3P0Utils.getConnection(); tl.set(conn); } //如果不為空,說明是dao層第二次獲取 return conn; } //開啟事務 public static void start() throws SQLException { ConnectionManager.getConnection().setAutoCommit(false); } //提交事務 public static void commit() throws SQLException { ConnectionManager.getConnection().commit(); } //回滾事務 public static void rollback() throws SQLException { ConnectionManager.getConnection().rollback(); } //關閉連接 public static void close() throws SQLException { ConnectionManager.getConnection().close(); } } ~~~ service層 ~~~ package com.like.service; import com.like.dao.AccountDao; import com.like.utils.ConnectionManager; import java.sql.Connection; import java.sql.SQLException; public class AccountService { public void transfer(String fromName, String toName, double money) { Connection connection = null; try { AccountDao accountDao = new AccountDao(); ConnectionManager.start(); //轉出去 accountDao.fromAccount(fromName, money); //收回來 accountDao.toAccount(toName, money); ConnectionManager.commit(); } catch (SQLException e) { try { ConnectionManager.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { try { ConnectionManager.close(); } catch (SQLException e) { e.printStackTrace(); } } } } ~~~ view層 ~~~ package com.like.view; import com.like.service.AccountService; public class AccountView { public static void main(String[] args) { String fromName = "jack"; String toName = "tom"; double money = 1000; AccountService accountService = new AccountService(); accountService.transfer(fromName, toName, money); } } ~~~
                  <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>

                              哎呀哎呀视频在线观看