这一章节是本系列文章最后一篇。经历过前三篇文章的洗礼,相信你对 iBATIS SQL Maps 有比较详细的认识了吧?
来看看新需求:
张三后来做生意,自己经营得很好,打算再买辆车跑运输。对于第二次买车,车辆管理系统的 PEOPLE 表原本已经记录了他的基本信息,遂不对 PEOPLE 表操作。只向 AUTO_INFO表 insert 一条车辆记录即可。
新需求所用到的技术要点都在《iBATIS SQL Maps(二)》和《iBATIS SQL Maps(三)》中出现过了。请看映射文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap>
<insert id="insertAutoInfo" parameterClass="bo.AutoInfo">
<![CDATA[ insert into auto_info (license_plate, owner_no) values (#licensePlate#, #ownerNo.id#) ]]> </insert> <resultMap id="get-autoInfo-result" class="bo.AutoInfo">
<result property="id" column="auto_id"/> <result property="licensePlate" column="license_plate"/> </resultMap> <resultMap id="get-people-result" class="bo.People">
<result property="id" column="owner_id"/> <result property="name" column="name"/> <result property="address" column="address"/> <result property="autoInfoList" column="owner_id" select="getAutoInfo"/> </resultMap> <select id="getPeople" resultMap="get-people-result" parameterClass="bo.People">
<![CDATA[ select * from people ]]> <dynamic prepend="where"> <isNotNull property="id"> <![CDATA[ owner_id=#id# ]]> </isNotNull> </dynamic> </select> <select id="getAutoInfo" resultMap="get-autoInfo-result" parameterClass="int"> <![CDATA[ select * from auto_info where owner_no=#id# ]]> </select> </sqlMap> |
简单地组合一下以前出现过的映射文件,就能满足新需求。相应程序代码如下:
package test;
import java.io.Reader;
import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*; import bo.*;
public class AutoMag {
private Reader reader;
private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public void insertPeople() throws Exception{ try{ reader = Resources.getResourceAsReader(resource); sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader); sqlMap.startTransaction(); People people=new People(); people.setId(new Integer("1")); people=(People)sqlMap.queryForObject("getPeople",people); AutoInfo autoInfo=new AutoInfo(); autoInfo.setLicensePlate("A00002"); autoInfo.setOwnerNo(people); sqlMap.insert("insertAutoInfo",autoInfo); sqlMap.commitTransaction(); }finally{ sqlMap.endTransaction(); } } } |
程序代码也是简单组合一下而已,想想 Hibernate 又是怎么写的呢?相信有 JDBC 经验的程序员应该更喜欢 iBATIS SQL Maps !
斗转星移、峰回路转,张三在经历过生意红火之后,接下来的一年内生意场上连连告负,不得不把自己的摊子收缩一下。这第一件事要把跑运输的车卖掉,就是那辆牌照为“A00002”的。
相应映射文件只需小小修改:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap>
<delete id="deleteAutoInfo" parameterClass="bo.AutoInfo"> <![CDATA[ delete from auto_info where license_plate=#licensePlate# and owner_no=#ownerNo.id# ]]> </delete> <resultMap id="get-autoInfo-result" class="bo.AutoInfo">
<result property="id" column="auto_id"/> <result property="licensePlate" column="license_plate"/> </resultMap> <resultMap id="get-people-result" class="bo.People">
<result property="id" column="owner_id"/> <result property="name" column="name"/> <result property="address" column="address"/> <result property="autoInfoList" column="owner_id" select="getAutoInfo"/> </resultMap> <select id="getPeople" resultMap="get-people-result" parameterClass="bo.People">
<![CDATA[ select * from people ]]> <dynamic prepend="where"> <isNotNull property="id"> <![CDATA[ owner_id=#id# ]]> </isNotNull> </dynamic> </select> <select id="getAutoInfo" resultMap="get-autoInfo-result" parameterClass="int"> <![CDATA[ select * from auto_info where owner_no=#id# ]]> </select> </sqlMap> |
添加了一个 delete 类型的 Mapped Statement。同样,也无需再解释了。相应程序代码:
package test;
import java.io.Reader;
import com.ibatis.sqlmap.client.*;
import com.ibatis.common.resources.*; import bo.*;
public class AutoMag {
private Reader reader;
private SqlMapClient sqlMap; private String resource = "SqlMapConfig.xml"; public void delPeople() throws Exception{ try{ reader = Resources.getResourceAsReader(resource); sqlMap=SqlMapClientBuilder.buildSqlMapClient(reader); sqlMap.startTransaction(); People people=new People(); people.setId(new Integer("1")); people=(People)sqlMap.queryForObject("getPeople",people); AutoInfo autoInfo=new AutoInfo(); autoInfo.setLicensePlate("A00002"); autoInfo.setOwnerNo(people); sqlMap.delete("deleteAutoInfo",autoInfo); sqlMap.commitTransaction(); }finally{ sqlMap.endTransaction(); } } } |
到这里,iBATIS SQL Maps 之旅也就结束了。是的,很有意犹未尽的感觉,还没按照 one-to-many 方式删除张三和他所有的车辆呢。
这个工作就留给你来完成吧!
我不想比较 Hibernate 和 iBATIS SQL Maps 孰优孰劣,因为这种比较实在无聊!从我开始写第一行 Delphi 代码开始,就充斥着“VC 好?还是 Delphi 好?”这样的比较!如果你尚未涉足过 JDBC,我想Hibernate 会更适合你… …