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

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

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

    一滴水

    java 2006年1月新開始:)
    隨筆 - 10, 文章 - 2, 評論 - 6, 引用 - 0
    數據加載中……

    Ibtis基本配置--[添加ibatis Dao支持]

    基本配置見上篇
    一.添加配置文件dao.xml
    <?xml?version="1.0"?encoding="UTF-8"?>
    <!DOCTYPE?daoConfig?PUBLIC?"-//ibatis.apache.org//DTD?DAO?Configuration?2.0//EN"?"http://ibatis.apache.org/dtd/dao-2.dtd">
    <daoConfig>
    ????
    <context>
    ????????
    <transactionManager?type="SQLMAP">
    ????????????
    <property?name="SqlMapConfigResource"
    ??????????????????????value
    ="yidishui/daoIbatisImpl/sql/SqlMapConfig.xml"/>
    ????????
    </transactionManager>
    ????????
    <dao?interface="yidishui.dao.PersonDao"??implementation="yidishui.daoIbatisImpl.PersonDaoImpl2"/>
    ????
    </context>
    </daoConfig>
    二.添加DaoConfig類
    package?yidishui;

    import?com.ibatis.dao.client.DaoManager;
    import?com.ibatis.dao.client.DaoManagerBuilder;
    import?com.ibatis.common.resources.Resources;

    import?java.io.Reader;
    import?java.util.Properties;

    public?class?DaoConfig?{
    ????
    private?static?final?String?resource?=?"yidishui/dao.xml";
    ????
    private?static?final?DaoManager?daoManager;

    ????
    static?{
    ????????daoManager?
    =?newDaoManager(null);
    ????}


    ????
    public?static?DaoManager?getDaoManager()?{
    ????????
    return?daoManager;
    ????}


    ????
    public?static?DaoManager?newDaoManager(Properties?props)?{
    ????????
    try?{
    ????????????Reader?reader?
    =?Resources.getResourceAsReader(resource);
    ????????????
    return?DaoManagerBuilder.buildDaoManager(reader,?props);
    ????????}
    ?catch?(Exception?e)?{
    ????????????
    throw?new?RuntimeException("Could?not?initialize?DaoConfig.??Cause:?"?+?e,?e);
    ????????}

    ????}

    }
    三.修改Dao實現,添加類PersonDaoImpl2
    package?yidishui.daoIbatisImpl;

    import?yidishui.dao.PersonDao;
    import?yidishui.domain.Person;

    import?java.util.List;
    import?java.sql.SQLException;

    import?com.ibatis.dao.client.template.SqlMapDaoTemplate;
    import?com.ibatis.dao.client.DaoManager;

    public?class?PersonDaoImpl2?extends?SqlMapDaoTemplate?implements?PersonDao?{
    ????
    public?PersonDaoImpl2(DaoManager?daoManager)?{
    ????????
    super(daoManager);
    ????}


    ????
    public?void?insertPerson(Person?person)?throws?SQLException?{
    ????????insert(
    "insertPerson",?person);
    ????}


    ????
    public?void?updatePerson(Person?person)?throws?SQLException?{
    ????????update(
    "updatePerson",?person);
    ????}


    ????
    public?Person?getPersonById(int?personId)?throws?SQLException?{
    ????????
    return?(Person)?queryForObject("getPersonById",?personId);
    ????}


    ????
    public?void?deletePerson(int?personId)?throws?SQLException?{
    ????????delete(
    "deletePerson",?personId);
    ????}


    ????
    public?List?allPersonList()?throws?SQLException?{
    ????????
    return?queryForList("allPersonList",?null);
    ????}

    }
    四,測試PersonDaoImpl2Test(正確行驗證)
    package?yidishui.daoIbatisImpl;
    import?junit.framework.*;
    import?yidishui.daoIbatisImpl.PersonDaoImpl;
    import?yidishui.domain.Person;
    import?yidishui.dao.PersonDao;
    import?yidishui.DaoConfig;

    import?java.sql.SQLException;
    import?java.util.List;

    import?com.ibatis.dao.client.DaoManager;

    public?class?PersonDaoImpl2Test?extends?TestCase?{

    ????DaoManager?daoManager;

    ????
    protected?void?setUp()?throws?Exception?{
    ????????daoManager?
    =?DaoConfig.getDaoManager();
    ????}


    ????
    public?void?testInsertPerson()?{
    ????????PersonDao?personDao?
    =?(PersonDao)?daoManager.getDao(PersonDao.class);

    ????????Person?person?
    =?new?Person();
    ????????person.setPersonName(
    "yidishui");
    ????????person.setPersonEmail(
    "yidishui1570@gamil.com");
    ????????person.setPersonAge(
    100);

    ????????
    try?{
    ????????????personDao.insertPerson(person);
    ????????}
    ?catch?(SQLException?e)?{
    ????????????e.printStackTrace();??
    //To?change?body?of?catch?statement?use?File?|?Settings?|?File?Templates.
    ????????}


    ????}


    ????
    public?void?testUpdatePerson()?throws?Exception?{
    ????????PersonDao?personDao?
    =?(PersonDao)?daoManager.getDao(PersonDao.class);
    ????????Person?person?
    =?new?Person();
    ????????person.setPersonId(
    1);
    ????????person.setPersonName(
    "caotao");
    ????????person.setPersonEmail(
    "caotao1570@gamil.com");
    ????????person.setPersonAge(
    1100);
    ????????personDao.updatePerson(person);

    ????}


    ????
    public?void?testDeletePerson()?throws?Exception?{
    ????????PersonDao?personDao?
    =?(PersonDao)?daoManager.getDao(PersonDao.class);
    ????????personDao.deletePerson(
    1);
    ????}


    ????
    public?void?testAllPersonList()?throws?Exception?{
    ????????PersonDao?personDao?
    =?(PersonDao)?daoManager.getDao(PersonDao.class);
    ????????List?list?
    =?personDao.allPersonList();
    ????????assertTrue(
    "list?size?is?0",?list.size()?>?0);
    ????????
    for?(int?i?=?0;?i?<?list.size();?i++)?{
    ????????????Person?person?
    =?(Person)?list.get(i);
    ????????????System.out.println(person.getPersonName());
    ????????}

    ????}


    }
    五.運行測試
    ?測試成功ok完成

    posted on 2006-07-19 17:06 一滴水 閱讀(2567) 評論(0)  編輯  收藏 所屬分類: ORMjava開發總結

    主站蜘蛛池模板: 亚洲第一区视频在线观看| 亚洲精品无码日韩国产不卡?V| 亚洲AV永久青草无码精品| yellow视频免费看| 亚洲精品尤物yw在线影院| 五月天婷婷免费视频| 国产精品亚洲玖玖玖在线观看| 国产精品久久久久久亚洲小说| 国产成人免费一区二区三区| 亚洲avav天堂av在线网毛片| 国产一区二区三区免费看| 国产av无码专区亚洲av毛片搜| 亚洲?V乱码久久精品蜜桃| 一个人晚上在线观看的免费视频| 亚洲精品中文字幕乱码三区| 亚洲一区免费观看| 亚洲另类视频在线观看| 国产乱子影视频上线免费观看| 免费无码国产V片在线观看| 久久影院亚洲一区| 一级毛片免费不卡在线| 亚洲一级毛片免观看| 四虎影院在线免费播放| 一个人免费观看日本www视频| 亚洲精品午夜国产VA久久成人| 30岁的女人韩剧免费观看| 亚洲国产激情在线一区| www国产亚洲精品久久久日本| 中文字幕手机在线免费看电影| 亚洲综合激情另类小说区| 国产麻豆免费观看91| 日本免费A级毛一片| 亚洲av无码一区二区三区天堂古代| 日韩视频免费在线| 99久久婷婷免费国产综合精品| 亚洲人成免费电影| 久久久久亚洲爆乳少妇无| 成人免费大片免费观看网站| 猫咪免费人成在线网站| 亚洲自偷自拍另类12p| 国产在线ts人妖免费视频|