将以下jar包加入到工程,ibatis-2.3.0.677.jar和mysql-connector-java-5.0.8.jar。
属性文件:SqlMap.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=root
SqlMapconfig.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <properties resource="iBatis/entity/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="iBatis/entity/Student.xml" /> </sqlMapConfig>
Student.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="Student" type="iBatis.entity.Student"/> <select id="selectAllStudent" resultClass="Student"> select * from tb_student </select> <select id="selectStudentById" parameterClass="int" resultClass="Student"> select * from tb_student where id=#id# </select> <select id="selectStudentByName" parameterClass="String" resultClass="Student"> select id,name,birth,score from tb_student where name like '%$name$%' </select> <insert id="addStudent" parameterClass="Student"> insert into tb_student (name,birth,score) values (#name#,#birth#,#score#) <selectKey resultClass="int" keyProperty="id"> select @@identity as inserted </selectKey> </insert> <delete id="deleteStudentById" parameterClass="int"> delete from tb_student where id=#id# </delete> <update id="updateStudent" parameterClass="Student"> update tb_student set name=#name#,birth=#birth#,score=#score# where id=#id# </update> </sqlMap>
Student.java
package iBatis.entity; import java.sql.Date; public class Student { private int id; private String name; private Date birth; private float score; public Student() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } 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() { return "id=" + id + "\t name=" + name + "\t birth=" + birth + "\t score=" + score + "\n"; } }
IStudentDao.java
package iBatis.dao; import java.util.List; import iBatis.entity.Student; public interface IStudentDao { /* * 添加学生信息 */ public boolean addStudent(Student student); /* * 根据id删除学生信息 */ public boolean deleteStudentById(int id); /* * 更新学生信息 */ public boolean updateStudent(Student student); /* * 查询全部学生信息 */ public List<Student> selectAllStudent(); /* * 根据学生姓名模糊查询学生信息 */ public List<Student> selectStudentByName(String name); /* * 根据学生id查询学生信息 */ public Student selectStudentById(int id); }
StudentDaoImpl.java
package iBatis.daoimpl; import java.io.IOException; import java.io.Reader; 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; import iBatis.dao.IStudentDao; import iBatis.entity.Student; public class StudentDaoImpl implements IStudentDao { private static SqlMapClient sqlMapClient = null; // 读取配置文件 static { try { Reader reader = Resources .getResourceAsReader("iBatis/entity/SqlMapConfig.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } public boolean addStudent(Student student) { Object object = null; boolean flag = false; try { object = sqlMapClient.insert("addStudent", student); // System.out.println("添加学生信息的返回值:" + object); } catch (SQLException e) { e.printStackTrace(); } if (object != null) { flag = true; } return flag; } public boolean deleteStudentById(int id) { boolean flag = false; Object object = null; try { object = sqlMapClient.delete("deleteStudentById", id); // System.out.println("删除学生信息的返回值:" + object + ",这里返回的是影响的函数"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (object != null) { flag = true; } return flag; } public boolean updateStudent(Student student) { boolean flag = false; Object object = false; try { object = sqlMapClient.update("updateStudent", student); // System.out.println("更新学生信息的返回值:" + object ); } catch (SQLException e) { e.printStackTrace(); } if (object != null) { flag = true; } return flag; } public List<Student> selectAllStudent() { List<Student> students = null; try { students = sqlMapClient.queryForList("selectAllStudent"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return students; } public List<Student> selectStudentByName(String name) { List<Student> students = null; try { students = sqlMapClient.queryForList("selectStudentByName", name); } catch (SQLException e) { e.printStackTrace(); } return students; } public Student selectStudentById(int id) { Student student = null; try { student = (Student) sqlMapClient.queryForObject( "selectStudentById", id); } catch (SQLException e) { e.printStackTrace(); } return student; } }
TestIbatis.java
package iBatis.test; import java.sql.Date; import java.util.List; import iBatis.daoimpl.StudentDaoImpl; import iBatis.entity.Student; public class TestIbatis { public static void main(String[] args) { StudentDaoImpl studentDaoImpl = new StudentDaoImpl(); // 查询所有 System.out.println("查询初始数据表:"); List<Student> list1 = studentDaoImpl.selectAllStudent(); for (Student student : list1) { System.out.println(student); } // 测试插入 System.out.println("测试插入:"); Student addStudent = new Student(); addStudent.setName("张三"); addStudent.setBirth(Date.valueOf("2011-09-02")); addStudent.setScore(88); System.out.println(studentDaoImpl.addStudent(addStudent)); addStudent.setName("李四"); addStudent.setBirth(Date.valueOf("1990-08-03")); addStudent.setScore(98); System.out.println(studentDaoImpl.addStudent(addStudent)); // 根据Id查询 System.out.println("根据Id查询:"); System.out.println(studentDaoImpl.selectStudentById(2)); // 根据姓名查询 System.out.println("根据姓名查询:"); List<Student> list = studentDaoImpl.selectStudentByName("四"); for (Student student : list) { System.out.println(student); } // 查询所有 System.out.println("查询修改后数据表:"); List<Student> list2 = studentDaoImpl.selectAllStudent(); for (Student student : list2) { System.out.println(student); } // 更新信息 System.out.println("更新信息:"); Student updateStudent = new Student(); updateStudent.setId(1); updateStudent.setName("王五"); updateStudent.setBirth(Date.valueOf("1990-09-07")); updateStudent.setScore(24); System.out.println(studentDaoImpl.updateStudent(updateStudent)); // 查询所有 System.out.println("查询修改后数据表:"); List<Student> list3 = studentDaoImpl.selectAllStudent(); for (Student student : list3) { System.out.println(student); } // 删除数据 System.out.println("删除数据:"); Boolean b = studentDaoImpl.deleteStudentById(1); System.out.println("删除结果:" + b); // 查询所有 System.out.println("查询修改后数据表:"); List<Student> list4 = studentDaoImpl.selectAllStudent(); for (Student student : list4) { System.out.println(student); } } }
转载:http://www.tuicool.com/articles/mQNnYnF