打印本文 打印本文 关闭窗口 关闭窗口
iBATIS SQL Maps(四)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2788  更新时间:2007/11/14 12:58:25  文章录入:mintao  责任编辑:mintao

这一章节是本系列文章最后一篇。经历过前三篇文章的洗礼,相信你对 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(

[1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口