OOP programming iBatis learning Note 2 add, delete, and modify a single table

Source: Internet
Author: User

Learning is from simple to complex. In this example, we start with adding, deleting, and modifying a simple single table.

 

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;
}
}

 

Remember to add the following statement in sqlMapConfig. xml:

<SqlMap resource ="Com/it/Note. xml"/>

Execution File

 

Package com. it;

/***

* 2009-10-8

* Single table operation 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 );
}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.