iBATIS DAO framework的应用一、
准备工作
1.到这里可以下载http://apache.etoak.com/ibatis/binaries/ibatis.java/iBATIS_DAO-2.2.0.638.zip
2.首先写DAO层接口及实现类:具体类方法省略,主要是实现类需要继承
com.ibatis.dao.client.template.SqlMapDaoTemplate类
Java代码
- package datamigrate;
- import datamigrate.TCommGuestbook0;
- import datamigrate.TCommGuestbook0Example;
- import java.util.List;
- public interface TCommGuestbook0DAO {
- int countByExample(TCommGuestbook0Example example);
- }
Java代码
- package datamigrate;
- import com.ibatis.dao.client.DaoManager;
- import com.ibatis.dao.client.template.SqlMapDaoTemplate;
- import datamigrate.TCommGuestbook0;
- import datamigrate.TCommGuestbook0Example;
- import java.util.List;
- public class TCommGuestbook0DAOImpl extends SqlMapDaoTemplate implements TCommGuestbook0DAO {
- public TCommGuestbook0DAOImpl(DaoManager daoManager) {
- super(daoManager);
- }
- public int countByExample(TCommGuestbook0Example example) {
- Integer count = (Integer) queryForObject(
- "t_comm_guestbook_0.ibatorgenerated_countByExample", example);
- return count;
- }
- }
iBATIS DAO framework应用二、
开始iBATIS DAO framework的体验
1.配置dao.xml –The Configuration File (http://ibatis.apache.org/dtd/dao-2.dtd)
Xml代码
- ﹤!DOCTYPE daoConfig
- PUBLIC "-//ibatis.apache.org//DTD DAO Configuration 2.0//EN"
- "http://ibatis.apache.org/dtd/dao-2.dtd"﹥
- ﹤daoConfig﹥
- ﹤!-- Example SQL Maps DAO Configuration --﹥
- ﹤context﹥
- ﹤transactionManager type="SQLMAP"﹥
- ﹤property name="SqlMapConfigResource" value="sqlmap-config.xml" /﹥
- ﹤/transactionManager﹥
- ﹤dao interface="datamigrate.TCommGuestbook0DAO" implementation="datamigrate.TCommGuestbook0DAOImpl" /﹥
- ﹤dao interface="datamigrate.TCommMsgbrdDAO" implementation="datamigrate.TCommMsgbrdDAOImpl" /﹥
- ﹤/context﹥
- ﹤/daoConfig﹥
非常简单的配置:
1.申明了事物和JDBC连接使用sqlmap-config.xml(Ibatis标准的配置文件,如下)中的属性,
2.申明了需要管理的dao文件接口和实现。
sqlmap-config.xml
Xml代码
- ﹤?xml version="1.0" encoding="GBK"?﹥
- ﹤!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
- "http://www.ibatis.com/dtd/sql-map-config-2.dtd"﹥
- ﹤sqlMapConfig﹥
- ﹤settings cacheModelsEnabled="true"
- useStatementNamespaces="true"/﹥
- ﹤transactionManager type="JDBC"﹥
- ﹤dataSource type="SIMPLE"﹥
- ﹤property name="JDBC.Driver" value="com.mysql.jdbc.Driver" /﹥
- ﹤property name="JDBC.ConnectionURL" value="jdbc:mysql:
- //134.1:3306/b23?useUnicode=true&
- characterEncoding=gbk&
- zeroDateTimeBehavior=convertToNull" /﹥
- ﹤property name="JDBC.Username" value="weisong" /﹥
- ﹤property name="JDBC.Password" value="weisong" /﹥
- ﹤/dataSource﹥
- ﹤/transactionManager﹥
- ﹤sqlMap resource="datamigrate/t_comm_guestbook_0_SqlMap.xml" /﹥
- ﹤sqlMap resource="datamigrate/t_comm_msgbrd_SqlMap.xml" /﹥
- ﹤/sqlMapConfig﹥
iBATIS DAO framework具体支持一下集中JDBC连接和事物管理方式(当然通常会用sqlmap)
图1
2.使用方式:
iBATIS DAO framework接口类非常简洁:
图2
使用起来大致如下:
Java代码
- package datamigrate;
- import java.io.IOException;
- import java.io.Reader;
- import java.util.List;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.dao.client.DaoManager;
- import com.ibatis.dao.client.DaoManagerBuilder;
- public class MainClass {
- public static void main(String[] args) throws IOException {
- Reader reader = Resources.getResourceAsReader("dao.xml");
- DaoManager daoManager = DaoManagerBuilder.buildDaoManager(reader);
- try {
- TCommMsgbrdDAO dao1 = (TCommMsgbrdDAO) daoManager
- .getDao(TCommMsgbrdDAO.class);
- daoManager.startTransaction();
- List﹤TCommMsgbrdWithBLOBs﹥ oldDB = dao1
- .selectByExampleWithBLOBs(new TCommMsgbrdExample());
- daoManager.commitTransaction();
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- daoManager.endTransaction();
- }
- }
- }
获取到DaoManager 之后磊代码就是了,根本不需要什么web环境。
iBATIS DAO framework三、
跟iBATIS结合
现在使用iBATIS的时候,通常会选择“Ibator”工具来自动生成PO对象和sql-map.xml,在﹤daoGenerator﹥元素里面可以配置一个属性type="IBATIS",具体意思就是“The generated DAO objects will conform to the (now deprecated) iBATIS DAO framework.” 就会自动生成支持iBATIS DAO framework的DAO实现了。
iBATIS DAO framework的体验就向你介绍到这里,希望对你有所帮助。
【编辑推荐】