Ibatis实例程序
一、搭建开发环境: 所需jar包:ibatis-2.3.0.677.jar 数据库驱动:ojdbc14.jar |
二、配置文件: 1、SqlMapConfig.xml 2、SqlMap.properties driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@127.0.0.1:1521:orcl username=zyx password=zyx 3、Student.xml |
四、数据库脚本 1、系统管理用创建新用户: --创建用户 create user zyx identified by zyx; --创建表空间 create tablespace ts_bj_zyx datafile 'F:\\bj_zyx\\bj_zyx_data.dbf' size 1000m; --设置用的默认表空间,不设置自动默认system。 alter user zyx default tablespace ts_bj_zyx; --设置用户在表空间的权限。 alter user zyx quota unlimited on ts_bj_zyx; --设置用户的权限 grant connect to zyx; grant resource to zyx; grant create session, grant any privilege to zyx; grant create cluster to zyx; grant create database link to zyx; grant create sequence to zyx; grant create session to zyx; grant create synonym to zyx; grant create table to zyx; grant create view to zyx; grant debug any procedure,debug connect session to zyx; 2、新用户登录数据库建表: create table STUDENT ( sid NUMBER(15), sname VARCHAR2(16), major VARCHAR2(16), birth DATE, score NUMBER(6,2) ); create sequence studentPkSequence start with 5 increment by 1; insert into student (SID, SNAME, MAJOR, BIRTH, SCORE, ROWID) values ('1', '张三丰', '数学', to_date('01-01-2014', 'dd-mm-yyyy'), '100', 'AAARgaAAIAAAAAOAAA'); insert into student (SID, SNAME, MAJOR, BIRTH, SCORE, ROWID) values ('2', '李世民', '历史', to_date('01-01-2014', 'dd-mm-yyyy'), '98', 'AAARgaAAIAAAAAOAAB'); insert into student (SID, SNAME, MAJOR, BIRTH, SCORE, ROWID) values ('3', '武松', '武术', to_date('01-01-2014', 'dd-mm-yyyy'), '99', 'AAARgaAAIAAAAAOAAC'); insert into student (SID, SNAME, MAJOR, BIRTH, SCORE, ROWID) values ('4', '张无忌', '武术', to_date('01-01-2014', 'dd-mm-yyyy'), '100', 'AAARgaAAIAAAAAOAAD'); |
五、学生实体类: package com.zyx; import java.sql.Date; public class Student { private int sid = 0; private String sname; private String major; private Date birth; private float score; public Student(){}; public Student(int sid, String sname, String major, Date birth, float score) { this.sid = sid; this.sname = sname; this.major = major; this.birth = birth; this.score = score; } 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; } public float getScore() { return score; } public void setScore(float score) { this.score = score; } @Override public String toString() { String content = "sid=" + sid+"\\\\\\\\tsname=" + sname+"\\\\\\\\tmajor="+major+"\\\\\\\\tbirth="+birth+"\\\\\\\\tscore="+score; return content; } } |
六、操作接口类 package com.zyx; import java.util.List; public interface IStudentDAO { public void addStudent(Student student); public void addStudentBySequence(Student student); public void deleteStudentById(int id); public void updateStudent(Student student); public List queryAllStedent(); public List queryStudentByName(String name); public Student queryStudentById(int id); } |
七、操作接口实现类:带有测试方法。 package com.zyx; import java.io.IOException; import java.io.Reader; import java.sql.Date; import java.sql.SQLException; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class IStudentDAOImpl implements IStudentDAO { private static SqlMapClient sqlMapClient; static{ try { Reader reader = Resources.getResourceAsReader("com/zyx/SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 插入一个实体对象 */ public void addStudent(Student student) { try { sqlMapClient.insert("insertStudent", student); } catch (SQLException e) { e.printStackTrace(); } } /** * 通过序列增加学生 */ public void addStudentBySequence(Student student) { try { //1.从数据库序列中获取主键值 //2.往studnet表中插入记录 sqlMapClient.insert("insertStudentBySequence",student); System.out.println("sid="+student.getSid()); } catch (SQLException e) { e.printStackTrace(); } } /** * 删除指定id的单个对象 */ public void deleteStudentById(int id) { try { System.out.println(sqlMapClient.delete("deleteStudentById", id)); } catch (SQLException e) { e.printStackTrace(); } } /** * 查询所有的对象 */ public List queryAllStedent() { List studentList = null; try { studentList = sqlMapClient.queryForList("selectAllStudent"); } catch (SQLException e) { e.printStackTrace(); } return studentList; } /** * 查询指定id的单个对象 */ public Student queryStudentById(int id) { Student student = null; try { student = (Student) sqlMapClient.queryForObject("selectStudentById",id); } catch (SQLException e) { e.printStackTrace(); } return student; } /** * 模糊查询实体对象 */ public List queryStudentByName(String name) { List studentList = null; try { studentList = sqlMapClient.queryForList("selectStudentByName", name); } catch (SQLException e) { e.printStackTrace(); } return studentList; } /** * 修改实体对象 */ public void updateStudent(Student student) { try { System.out.println(sqlMapClient.update("updateStudentById", student)); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) { IStudentDAO dao = new IStudentDAOImpl(); //1查询所有的对象 /*for (Student student : dao.queryAllStedent()) { System.out.println(student); }*/ //2查询指定id的单个对象 //System.out.println(dao.queryStudentById(2)); //3插入一个实体对象 /*Student student = new Student(); student.setSid(4); student.setSname("宋江"); student.setMajor("政治"); student.setBirth(Date.valueOf("2010-11-12")); student.setScore(97); dao.addStudent(student);*/ //4删除指定id的单个对象 //dao.deleteStudentById(4); //5修改实体对象 /*Student student = new Student(); student.setSid(4); student.setSname("宋江1"); student.setMajor("政治1"); student.setBirth(Date.valueOf("2012-12-12")); student.setScore(100); dao.updateStudent(student);*/ //6模糊查询实体对象 /*for (Student student : dao.queryStudentByName("张")) { System.out.println(student); }*/ //7通过序列增加学生 /*Student student = new Student(); student.setSid(10); student.setSname("zyx"); student.setMajor("政治"); student.setBirth(Date.valueOf("2014-1-1")); student.setScore(100); dao.addStudentBySequence(student);*/ } } |