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

          本博客系個(gè)人收集材料及學(xué)習(xí)記錄之用,各類“大俠”勿擾!

          留言簿(14)

          隨筆分類

          收藏夾

          My Favorite Web Sites

          名Bloger

          非著名Bloger

          搜索

          •  

          積分與排名

          • 積分 - 204220
          • 排名 - 283

          最新評(píng)論

          近日在看臺(tái)灣人林信良的《Spring技術(shù)手冊(cè)》,這本書總體上簡(jiǎn)單易懂,適合初學(xué)者。但是在聲明性JDBC事務(wù)管理這節(jié)中的例子程序?qū)懙牟粔蛟敿?xì)。下來看看并略加修改了下。
          首先,在MySQL中建立一個(gè)表myuser。注意要讓MySQL支持事務(wù),要選擇InnoDB類型的表。
          Create table myuser(
          ?id int (11) not null auto_increment primary key,
          name varchar(100) not null default '',
          age int
          )type=InnoDB;

          這里有個(gè)實(shí)體類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;
          ????}

          }
          為了面向接口編程,我們實(shí)現(xiàn)一個(gè)接口,讓DAO類實(shí)現(xiàn)這個(gè)接口。
          package?onlyfun.caterpillar;

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

          具體的DAO類如下:在這個(gè)類中我們模擬事務(wù)處理。

          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)?{//在這個(gè)方法中我們執(zhí)行了數(shù)據(jù)插入和數(shù)據(jù)查詢兩個(gè)操作,用來模擬事務(wù)操作。我們故意在查詢的語句中把數(shù)據(jù)庫表寫成smyuser。
          ???????????????????????????????????
          //這樣,在insert方法執(zhí)行過程中,會(huì)由于查詢語句出錯(cuò)而撤銷之前的插入語句的效果。
          ???????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;
          ????}

          }

          然后,在具體測(cè)試類中我們這樣:
          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());
          ????}

          }

          最后,我們來看配置文件:在這個(gè)文件中我們注入事務(wù)管理。
          <?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這個(gè)bean中我們配置了數(shù)據(jù)源的相關(guān)屬性。在userDAOProxy這個(gè)bean中,我們配置了target、transactionManager、transactionAttributes屬性。在transactionAttributes中,我們指定所有insert*方法中操作會(huì)注入事務(wù)管理。

          程序的運(yùn)行結(jié)果是,由于在insert方法中的查詢語句出錯(cuò),會(huì)引起之前的insert語句效果被撤銷。你也可以把查詢語句先設(shè)置正確,看看效果。
          posted on 2006-12-30 11:17 matthew 閱讀(964) 評(píng)論(0)  編輯  收藏 所屬分類: 閱讀筆記
          主站蜘蛛池模板: 周宁县| 普格县| 巫溪县| 年辖:市辖区| 鱼台县| 宁阳县| 北安市| 乌审旗| 舟曲县| 宝丰县| 彰化市| 乐平市| 金湖县| 江永县| 皮山县| 电白县| 林西县| 宽城| 吉木乃县| 浦东新区| 义马市| 揭东县| 襄樊市| 大田县| 余江县| 福州市| 定州市| 浪卡子县| 麻江县| 洪湖市| 乌海市| 西乡县| 文昌市| 浙江省| 广水市| 永州市| 清涧县| 墨玉县| 改则县| 鞍山市| 仙桃市|