OOP编程iBatis 学习笔记之二 单表增删改操作
2012-11-05 13:26
375 查看
学习都是从简单到复杂,本例子是简单单表的增删改开始
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);
}
}
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);
}
}
相关文章推荐
- OOP编程iBatis 学习笔记之三 2个表或者多表关联查询
- 【原创】OOP编程iBatis 学习笔记之四 多表关联查询表达
- OOP编程iBatis 学习笔记之- 环境配置及简单例子测试
- PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
- JAVA并发编程学习笔记之CAS操作
- python学习笔记之二:字符串操作
- JAVA并发编程学习笔记之CAS操作
- Linux程序设计学习笔记----多线程编程基础概念与基本操作
- 【Python学习笔记】IO编程:操作文件和目录
- PHP 面向对象程序设计(oop)学习笔记(一) - 抽象类、对象接口、instanceof 和契约式编程
- Boost asio学习笔记之二—— 网络编程
- UNIX环境编程学习笔记(12)——文件I/O之目录操作
- [学习笔记]JAVA的多线程同步编程(非OOP)
- discuz学习笔记:以easyUI DataGrid为前台进行数据库增查删改操作
- Opencv学习笔记之二:操作像素
- ASP.NET MVC and jqGrid 学习笔记 6-增删改操作
- Qt学习笔记 TableWidget使用说明和增删改操作的实现
- JAVA并发编程学习笔记之CAS操作
- JavaScript Dom 编程艺术学习笔记之DOM操作