<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    留言簿(14)

    隨筆分類

    收藏夾

    My Favorite Web Sites

    名Bloger

    非著名Bloger

    搜索

    •  

    積分與排名

    • 積分 - 204340
    • 排名 - 283

    最新評論

    近日在看臺灣人林信良的《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 閱讀(965) 評論(0)  編輯  收藏 所屬分類: 閱讀筆記
    主站蜘蛛池模板: 久久亚洲中文字幕无码| 99人中文字幕亚洲区| 亚洲中文字幕无码爆乳| 亚欧在线精品免费观看一区| 国产aⅴ无码专区亚洲av| 成人片黄网站色大片免费观看cn| 久久久久噜噜噜亚洲熟女综合| 视频一区二区三区免费观看| 免费一级毛片在级播放| 老司机午夜免费视频| 一本色道久久88亚洲综合| 一级看片免费视频| 色欲aⅴ亚洲情无码AV蜜桃| 日韩中文无码有码免费视频 | 国产婷婷高清在线观看免费| 亚洲欧好州第一的日产suv| 日韩免费高清一级毛片在线| 国产午夜亚洲精品不卡| 自拍偷自拍亚洲精品第1页| 另类免费视频一区二区在线观看| 老司机亚洲精品影院| 精品熟女少妇AV免费观看| 国产精品亚洲色图| 亚洲色婷婷一区二区三区| 8888四色奇米在线观看免费看| 亚洲欧洲国产成人精品| 在线看片无码永久免费aⅴ| 一本到卡二卡三卡免费高| 久久亚洲精品AB无码播放| 999在线视频精品免费播放观看| 亚洲乱妇老熟女爽到高潮的片| 亚洲无线一二三四区手机| 91精品免费高清在线| 老牛精品亚洲成av人片| 国产亚洲精品国产| 最近的免费中文字幕视频| xxxxx做受大片视频免费| 亚洲国产一区二区三区青草影视 | 亚洲精品二三区伊人久久| 四虎永久免费地址在线网站| 亚洲熟妇av午夜无码不卡|