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类

package datamigrate;
import datamigrate.TCommGuestbook0;
import datamigrate.TCommGuestbook0Example;
import java.util.List;

public interface TCommGuestbook0DAO {
	int countByExample(TCommGuestbook0Example example);
}

 

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的体验

1.配置dao.xml –The Configuration File (http://ibatis.apache.org/dtd/dao-2.dtd)

<!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 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&amp;characterEncoding=gbk&amp;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)

 
 

2.使用方式:

iBATIS DAO framework接口类非常简洁:



 
 使用起来大致如下:

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结合

     现在使用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实现了

 

 

P.s

1.在property文件配置DB连接时,通常

jdbc.url=jdbc:mysql://1.2.3.4:3306/bb?useUnicode=true;characterEncoding=gbk;zeroDateTimeBehavior=convertToNull

 

但是写到xml里面;会报错,需要转译成& a m p ; 使用

 

2.zeroDateTimeBehavior=convertToNull来自一个mysql版本升级的bug

Datetimes with all-zero components (0000-00-00 ...) — These values can not be represented reliably in Java. Connector/J 3.0.x always converted them to NULL when being read from a ResultSet.

Connector/J 3.1 throws an exception by default when these values are encountered as this is the most correct behavior according to the JDBC and SQL standards. This behavior can be modified using the zeroDateTimeBehavior configuration property. The allowable values are:

exception (the default), which throws an SQLException with an SQLState of S1009.

convertToNull, which returns NULL instead of the date.

round, which rounds the date to the nearest closest value which is 0001-01-01.

Starting with Connector/J 3.1.7, ResultSet.getString() can be decoupled from this behavior via noDatetimeStringSync=true (the default value is false) so that you can retrieve the unaltered all-zero value as a String. It should be noted that this also precludes using any time zone conversions, therefore the driver will not allow you to enable noDatetimeStringSync and useTimezone at the same time.

<!--EndFragment--><!--EndFragment-->

 

<!--EndFragment-->
  • 大小: 67.9 KB
  • 大小: 49 KB
基于SSM框架的智能家政保洁预约系统,是一个旨在提高家政保洁服务预约效率和管理水平的平台。该系统通过集成现代信息技术,为家政公司、家政服务人员和消费者提供了一个便捷的在线预约和管理系统。 系统的主要功能包括: 1. **用户管理**:允许消费者注册、登录,并管理他们的个人资料和预约历史。 2. **家政人员管理**:家政服务人员可以注册并更新自己的个人信息、服务类别和服务时间。 3. **服务预约**:消费者可以浏览不同的家政服务选项,选择合适的服务人员,并在线预约服务。 4. **订单管理**:系统支持订单的创建、跟踪和管理,包括订单的确认、完成和评价。 5. **评价系统**:消费者可以在家政服务完成后对服务进行评价,帮助提高服务质量和透明度。 6. **后台管理**:管理员可以管理用户、家政人员信息、服务类别、预约订单以及处理用户反馈。 系统采用Java语言开发,使用MySQL数据库进行数据存储,通过B/S架构实现用户与服务的在线交互。系统设计考虑了不同用户角色的需求,包括管理员、家政服务人员和普通用户,每个角色都有相应的权限和功能。此外,系统还采用了软件组件化、精化体系结构、分离逻辑和数据等方法,以便于未来的系统升级和维护。 智能家政保洁预约系统通过提供一个集中的平台,不仅方便了消费者的预约和管理,也为家政服务人员提供了一个展示和推广自己服务的机会。同时,系统的后台管理功能为家政公司提供了强大的数据支持和决策辅助,有助于提高服务质量和管理效率。该系统的设计与实现,标志着家政保洁服务向现代化和网络化的转型,为管理决策和控制提供保障,是行业发展中的重要里程碑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值