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 阅读(2640) 评论(1)  编辑  收藏 所属分类: java


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论