




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
iBatis 学习笔记 Author : meetweb mail: 环境配置1. 导入ibatis-26.jar mysql-connector-java-5.1.6-bin.jar导入配置文件SqlMapConfig.xml SqlMpertiesdriver=com.mysql.jdbc.Driverurl=jdbc:mysql://testusername=rootpassword=adminStudent.xml select * from student 2. 建立对应表-类文件 3. package com.it;4.5. import java.sql.Date;6.7. public class Note 8. private int sid=0;9. private String sname=null;10. private String major=null;11. private Date birth=null;12. 执行文件 package com.it;import java.io.IOException;import java.io.Reader;import java.sql.SQLException;import java.util.List;import com.ibatis.sqlmap.client.SqlMapClient;public class IStudentDAOImpl implements IStudentDao private static SqlMapClient sqlMapClinet=null;static Reader reader;try reader = mon.resources.Resources.getResourceAsReader(com/it/SqlMapConfig.xml);sqlMapClinet=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);reader.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();public List queryAllN() / TODO Auto-generated method stubList studentList =null;try studentList=sqlMapClinet.queryForList(selectAllNote); catch (SQLException e) / TODO Auto-generated catch blocke.printStackTrace();return studentList;public static void main(String args)IStudentDao dao=new IStudentDAOImpl();/dao.queryAllStudent();System.out.println(OK);/List myList=dao.queryAllStudent();for(Note student :dao.queryAllStudent()System.out.println(student.getSname();二 单表增删改操作Note.xml select * from Note select sid,sname,major,birth from Note where sid=#sid# insert into Note ( sid, sname, major, birth ) values( #sid#,#sname#,#major#,#birth#) update note set sname=#sname#, major=#major#, birth=#birth# where sid=#sid# delete from Note where sid=#sid# call swap_email_address (?)Note.Javapackage 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中加如下语句执行文件package com.it;/* *2010-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 = mon.resources.Resources.getResourceAsReader(resource);sqlMapClinet=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);reader.close(); catch (IOException e) / TODO Auto-generated catch blocke.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 queryAllNote() List 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 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);/dao.updateNote(note);/dao.delNoteById(10);三 . 2个表关联查询Note.javapublic 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;Note.javapackage com.it;import java.util.List;public class Book private int oid; private String name; private List notes; public int getOid() return oid;public void setOid(int oid) this.oid = oid;public String getName() return name;public void setName(String name) = name;public List getNotes() return notes;public void setNotes(List notes) this.notes = notes;Book.xml Note.xml select * from Note 测试package com.it;/* *2010-10-8 *2表关联 */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 = mon.resources.Resources.getResourceAsReader(resource);sqlMapClinet=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);reader.close(); catch (IOException e) / TODO Auto-generated catch blocke.printStackTrace();public static void main( String args) try List books=sqlMapClinet.queryForList(selectAllBooks);System.out.println(books.size();Book book=(Book)books.get(0); /System.out.println(book.getName(); List notes=(List) book.getNotes(); for (Iterator iterator = notes.iterator(); iterator.hasNext();) Note note = (Note) iterator.next(); System.out.println(note.getBook_oid(); System.out.println(note.getSname(); catch (SQLException e) e.pr
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年西电集团医院招聘(57人)考前自测高频考点模拟试题完整参考答案详解
- 2025广东广州工程技术职业学院第一批招聘一般岗位7人模拟试卷(含答案详解)
- 2025内蒙古通辽市招募企业储备人才37人模拟试卷带答案详解
- 2025年上海奉贤区教育系统事业单位编外用工招聘143名模拟试卷附答案详解(突破训练)
- 2025年灯具配附件:触点项目合作计划书
- 小学安全员培训课件
- 小学安全全员培训方案课件
- 小学安全专题培训心得课件
- Human-VEGFC-mRNA-生命科学试剂-MCE
- HIV-1-protease-IN-15-生命科学试剂-MCE
- Unit 1 Lesson1 Hello!教学设计 2024-2025学年冀教版英语七年级上册
- 2024年省食品生产监管能力大比武理论备赛试题库(含答案)
- 黑布林阅读初一5《大卫和超级神探》中文版
- 2025届高三化学一轮复习策略讲座
- 50000t天污水厂课程设计
- GB/T 44251-2024腿式机器人性能及试验方法
- 人音版 (五线谱)一年级上册音乐-1 《玩具兵进行曲》教案
- 医药产业园区智慧园区系统建设方案
- 村民集体经济发展规划方案
- 医药行业药品市场营销计划书中的销售预测与预算
- 人教版六年级数学上册第一、二单元试卷及答案
评论
0/150
提交评论