ibatis 使用笔记
2007-07-18 19:30
190 查看
ibatis 使用笔记
1.下载ibatis 2.17http://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());
}
}
成功了.....
相关文章推荐
- ibatis学习笔记(四)>>>>>>>ibatis使用实例
- ibatIS学习笔记---Ibatis的基本使用方法
- ibatis中标签的使用笔记
- Ibatis使用笔记
- iBATIS插件使用笔记
- ibatis学习笔记(四)>>>>>>>ibatis使用实例
- [ACE程序员教程笔记]使用ACE_Acceptor分离连接和事件处理
- Github使用学习笔记(一)
- 【Hibernate学习笔记-5】@Formula注解的使用
- 几种笔记软件的使用感想
- Android笔记之Actionbar使用(二)
- git 在客户端的使用笔记
- Android开发笔记—Http通信的基础使用
- ros笔记:(6)使用rosed编辑ROS中的文件
- iPhone私有API使用学习笔记
- Xcode控件使用笔记三:UIScrollView 查看大图、缩放、分页
- Hibernate表中外键使用(个人笔记)
- Java Web 学习笔记之八:嵌入式web服务器Jetty的基本使用
- iOS开发笔记之NSFileManager的使用
- (转)cocos2d-x学习笔记14:粒子系统1:简介&工具使用