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

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

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

    blogjava's web log

    blogjava's web log
    ...

    ibatis 使用筆記


    1.下載ibatis 2.17
    http://cvs.apache.org/dist/ibatis/ibatis.java/builds/iBATIS_DBL-2.1.7.597.zip
    2.打開MYsql建表

    CREATE ? TABLE ?`category`?(
    ??`catid`?
    varchar ( 10 )? NOT ? NULL ,
    ??`name`?
    varchar ( 80 )? default ? NULL ,
    ??`descn`?
    varchar ( 255 )? default ? NULL ,
    ??
    PRIMARY ? KEY ??(`catid`)
    )?ENGINE
    = InnoDB? DEFAULT ?CHARSET = gb2312;


    3.寫上配置文件共包括3個配置文件
    1.database.properties
    2.dao.xml
    3.Category.xml
    4.sql-map-config.xml

    database.properties

    driver = org.gjt.mm.mysql.Driver
    password
    = wujun
    url
    = jdbc:mysql: // localhost: 3306 / JPETSTORE
    username
    = root

    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
    ="com/wj/firstibatis/sql-map-config.xml" />
    ????
    </ transactionManager > ?

    ????
    < dao? interface ="com.wj.firstibatis.Idao"
    ??????implementation
    ="com.wj.firstibatis.Dao" />
    ??
    </ context > ?

    </ daoConfig >


    sql-map-config.xml配置文件

    <? xml?version="1.0"?encoding="UTF-8"? ?> ?

    <! DOCTYPE?sqlMapConfig?PUBLIC?"-//ibatis.apache.org//DTD?SQL?Map?Config?2.0//EN"
    ????"http://ibatis.apache.org/dtd/sql-map-config-2.dtd"
    > ?

    < sqlMapConfig > ?

    ??
    < properties? resource ="com/wj/firstibatis/database.properties" /> ?

    ??
    < transactionManager? type ="JDBC" >
    ????
    < dataSource? type ="SIMPLE" >
    ??????
    < property? value ="${driver}" ?name ="JDBC.Driver" />
    ??????
    < property? value ="${url}" ?name ="JDBC.ConnectionURL" />
    ??????
    < property? value ="${username}" ?name ="JDBC.Username" />
    ??????
    < property? value ="${password}" ?name ="JDBC.Password" />
    ????
    </ dataSource >
    ??
    </ transactionManager > ?

    ??
    < sqlMap? resource ="com/wj/firstibatis/Category.xml" /> ?


    </ sqlMapConfig >


    ?

    Category.xml配置文件

    <? xml?version="1.0"?encoding="UTF-8"? ?>

    <! DOCTYPE?sqlMap?PUBLIC?"-//ibatis.apache.org//DTD?SQL?Map?2.0//EN"
    ????"http://ibatis.apache.org/dtd/sql-map-2.dtd"
    >

    < sqlMap? namespace ="Category" >

    ??
    < typeAlias? alias ="category" ?type ="com.wj.firstibatis.CategoryVO" />

    ??
    < cacheModel? id ="categoryCache" ?type ="LRU" >
    ????
    < flushInterval? hours ="24" />
    ????
    < property? name ="size" ?value ="100" />
    ??
    </ cacheModel >

    ??
    < resultMap? id ="categoryResult" ?class ="category" >
    ????
    < result? property ="categoryId" ?column ="CATID" />
    ????
    < result? property ="name" ?column ="NAME" />
    ????
    < result? property ="description" ?column ="DESCN" />
    ??
    </ resultMap >

    ??
    < select? id ="getCategory" ?resultClass ="category" ?parameterClass ="string" ?cacheModel ="categoryCache" >
    ????SELECT
    ??????CATID?AS?categoryId,
    ??????NAME,
    ??????DESCN?AS?description
    ????FROM?CATEGORY
    ????WHERE?CATID?=?#categoryId#
    ??
    </ select >

    ??
    < select? id ="getCategoryList" ?resultClass ="category" ?cacheModel ="categoryCache" >
    ????SELECT
    ??????CATID?AS?categoryId,
    ??????NAME,
    ??????DESCN?AS?description
    ????FROM?CATEGORY
    ??
    </ select >

    </ sqlMap >

    寫CategoryVO.java
    package?com.wj.firstibatis;

    public?class?CategoryVO?{
    ????
    private?String?categoryId;
    ????
    private?String?name;
    ????
    private?String?description;
    //getter?setter

    接著寫Idao.java接口
    package?com.wj.firstibatis;

    import?java.util.List;

    public?interface?Idao?{
    ????
    public?List?getAll();
    ????
    public?CategoryVO?getById(String?categoryId);
    }


    實現類Dao.java
    package?com.wj.firstibatis;

    import?java.util.List;
    import?com.ibatis.dao.client.DaoManager;
    import?com.ibatis.dao.client.template.SqlMapDaoTemplate;

    public?class?Dao?extends?SqlMapDaoTemplate??implements?Idao{
    ?????????
    ????
    public?Dao(DaoManager?daoManager){
    ????????
    super(daoManager);
    ????}

    ????
    public?List?getAll()
    ????
    {
    ????????
    return?this.queryForList("getCategoryList",?null);
    ????}

    ????
    public?CategoryVO?getById(String?categoryId)
    ????
    {
    ????????
    return?(CategoryVO)?queryForObject("getCategory",?categoryId);
    ????}


    }


    測試。。
    public ? void ?getCategoryAll()
    ????
    {
    ????????
    try
    ????????
    {
    ????????DaoManager?daoManager?
    = ? null ;
    ????????Reader?reader;
    ????????reader?
    = ? new ?FileReader( " C:/Documents?and?Settings/wujun/workspace/ibatisTest/bin/com/wj/firstibatis/dao.xml " );
    ???????
    // reader?=?new?FileReader("com/wj.firstibatis/dao.xml");
    ????????daoManager? = ?DaoManagerBuilder.buildDaoManager(reader);
    ????????daoManager.startTransaction();
    ????????Idao?idao?
    = ?(Idao)?daoManager.getDao(Idao. class );
    ????????List?li
    = idao.getAll();
    ????????
    for ( int ?i = 0 ;i < li.size();i ++ )
    ????????
    {
    ????????????CategoryVO?vo
    = (CategoryVO)li.get(i);
    ????????????System.out.println(
    " categoryId " + vo.getCategoryId());
    ????????????System.out.println(
    " Name: " + vo.getName());
    ????????????System.out.println(
    " Descrition: " + vo.getDescription());
    ????????}

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

    ????????
    catch (Exception?ee)
    ????????
    {
    ???????????log.info(
    " error: " + ee.getMessage());
    ????????}

    ????}


    成功了.....

    posted on 2006-05-23 10:30 record java and net 閱讀(2655) 評論(1)  編輯  收藏 所屬分類: java

    導航

    常用鏈接

    留言簿(44)

    新聞檔案

    2.動態語言

    3.工具箱

    9.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 亚洲精品制服丝袜四区| 亚洲成a∨人片在无码2023 | 亚洲精品美女在线观看播放| 亚洲精品在线免费观看| 亚洲色大成网站www尤物| 羞羞视频免费观看| 99免费观看视频| 免费观看亚洲人成网站| 国产亚洲一区二区三区在线| 亚洲国产亚洲片在线观看播放| 亚洲一区中文字幕在线电影网| 国产精品亚洲一区二区三区 | 国产成人亚洲综合无码精品| 亚洲制服在线观看| 日本特黄特色AAA大片免费| 国产精品网站在线观看免费传媒| 成年女人看片免费视频播放器| 亚洲av日韩片在线观看| 亚洲入口无毒网址你懂的| a级毛片无码免费真人久久| 成年人在线免费看视频| 亚洲尹人九九大色香蕉网站 | 亚洲国产成人久久精品动漫| 亚洲成在人线在线播放无码| 99re在线视频免费观看| 国产亚洲精品激情都市| 国产成人综合久久精品亚洲| 67194熟妇在线永久免费观看| 亚洲av伊人久久综合密臀性色| 亚洲精品无码日韩国产不卡av| 中国xxxxx高清免费看视频| 亚洲人成网亚洲欧洲无码久久 | 国产青草亚洲香蕉精品久久| 四虎影视永久免费观看网址| 亚洲视频免费观看| jizz免费在线观看| 最近中文字幕完整版免费高清| 亚洲精品成人在线| 亚洲精品日韩一区二区小说| 中文字幕无码日韩专区免费| 91香蕉视频免费|