OOP编程iBatis 学习笔记之二 单表增删改操作

 学习都是从简单到复杂,本例子是简单单表的增删改开始

Note.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>

 <typeAlias alias="Note" type="com.it.Note"/>

 <select id="selectAllNote" resultClass="Note">

   select * 

 from Note 

 </select>


 <select id="selectNoteById" 

 parameterClass
="int" resultClass="Note">

 select sid,sname,major,birth from Note where sid=#sid#

 </select>


 <insert id="insertNote" parameterClass="Note">

 insert into Note (

 sid,

 sname,

 major,

 birth

 )

 values(

 #sid#,#sname#,#major#,#birth#)

 </insert>


 <update id ="updateNoteById" parameterClass="Note">

 update note

 set sname=#sname#,

      major=#major#,

      birth=#birth#

      where sid=#sid#   

 </update>

 
 <delete id="deleteNoteById" parameterClass="int">

 delete from Note where sid=#sid#

 </delete>
<parameterMap id="noteParameters" class="Note" >

<parameter property="sname" jdbcType="VARCHAR" javaType="java.lang.String" mode="INOUT"/>
</parameterMap>

<procedure id="getNotes" parameterMap="noteParameters" >
{call swap_email_address (?)}
</procedure>
</sqlMap>

Note.Java

package com.it;
import java.sql.Date;

public class Note {

 private int sid=0;

 private String sname=null;

 private String major=null;

 private Date birth=null;

 private int book_oid;

 public int getBook_oid() {
    return book_oid;
}

public void setBook_oid(int bookOid) {
    book_oid = bookOid;
}

public int getSid() {
    return sid;
}

public void setSid(int sid) {
    this.sid = sid;
}

public String getSname() {
    return sname;
}

public void setSname(String sname) {
    this.sname = sname;
}

public String getMajor() {
    return major;
}

public void setMajor(String major) {
    this.major = major;
}

public Date getBirth() {
    return birth;
}

public void setBirth(Date birth) {
    this.birth = birth;
}
}

记得在sqlMapConfig.xml中加如下语句

<sqlMap resource="com/it/Note.xml"/>

执行文件

 

package com.it;

/***

 *2009-10-8 

 *单表操作CRUD 

 *
*/
import java.io.IOException;

import java.io.Reader;

import java.sql.Date;

import java.sql.SQLException;

import java.util.Iterator;

import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

public class INoteDAOImpl implements INoteDao {

private static SqlMapClient sqlMapClinet=null;
    static {
     Reader reader;

   try {

      String resource="com/it/SqlMapConfig.xml";

     reader = com.ibatis.common.resources.Resources.getResourceAsReader(resource);          

                            sqlMapClinet=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);        

      reader.close();   

       } catch (IOException e) {

                            // TODO Auto-generated catch block

                            e.printStackTrace();

                   }

         }

        public void addNoteBySequence(Note note) {

         }

 

         public void addNote(Note note) {
                   try {

                            sqlMapClinet.insert("insertNote", note);

                   } catch (SQLException e) {

                            e.printStackTrace();

                   }
          }

         public void delNoteById(int id) {

                   try {

                            sqlMapClinet.delete("deleteNoteById", id);

                   } catch (SQLException e) {

                            e.printStackTrace();

                   }
         }

         public List<Note> queryAllNote() {

                   List<Note> noteList =null;

                   try {

                            noteList=sqlMapClinet.queryForList("selectAllNote");

                   } catch (SQLException e) {

                            e.printStackTrace();
               }
              return noteList;

         }

   public Note queryNoteById(int id) {

        Note note =null;

         try {

             note=(Note)sqlMapClinet.queryForObject("selectNoteById", id);

         } catch (SQLException e) {

        e.printStackTrace();

        }

        return note;
  }

    public List<Note> queryNoteByName(String name) {

                   return null;

         }

    public void updateNote(Note note) {

       try {

          sqlMapClinet.update("updateNoteById", note);

                   } catch (SQLException e) {

       e.printStackTrace();

  }

         }

         public static void main( String[] args) {

                   INoteDao dao=new INoteDAOImpl();      

                   //dao.queryAllStudent();

                   System.out.println("OK");

                   /**             

                   for(Note student :dao.queryAllNote()){

                            System.out.println(student.getSname());

                   }
                   System.out.println(dao.queryNoteById(1));*
*/
                                     Note note =new Note();
                   note.setSid(10);
                   note.setSname("xname1");
                   note.setMajor("C#1");
                   note.setBirth(Date.valueOf("2012-10-8"));

                   dao.addNote(note);
         }

}
原文地址:https://www.cnblogs.com/meetweb/p/2750951.html