1.ibatIS的基本使用方法,这里通过一个例子来学习

--------------------------------------------------

a.新建IbatisTest项目

/IbatisTest/src/com/credream/test/testimpl.java

package com.credream.test;

import java.sql.Date;

import com.credream.IStudentDao;

import com.credream.IStudentDaoImpl;

import com.credream.Student;

public class testimpl {

    public static void main(String[] args) {

  IStudentDao dao=new IStudentDaoImpl();

  //测试queryAllStudent()方法

        /*for (Student student:dao.queryAllStudent()) {

   System.out.println(student);

  }*/

  //测试queryStudentById()方法

  /*System.out.println("id is 1:"+dao.queryStudentById(1));*/

  //测试addStudent(Student student) 方法

  /*Student student=new Student();

  student.setSid(4);

  student.setSname("xushu");

  student.setMajor("games");

  student.setBirth(Date.valueOf("2012-12-22"));

  student.setScore((float) 18.5);

     dao.addStudent(student);*/

  //测试delteStudentById(int id)方法

  //dao.delteStudentById(3);

       //测试updateStudentById(Student student) 方法;

  /*Student student=new Student();

  student.setSid(4);

  student.setSname("xushen");

  student.setMajor("games");

  student.setBirth(Date.valueOf("2012-12-22"));

  student.setScore((float) 18.5);

     dao.updateStudentById(student);*/

    //测试queryStudentByName(String name)方法

     /*List<Student> students=dao.queryStudentByName("x");

       for(Student student:students){

     System.out.println(student);

    }*/

    //测试:addStudentBySequence(Student student) 方法

  Student student=new Student();

  student.setSid(4);

  student.setSname("xushen");

  student.setMajor("games");

  student.setBirth(Date.valueOf("2012-12-22"));

  student.setScore((float) 18.5);

  dao.addStudentBySequence(student);


    }

}

-------------------------------

2./IbatisTest/src/com/credream/util/DButil.java

package com.credream.util;

public class DButil {


}

---------------------------------------------------------

3./IbatisTest/src/com/credream/IStudentDao.java

package com.credream;

import java.util.List;

public interface IStudentDao {

public void addStudent(Student student);

public void addStudentBySequence(Student student);

public void delteStudentById(int id);

public void updateStudentById(Student student);

public List<Student> queryAllStudent();

public List<Student> queryStudentByName(String name);

public Student queryStudentById(int id);

}

------------------------------------------------

4./IbatisTest/src/com/credream/IStudentDaoImpl.java

package com.credream;

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 sqlMapClient=null;

    static{

     Reader reader;

  try {

   reader = com.ibatis.common.resources.Resources.getResourceAsReader("com/credream/SqlMapConfig.xml");

   sqlMapClient=com.ibatis.sqlmap.client.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.让student表中插入记录

 sqlMapClient.insert("insertStudentBySequence", student);

    System.out.println("sid="+student.getSid());

} catch (SQLException e) {

 e.printStackTrace();

}

 }

 public void delteStudentById(int id) {

    try {

     System.out.println("开始删除....");

  System.out.println(sqlMapClient.delete("delteStudentById", id));

  System.out.println("删除成功...");

 } catch (SQLException e) {


  e.printStackTrace();

 }  

 }

 public List<Student> queryAllStudent() {

  List<Student> studentList=null;

  try {

   studentList=sqlMapClient.queryForList("selectAllStudent");

  } catch (SQLException e) {

   e.printStackTrace();

  }

  return studentList;

 }

 public Student queryStudentById(int id) {

  Student student=null;

  try {

   student=(Student) sqlMapClient.queryForObject("selectStudentById",id);

  } catch (SQLException e) {

   e.printStackTrace();

  }

  return student;

 }

 public List<Student> queryStudentByName(String name) {

  List<Student> students=null;

  try {

   students=sqlMapClient.queryForList("queryStudentByName",name);

  } catch (SQLException e) {


   e.printStackTrace();

  }

  return students;

 }

 public void updateStudentById(Student student) {

  try {

   System.out.println(sqlMapClient.update("updateStudentById", student));

  } catch (SQLException e) {

   e.printStackTrace();

  }

 }

}

---------------------------------------------------------------------------

5./IbatisTest/src/com/credream/Student.java

package com.credream;

import java.util.Date;

public class Student {

 /**

  * 这个地方最好不要覆盖父类的构造方法

  */

private int sid=0;

private String sname=null;

private String major=null;

private Date birth=null;

private float score=0;

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+"/sname"+sname+"tmajor:"+major+"birth:"+

 birth;

 return content;

}

}

-------------------------------------------------------------------------

6./IbatisTest/src/com/credream/SqlMap.propertiesdriver=com.mysql.jdbc.Driver

url=jdbc\:mysql\://localhost\:3306/test

username=root

password=1234

-------------------------------------------------

7./IbatisTest/src/com/credream/SqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" 

    "​​http://www.ibatis.com/dtd/sql-map-config-2.dtd​​​">

<!--<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 

   "​​http://ibatis.apache.org/dtd/sql-map-2.dtd​​​">

  -->

   <sqlMapConfig>

  <properties resource="com/credream/SqlMap.properties"/>

  <transactionManager type="JDBC">

   <dataSource type="SIMPLE">

   <property name="JDBC.Driver" value="${driver}"/>

   <property name="JDBC.ConnectionURL" value="${url}"/>

   <property name="JDBC.Username" value="${username}"/>

   <property name="JDBC.Password" value="${password}"/>

   </dataSource>

  </transactionManager>

  <sqlMap resource="com/credream/Student.xml"/>

  </sqlMapConfig>

 -------------------------------------------------------------

 8./IbatisTest/src/com/credream/Student.xml

 <?xml version="1.0" encoding="UTF-8"?> 

<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "​​http://www.ibatis.com/dtd/sql-map-2.dtd​​​">

  <sqlMap>

  <typeAlias alias="Student" type="com.credream.Student"/>

  <select id="selectAllStudent" resultClass="Student">

  select * from student;

  </select>

 <select id="selectStudentById" parameterClass="int" resultClass="Student">

 select * from student where sid=#sid#

 </select>

 <insert id="insertStudent" parameterClass="Student">

 insert into Student (sid,sname,major,birth,score)values (#sid#,#sname#,#major#,#birth#,#score#)

 </insert>


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

delete from student where sid=#sid#

 </delete>


 <update id="updateStudentById" parameterClass="student">

 update Student set sname=#sname#,major=#major#,score=#score#,birth=#birth#

 where sid=#sid#

 </update>



 <select id="queryStudentByName" parameterClass="string" resultClass="Student">

 select sid,sname,major,birth,score from student where sname like '%$sname$%'


 </select>



 <insert id="insertStudentBySequence" parameterClass="Student">

 <selectKey resultClass="int" keyProperty="sid">

 select studentPKSequence.netVal from dual

 </selectKey>

 insert into student(sid,sname,birth,major,score)values(#sid#,#sname#,

 #birth#,#major#,#score#)

 </insert>


  </sqlMap>

  ------------------------------------------------------

  9.G:\javalib\mysqldriver\mysql-connector-java-5.1.16-bin.jar

  G:\javalib\ibatis\ibatis-2.3.0.677.jar

  --------------------------------------------------------------------------