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個(gè)配置文件
          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);
          }


          實(shí)現(xiàn)類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 閱讀(2656) 評論(1)  編輯  收藏 所屬分類: java

          導(dǎo)航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動(dòng)態(tài)語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 襄垣县| 濉溪县| 西林县| 衡山县| 黑龙江省| 兴义市| 布尔津县| 满城县| 平和县| 安陆市| 海丰县| 克什克腾旗| 新营市| 江达县| 伊宁市| 扬中市| 麟游县| 义乌市| 宝鸡市| 廉江市| 丰城市| 宜兴市| 孟村| 平顶山市| 马公市| 莱州市| 沁源县| 安远县| 深水埗区| 义乌市| 丰顺县| 岫岩| 舞阳县| 玉龙| 昌都县| 临桂县| 当阳市| 江永县| 株洲县| 郑州市| 嘉定区|