<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

    搜索

    •  

    積分與排名

    • 積分 - 204316
    • 排名 - 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)  編輯  收藏 所屬分類: 閱讀筆記
    主站蜘蛛池模板: 国产AV无码专区亚洲A∨毛片| 久久久久亚洲av无码专区喷水| h片在线播放免费高清| 亚洲国产精品VA在线看黑人| 最近新韩国日本免费观看 | 免费A级毛片无码视频| 精品丝袜国产自在线拍亚洲| 免费A级毛片无码A| 免费无遮挡无码永久视频| 在线a亚洲老鸭窝天堂av高清| 亚洲精品一级无码鲁丝片| 小草在线看片免费人成视久网| 亚洲熟女综合色一区二区三区| 久久久久亚洲AV无码专区网站 | 亚洲国产成人精品无码区花野真一| 久久精品夜色噜噜亚洲A∨| 最近在线2018视频免费观看| 免费大片av手机看片| 亚洲网址在线观看| 亚洲国产香蕉人人爽成AV片久久 | 一级成人a毛片免费播放| 亚洲欧洲精品成人久久曰| 久久亚洲国产欧洲精品一| 免费无码成人AV片在线在线播放| 久草免费福利资源站| 亚洲av永久中文无码精品综合| 亚洲av日韩av无码黑人| 免费a级毛片无码a∨性按摩| 久草视频免费在线观看| 国产一级一毛免费黄片| 爱情岛亚洲论坛在线观看 | 亚洲AV无码久久精品狠狠爱浪潮 | 亚洲Av永久无码精品三区在线| 日本特黄特黄刺激大片免费| 美女内射毛片在线看免费人动物| 国产福利电影一区二区三区,免费久久久久久久精 | 国产精品亚洲综合一区| 成年午夜视频免费观看视频| 24小时日本电影免费看| 中文精品人人永久免费| 麻豆安全免费网址入口|