ibatis入门实例(完整)
一:首先展示一下我的web文件结构,首先导入Ibatis所需jar和数据库驱动,从第二步开始跟着笔者一步步来
二:数据库建测试表
CREATE TABLE STUDENT (
ID NUMBER(5),
NAME VARCHAR2(10),
SEX VARCHAR2(10),
AGE NUMBER(10),
ADDRESS VARCHAR2(10),
CONSTRAINT PK_ID PRIMARY KEY(ID)
);
三:创建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"> <sqlMapConfig> <properties resource="SqlMapConfig.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="sqlMap_student.xml" /> </sqlMapConfig>
四:创建SqlMapConfig.properties,将用户名和密码改成自己的
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@localhost:1521:orcl username=ynsb1 password=1
五 创建sqlMap_student.xml映射文件,注意parameterClass要改成自己的pojo类
<?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 namespace="Test"> <statement id="insert_student" parameterClass="com.shangcg.ibatis.StudentDto"> insert into student( id,name,age,sex,address) values( #id#,#name#,#age#,#sex#,#address# ) </statement> <statement id="delete_all_student" parameterClass="com.shangcg.ibatis.StudentDto"> delete from student </statement> <statement id="deleteByID_student" parameterClass="com.shangcg.ibatis.StudentDto"> delete from student where id = #id# </statement> <statement id="updataStudent_test" parameterClass="com.shangcg.ibatis.StudentDto"> update student set name=#name#,sex=#sex#,age=#age#,address=#address# where id = #id# </statement> <statement id="select_all_student" resultClass="com.shangcg.ibatis.StudentDto"> select * from student order by id </statement> <statement id="selectByID_student" parameterClass="com.shangcg.ibatis.StudentDto" resultClass="com.shangcg.ibatis.StudentDto"> select * from student where id = #id# order by id </statement> </sqlMap>
六:配置文件创建完成,接下来创建java类
1)创建StudentDto
import java.sql.Date; public class StudentDto { // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题 //学生ID private int id = 0; //学生姓名 private String name = ""; //学生性别 private String sex = ""; //学生年龄 private int age = 0; //学生地址 private String address = ""; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
2)创建接口StudentDao
import java.util.ArrayList; import com.ibatis.sqlmap.client.SqlMapClient; import com.shangcg.ibatis.StudentDto; public interface StudentDao { //添加student表的数据 public void addStudent(SqlMapClient sqlMap,StudentDto studentdto); //删除student表的数据 public void delStudent(SqlMapClient sqlMap); //删除student表的指定ID数据 public void delStudentByID(SqlMapClient sqlMap,StudentDto studentdto); //更新student表的数据 public void updataStudent(SqlMapClient sqlMap,StudentDto studentdto); //查询student表的所有数据 public ArrayList selectStudent(SqlMapClient sqlMap); //查询student表的指定ID数据 public StudentDto selectStudentByID(SqlMapClient sqlMap,StudentDto studentdto); }
3)创建实现类StudentImpl
import java.sql.SQLException; import java.util.ArrayList; import com.ibatis.sqlmap.client.SqlMapClient; import com.shangcg.ibatis.StudentDto; import com.shangcg.interfaces.StudentDao; public class StudentImpl implements StudentDao { //添加student表的数据 public void addStudent(SqlMapClient sqlMap, StudentDto studentdto) { try { sqlMap.insert("insert_student", studentdto); } catch (SQLException e) { e.printStackTrace(); } } //删除student表的数据 public void delStudent(SqlMapClient sqlMap) { try { sqlMap.delete("delete_all_student", null); } catch (SQLException e) { e.printStackTrace(); } } //删除student表的指定ID数据 public void delStudentByID(SqlMapClient sqlMap, StudentDto studentdto) { try { sqlMap.delete("deleteByID_student",studentdto ); } catch (SQLException e) { e.printStackTrace(); } } //更新student表的数据 public void updataStudent(SqlMapClient sqlMap, StudentDto studentdto) { try { sqlMap.update("updataStudent_test",studentdto ); } catch (SQLException e) { e.printStackTrace(); } } //查询student表的所有数据 public ArrayList selectStudent(SqlMapClient sqlMap) { //保存查询结果 ArrayList rsList = new ArrayList(); try { rsList = (ArrayList)sqlMap.queryForList("select_all_student",""); } catch (SQLException e) { e.printStackTrace(); } return rsList; } //查询student表的指定ID数据 public StudentDto selectStudentByID(SqlMapClient sqlMap, StudentDto studentdto) { //返回后保存在info中 StudentDto info = new StudentDto(); try { info = (StudentDto)sqlMap.queryForObject("selectByID_student", studentdto); } catch (SQLException e) { e.printStackTrace(); } return info; } }
4)创建MainTest 测试类
import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.shangcg.ibatis.StudentDto; import com.shangcg.impl.StudentImpl; public class MainTest { public StudentImpl impl = new StudentImpl(); public StudentDto info = new StudentDto(); public static SqlMapClient sqlmapclient = null; static { try { //读取xml文件 Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml"); sqlmapclient = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String []args){ MainTest stu = new MainTest(); System.out.println("------------------------------- start ------------------------------"); //以下为各种方法测试 //添加student表的数据 stu.addStudent_test(); //删除student表的数据 //stu.delStudent_test(); //删除student表的指定ID数据 //stu.delStudentByID_test(); //更新student表的数据 //stu.updataStudent_test(); //查询student表的所有数据 //stu.selectStudent_test(); //查询student表的所有数据 //stu.selectStudentByID_test(); System.out.println("------------------------------- end ------------------------------"); } //添加student表的数据 public void addStudent_test(){ //把要插入的数据填入info对象中 info.setId(5); info.setName("zh2208"); info.setSex("男"); info.setAge(24); info.setAddress("上海"); impl.addStudent(sqlmapclient, info); //参数就是sqlMap_student.xml中配置对应的id } //删除student表的数据 public void delStudent_test(){ impl.delStudent(sqlmapclient); } //删除student表的指定ID数据 public void delStudentByID_test(){ //指定ID info.setId(1); impl.delStudentByID(sqlmapclient,info); } //更新student表的数据 public void updataStudent_test(){ //把要更新的数据填入info对象中 info.setId(6); info.setName("zh2208up"); info.setSex("男"); info.setAge(20); info.setAddress("上海up"); impl.updataStudent(sqlmapclient, info); } //查询student表的所有数据 public void selectStudent_test(){ StudentDto stu_dto = new StudentDto(); //检索结果保存到list中 ArrayList resultList = impl.selectStudent(sqlmapclient); for(int i = 0; i < resultList.size();i++){ stu_dto = (StudentDto) resultList.get(i); //打印对象中的信息 show(stu_dto); } } //查询student表的指定ID数据 public void selectStudentByID_test(){ StudentDto stu_dto = new StudentDto(); info.setId(1); stu_dto = impl.selectStudentByID(sqlmapclient,info); if(stu_dto != null){ show(stu_dto); }else{ System.out.println("no data!!!!"); } } //打印查询结果 public void show(StudentDto stu_dto){ System.out.print("学生ID :" + stu_dto.getId() + " ; "); System.out.print("学生姓名 :" + stu_dto.getName() + " ; "); System.out.print("学生性别 :" + stu_dto.getSex() + " ; "); System.out.print("学生年龄 :" + stu_dto.getAge() + " ; "); System.out.print("学生地址 :" + stu_dto.getAddress()); System.out.println(); } }