隨筆 - 251  文章 - 504  trackbacks - 0
          <2006年12月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          本博客系個人收集材料及學習記錄之用,各類“大俠”勿擾!

          留言簿(14)

          隨筆分類

          收藏夾

          My Favorite Web Sites

          名Bloger

          非著名Bloger

          搜索

          •  

          積分與排名

          • 積分 - 202354
          • 排名 - 285

          最新評論

          近日在看臺灣人林信良的《Spring技術手冊》,這本書總體上簡單易懂,適合初學者。但是在聲明性JDBC事務管理這節中的例子程序寫的不夠詳細。下來看看并略加修改了下。
          首先,在MySQL中建立一個表myuser。注意要讓MySQL支持事務,要選擇InnoDB類型的表。
          Create table myuser(
          ?id int (11) not null auto_increment primary key,
          name varchar(100) not null default '',
          age int
          )type=InnoDB;

          這里有個實體類user.java:
          package?onlyfun.caterpillar;

          public?class?User?{
          ????
          private?Integer?id;
          ????
          private?String?name;
          ????
          private?Integer?age;

          ????
          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?Integer?getAge()?{
          ????????
          return?age;
          ????}


          ????
          public?void?setAge(Integer?age)?{
          ????????
          this.age?=?age;
          ????}

          }
          為了面向接口編程,我們實現一個接口,讓DAO類實現這個接口。
          package?onlyfun.caterpillar;

          public?interface?IUserDAO?{
          ????
          public?void?insert(User?user);
          ????
          public?User?find(Integer?id);
          }

          具體的DAO類如下:在這個類中我們模擬事務處理。

          package?onlyfun.caterpillar;

          import?java.util.Iterator;
          import?java.util.List;
          import?java.util.Map;

          import?javax.sql.DataSource;

          import?org.springframework.jdbc.core.JdbcTemplate;

          public?class?UserDAO?implements?IUserDAO?{
          ????
          private?JdbcTemplate?jdbcTemplate;
          ????
          ????
          public?void?setDataSource(DataSource?dataSource)?{
          ????????jdbcTemplate?
          =?new?JdbcTemplate(dataSource);
          ????}

          ????
          ????
          public?void?insert(User?user)?{//在這個方法中我們執行了數據插入和數據查詢兩個操作,用來模擬事務操作。我們故意在查詢的語句中把數據庫表寫成smyuser。
          ???????????????????????????????????
          //這樣,在insert方法執行過程中,會由于查詢語句出錯而撤銷之前的插入語句的效果。
          ???????String?name?=?user.getName();
          ???????
          int?age?=?user.getAge().intValue();
          ???????
          ???????jdbcTemplate.update(
          "INSERT?INTO?myuser?(name,age)?"?
          ???????????????
          +?"VALUES('"?+?name?+?"',"?+?age?+?")");
          ???????
          ???????
          ???????List?rows?
          =?jdbcTemplate.queryForList(
          ?????????
          "SELECT?*?FROM?smyuser?WHERE?id=4");
          ???????
          ???????Iterator?it?
          =?rows.iterator();
          ???????
          if(it.hasNext())?{
          ???????????Map?userMap?
          =?(Map)?it.next();
          ???????????Integer?i?
          =?new?Integer(userMap.get("id").toString());
          ???????????String?names?
          =?userMap.get("name").toString();
          ???????????Integer?ages?
          =?new?Integer(userMap.get("age").toString());

          ???????????User?users?
          =?new?User();
          ???????????
          ???????????users.setId(i);
          ???????????users.setName(name);
          ???????????users.setAge(age);
          ???????????System.out.println(
          "names:?"?+?users.getName());
          ??????????
          ???????}


          ?????
          ??
          ????}


          ????
          public?User?find(Integer?id)?{
          ????????
          ????????
          ?????????
          ????????List?rows?
          =?jdbcTemplate.queryForList(
          ??????????
          "SELECT?*?FROM?myuser?WHERE?id="?+?id.intValue());
          ????????
          ????????Iterator?it?
          =?rows.iterator();
          ????????
          if(it.hasNext())?{
          ????????????Map?userMap?
          =?(Map)?it.next();
          ????????????Integer?i?
          =?new?Integer(userMap.get("id").toString());
          ????????????String?name?
          =?userMap.get("name").toString();
          ????????????Integer?age?
          =?new?Integer(userMap.get("age").toString());

          ????????????User?user?
          =?new?User();
          ????????????
          ????????????user.setId(i);
          ????????????user.setName(name);
          ????????????user.setAge(age);
          ????????????
          ????????????
          return?user;
          ????????}


          ????????
          return?null;
          ????}

          }

          然后,在具體測試類中我們這樣:
          package?onlyfun.caterpillar;

          import?org.springframework.context.ApplicationContext;
          import?org.springframework.context.
          ??????????????support.FileSystemXmlApplicationContext;

          public?class?SpringDAODemo?{
          ????
          public?static?void?main(String[]?args)?{
          ????????ApplicationContext?context?
          =?
          ????????????
          new?FileSystemXmlApplicationContext(
          ????????????????????
          "beans-config.xml");
          ????????
          ????????User?user?
          =?new?User();
          ????????
          ????????user.setName(
          "1matthew");
          ????????user.setAge(
          new?Integer(30));
          ????????
          ????????IUserDAO?userDAO?
          =?
          ????????????(IUserDAO)?context.getBean(
          "userDAOProxy");
          ????????
          ????????userDAO.insert(user);
          ????????
          ????????user?
          =?userDAO.find(new?Integer(16));
          ????????
          ????????System.out.println(
          "name:?"?+?user.getName());
          ????}

          }

          最后,我們來看配置文件:在這個文件中我們注入事務管理。
          <?xml?version="1.0"?encoding="UTF-8"?>
          <!DOCTYPE?beans?PUBLIC?"-//SPRING/DTD?BEAN/EN"?
          ??"http://www.springframework.org/dtd/spring-beans.dtd"
          >

          <beans>
          ????
          <bean?id="dataSource"?class="org.apache.commons.dbcp.BasicDataSource"?destroy-method="close">
          ????????
          <property?name="driverClassName">
          ????????????
          <value>com.mysql.jdbc.Driver</value>
          ????????
          </property>
          ????????
          <property?name="url">
          ????????????
          <value>jdbc:mysql://localhost:3306/test</value>
          ????????
          </property>
          ????????
          <property?name="username">
          ????????????
          <value>root</value>
          ????????
          </property>
          ????????
          <property?name="password">
          ????????????
          <value>131421</value>
          ????????
          </property>
          ????
          </bean>

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

          ????
          <bean?id="userDAO"?class="onlyfun.caterpillar.UserDAO">
          ????????
          <property?name="dataSource">
          ????????????
          <ref?bean="dataSource"?/>
          ????????
          </property>
          ????
          </bean>

          ????
          <bean?id="userDAOProxy"?class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
          ????????
          <property?name="proxyInterfaces">
          ????????????
          <list>
          ????????????????
          <value>onlyfun.caterpillar.IUserDAO</value>
          ????????????
          </list>
          ????????
          </property>
          ????????
          <property?name="target">
          ????????????
          <ref?bean="userDAO"?/>
          ????????
          </property>
          ????????
          <property?name="transactionManager">
          ????????????
          <ref?bean="transactionManager"?/>
          ????????
          </property>
          ????????
          <property?name="transactionAttributes">
          ????????????
          <props>
          ????????????????
          <prop?key="insert*">PROPAGATION_REQUIRED</prop>

          ????????????
          </props>
          ????????????
          ????????
          </property>
          ????
          </bean>
          </beans>
          datasource這個bean中我們配置了數據源的相關屬性。在userDAOProxy這個bean中,我們配置了target、transactionManager、transactionAttributes屬性。在transactionAttributes中,我們指定所有insert*方法中操作會注入事務管理。

          程序的運行結果是,由于在insert方法中的查詢語句出錯,會引起之前的insert語句效果被撤銷。你也可以把查詢語句先設置正確,看看效果。
          posted on 2006-12-30 11:17 matthew 閱讀(956) 評論(0)  編輯  收藏 所屬分類: 閱讀筆記
          主站蜘蛛池模板: 新化县| 津市市| 湘潭县| 西乡县| 池州市| 道孚县| 昌平区| 巩留县| 西青区| 石嘴山市| 高雄市| 天柱县| 石首市| 宁国市| 墨玉县| 铜川市| 海宁市| 西乌珠穆沁旗| 奇台县| 安顺市| 南京市| 外汇| 汉川市| 若尔盖县| 林西县| 射洪县| 南京市| 招远市| 红桥区| 柳林县| 静乐县| 汪清县| 二连浩特市| 收藏| 德钦县| 绥阳县| 常德市| 永春县| 星子县| 崇明县| 金川县|