一:首先展示一下我的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文件
四:创建SqlMapConfig.properties,将用户名和密码改成自己的
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=ynsb1
password=1
五 创建sqlMap_student.xml映射文件,注意parameterClass要改成自己的pojo类
insert into student(
id,name,age,sex,address) values(
#id#,#name#,#age#,#sex#,#address# )
delete from student
delete from student where
id=#id#
update student set
name=#name#,sex=#sex#,age=#age#,address=#address#
where id=#id#
select*from student order by id
select*from student
where id=#id#
order by id
六:配置文件创建完成,接下来创建java类
1)创建StudentDto
importjava.sql.Date;public classStudentDto {//注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题//学生ID
private int id = 0;//学生姓名
private String name = "";//学生性别
private String sex = "";//学生年龄
private int age = 0;//学生地址
private String address = "";public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}publicString getSex() {returnsex;
}public voidsetSex(String sex) {this.sex =sex;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}
}
2)创建接口StudentDao
importjava.util.ArrayList;importcom.ibatis.sqlmap.client.SqlMapClient;importcom.shangcg.ibatis.StudentDto;public interfaceStudentDao {//添加student表的数据
public voidaddStudent(SqlMapClient sqlMap,StudentDto studentdto);//删除student表的数据
public voiddelStudent(SqlMapClient sqlMap);//删除student表的指定ID数据
public voiddelStudentByID(SqlMapClient sqlMap,StudentDto studentdto);//更新student表的数据
public voidupdataStudent(SqlMapClient sqlMap,StudentDto studentdto);//查询student表的所有数据
publicArrayList selectStudent(SqlMapClient sqlMap);//查询student表的指定ID数据
publicStudentDto selectStudentByID(SqlMapClient sqlMap,StudentDto studentdto);
}
3)创建实现类StudentImpl
importjava.sql.SQLException;importjava.util.ArrayList;importcom.ibatis.sqlmap.client.SqlMapClient;importcom.shangcg.ibatis.StudentDto;importcom.shangcg.interfaces.StudentDao;public class StudentImpl implementsStudentDao {//添加student表的数据
public voidaddStudent(SqlMapClient sqlMap, StudentDto studentdto) {try{
sqlMap.insert("insert_student", studentdto);
}catch(SQLException e) {
e.printStackTrace();
}
}//删除student表的数据
public voiddelStudent(SqlMapClient sqlMap) {try{
sqlMap.delete("delete_all_student", null);
}catch(SQLException e) {
e.printStackTrace();
}
}//删除student表的指定ID数据
public voiddelStudentByID(SqlMapClient sqlMap, StudentDto studentdto) {try{
sqlMap.delete("deleteByID_student",studentdto );
}catch(SQLException e) {
e.printStackTrace();
}
}//更新student表的数据
public voidupdataStudent(SqlMapClient sqlMap, StudentDto studentdto) {try{
sqlMap.update("updataStudent_test",studentdto );
}catch(SQLException e) {
e.printStackTrace();
}
}//查询student表的所有数据
publicArrayList selectStudent(SqlMapClient sqlMap) {//保存查询结果
ArrayList rsList = newArrayList();try{
rsList= (ArrayList)sqlMap.queryForList("select_all_student","");
}catch(SQLException e) {
e.printStackTrace();
}returnrsList;
}//查询student表的指定ID数据
publicStudentDto selectStudentByID(SqlMapClient sqlMap, StudentDto studentdto) {//返回后保存在info中
StudentDto info = newStudentDto();try{
info= (StudentDto)sqlMap.queryForObject("selectByID_student", studentdto);
}catch(SQLException e) {
e.printStackTrace();
}returninfo;
}
}
4)创建MainTest 测试类
importjava.io.IOException;importjava.io.Reader;importjava.util.ArrayList;importcom.ibatis.common.resources.Resources;importcom.ibatis.sqlmap.client.SqlMapClient;importcom.ibatis.sqlmap.client.SqlMapClientBuilder;importcom.shangcg.ibatis.StudentDto;importcom.shangcg.impl.StudentImpl;public classMainTest {public StudentImpl impl = newStudentImpl();public StudentDto info = newStudentDto();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 voidmain(String []args){
MainTest stu= newMainTest();
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 voidaddStudent_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 voiddelStudent_test(){
impl.delStudent(sqlmapclient);
}//删除student表的指定ID数据
public voiddelStudentByID_test(){//指定ID
info.setId(1);
impl.delStudentByID(sqlmapclient,info);
}//更新student表的数据
public voidupdataStudent_test(){//把要更新的数据填入info对象中
info.setId(6);
info.setName("zh2208up");
info.setSex("男");
info.setAge(20);
info.setAddress("上海up");
impl.updataStudent(sqlmapclient, info);
}//查询student表的所有数据
public voidselectStudent_test(){
StudentDto stu_dto= newStudentDto();//检索结果保存到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 voidselectStudentByID_test(){
StudentDto stu_dto= newStudentDto();
info.setId(1);
stu_dto=impl.selectStudentByID(sqlmapclient,info);if(stu_dto != null){
show(stu_dto);
}else{
System.out.println("no data!!!!");
}
}//打印查询结果
public voidshow(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();
}
}