ibatis学习之搭建Java项目
IBATIS简介
ibatis是Apache的开源项目,一个ORM解决方案,ibatis最大的特点就是小巧,上手很快。
使用ibatis提供的ORM机制,对业务逻辑实现人员而言,面对的是纯粹的Java对象,这一层与通过Hibernate实现ORM而言是基本一致的。
iBatis是一个基于SQL映射支持Java和·NET的持久层框架,相对Hibernate和ApacheOJB等“一站式”ORM解决方案而言,iBatis是一种“半自动化”的ORM实现。
一、JAR包依赖
ibatis-2.3.4.726.jar
mysql-connector-java-5.0.8-bin.jar
二、SqlMap.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1:3306/test username=root password=root
三、SqlMapConfig.xml
四、Student.xml
select*fromstudent select*fromstudentwhereid=#id# insertintoStudent(id,name,age,address)values(#id#,#name#,#age#,#address#) select@@identityasinserted deletefromstudentwhereid=#id# deletefromStudentwhereid=#id# updatestudentsetname=#name#,age=#age#,address=#address#whereid=#id# select*fromstudentwherenamelike'%$name$%' select*fromstudentwherenamelike'%$name$%'andage>=#age# select*fromstudentwherenamelike?andage>=?
五、JAVA代码
实体类:略
Dao:略
DaoImpl:
packagecom.ligang; importjava.io.IOException; importjava.io.Reader; importjava.sql.SQLException; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importcom.ibatis.common.resources.Resources; importcom.ibatis.sqlmap.client.SqlMapClient; importcom.ibatis.sqlmap.client.SqlMapClientBuilder; publicclassStudentDaoImplimplementsStudentDao{ publicstaticSqlMapClientsqlMapClient=null; static{ try{ Readerreader=Resources.getResourceAsReader("com/ligang/SqlMapConfig.xml"); sqlMapClient=SqlMapClientBuilder.buildSqlMapClient(reader); }catch(IOExceptione){ e.printStackTrace(); } } publicListfindAll(){ List list=null; try{ list=sqlMapClient.queryForList("findAll"); }catch(SQLExceptione){ e.printStackTrace(); } returnlist; } publicStudentfindByID(Stringid){ Studentstudent=null; try{ student=(Student)sqlMapClient.queryForObject("findByID",id); }catch(SQLExceptione){ e.printStackTrace(); } returnstudent; } publicvoidaddStudent(Studentstudent){ try{ sqlMapClient.insert("insertStudent",student); }catch(SQLExceptione){ e.printStackTrace(); } } publicvoiddeleteStudentByID(Stringid){ try{ sqlMapClient.delete("deleteStudentByID",id); }catch(SQLExceptione){ e.printStackTrace(); } } publicvoiddeleteStudent(Studentstudent){ try{ sqlMapClient.delete("deleteStudent",student); }catch(SQLExceptione){ e.printStackTrace(); } } publicvoidupdateStudent(Studentstudent){ try{ sqlMapClient.update("updateStudent",student); }catch(SQLExceptione){ e.printStackTrace(); } } publicList findByCon(Stringname){ List stuList=newArrayList (); try{ stuList=sqlMapClient.queryForList("selectByLike",name); }catch(SQLExceptione){ e.printStackTrace(); } returnstuList; } publicList findByCon(Studentstudent){ List stuList=newArrayList (); try{ stuList=sqlMapClient.queryForList("findByCon1",student); }catch(SQLExceptione){ e.printStackTrace(); } returnstuList; } publicList findByCon(Mapmap){ List stuList=newArrayList (); try{ stuList=sqlMapClient.queryForList("findByCon2",map); }catch(SQLExceptione){ e.printStackTrace(); } returnstuList; } }
总结
通过学习我们会发现,Hibernate体系中的内容真的很多,而ibatis更容易上手,小巧灵活。本文有关ibatis搭建Java项目的介绍就到这里,希望对大家有所帮助。