<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.文檔教程

    友情鏈接

    搜索

    最新評論

    主站蜘蛛池模板: 日本最新免费网站| 国产午夜不卡AV免费| 美女网站免费福利视频| 亚洲综合激情六月婷婷在线观看 | 无码久久精品国产亚洲Av影片| 狠狠躁狠狠爱免费视频无码| 亚洲国产精品综合久久网络| 人人爽人人爽人人片av免费| 久久精品国产精品亚洲下载| 国产精品黄页免费高清在线观看 | 久久精品国产亚洲77777| 日本免费久久久久久久网站| 久久国产精品亚洲一区二区| 91热久久免费精品99| 亚洲国产精品张柏芝在线观看| 久久久久久久91精品免费观看 | 国产精品1024在线永久免费 | 国产成人免费片在线视频观看| 美女无遮挡免费视频网站| 亚洲性日韩精品一区二区三区| a色毛片免费视频| 亚洲人成777在线播放| 美女被免费视频网站a国产| 一级特黄a免费大片| 亚洲狠狠久久综合一区77777| 免费观看无遮挡www的小视频| 亚洲精品日韩一区二区小说| 亚洲精品老司机在线观看| 热re99久久6国产精品免费| 亚洲大成色www永久网址| 亚洲高清成人一区二区三区| 一区二区三区福利视频免费观看| 亚洲人成在久久综合网站| jizzjizz亚洲| 120秒男女动态视频免费| 噜噜噜亚洲色成人网站| 亚洲伊人tv综合网色| 国产美女被遭强高潮免费网站| 日本免费大黄在线观看| 美美女高清毛片视频黄的一免费 | 亚洲成色WWW久久网站|