java間資料的同步(StringBuffer,Map)

來源:互聯網
上載者:User

 

java程式中,假如程式中N個方法放在一個java事務中,那麼可能這幾個方法中,每個方法都要用到某個值。並且作為一個事物,這個值應該是同一個值,而不應該會被多線程混亂,這時,方法間傳值時,單個字串,就要用到StringBuffer來傳,傳2個甚至多個字串,就要用到Map(注意,用MAP傳值時,map中的參數,要是Object類型的,所以如果map中要存放2個字串,那麼先將這2個字串分別放到2個StringBuffer裡面,再把這2個StringBuffer放到Map裡)這2個StringBuffer在方法裡面建就行了(局部變數),傳遞的時候傳遞MAP參數。

例:

同步單個字串(mobilecode ):

public int dabao(String tongdaoid, String mainid, StringBuffer sb,Connection conn) throws Exception {     ....    while (rs.next()) {          mobilecode = rs.getString("MOBILECODE");     }     ...     sb.append(mobilecode);   //將while中得到的mobilecode放到了StringBuffer中,下面send中調用時用到的StringBuffer用到的就是這裡的}public int send(String tongdaoid, String mainid, Connection conn)throws Exception {   //調用上面加進來的StringBuffer     int sjfsnumber = 0;     StringBuffer sb = new StringBuffer();     sjfsnumber = dabao(tongdaoid, mainid, sb, conn);   //send調用dabao}

 

同步2個字串(mobilecode ,clientid):

dabao(){      StringBuffer b1=new StringBuffer();      StringBuffer b2=new StringBuffer();      b1.append(mobilecode );          //將mobilecode 放到b1中      b2.append(clientid);                   //將clientid放到b2中      Map p=new HashMap();      p.put("mobilecode1",b1);        //前面雙引號引起來的,是到時候要得到p時候get的名字,要用雙引號引起      p.put("clientid1",b2);      //這樣,就把2個string都放到map裡面去了,下面來調用}send(Map p){             String a=p.get("mobilecode1");         //get後面要加引號             String b=p.get("clientid1");}

擴充:

public void tx_duanxinAdd(Map p) {

p.put("key",value)還可用於資料庫查詢,假如查詢出來ibatis後放到某個<select id裡面,可以用

p.put("欄位名",欄位值)

p.remove("欄位名"),即把這個欄位值清空

p.get("欄位名")

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.