0.需要的jar包ibatis-2.3.0.677.jar,下载点击 IBatis-2.3.0.677.jar
1.准备jdbc.properties,数据库配置文件放在一个单独的配置文件中,这里使用的是Oracel数据库,你可以换成其他
jdbc.properties
#Oracle:datasource connectiongString jdbc.driverClassName = oracle.jdbc.driver.OracleDriver jdbc.url = jdbc:oracle:thin:@localhost:1521:orcl jdbc.username = scott jdbc.password = tiger
2.编写核心配置文件sqlmap-config.xml
sqlmap-config.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="com/phome/jdbc.properties" /> <!--DataSource ConnectingString --> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${jdbc.driverClassName}" /> <property name="JDBC.ConnectionURL" value="${jdbc.url}" /> <property name="JDBC.Username" value="${jdbc.username}" /> <property name="JDBC.Password" value="${jdbc.password}" /> </dataSource> </transactionManager> <sqlMap resource="com/phome/Student.xml" /> </sqlMapConfig>
3.Student.java
package com.phome; import java.util.Date; class Student { private int id; private String name; private Date birthday; 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 getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", birthday=" + birthday + "]"; } }
4.IStudentDao.java
package com.phome; import java.util.List; public interface IStudentDao { public List<Student> findAllStudents(); public void delete(int id); public void add(Student student); }
5.StudentDaoImpl.java
package com.phome; import java.io.IOException; import java.io.Reader; import java.sql.SQLException; import java.util.Date; import java.util.List; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class StudentDaoImpl implements IStudentDao { public static SqlMapClient sqlMapClient = null; static { try { Reader reader = Resources .getResourceAsReader("com/phome/sqlmap-config.xml"); sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @SuppressWarnings("unchecked") @Override public List<Student> findAllStudents() { // TODO Auto-generated method stub List<Student> students = null; try { students = sqlMapClient.queryForList("findAllStudent"); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return students; } @Override public void delete(int id) { // TODO Auto-generated method stub try { sqlMapClient.delete("deleteStudentById", id); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //main方法用于测试 public static void main(String[] args) throws Exception { IStudentDao dao = new StudentDaoImpl(); /*List<Student> students = dao.findAllStudents(); for (Student stu : students) { System.out.println(stu); }*/ Student stu = new Student(); stu.setId(5); stu.setName("wangwu2"); stu.setBirthday(new Date()); dao.add(stu); //dao.delete(5); } @Override public void add(Student student) { // TODO Auto-generated method stub try { sqlMapClient.insert("addStudent", student); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
6.Student.xml
这个文件里写sql语句和配置与数据库的映射关系,在dao实现里通过Id调用并执行sql语句
<?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="com.phome.Student" /> <resultMap class="Student" id="StudentResult"> <result property="id" column="ID" /> <result property="name" column="NAME" /> <result property="birthday" column="BIRTHDAY" /> </resultMap> <select id="findAllStudent" resultClass="Student"> select id,name,birthday from Student </select> <delete id="deleteStudentById" parameterClass="int"> delete from student where id=#id# </delete> <insert id="addStudent" parameterClass="Student"> insert into student(id,name,birthday)values(#id#,#name#,#birthday#) </insert> </sqlMap>