ibatis3学习笔记1前段时间看了oracle内容,干脆把ibatis结合oracle一块搞了。准备ibatis环境oracle10gexpressibatisbeta5新建工程在WEBINF\lib下导入以下包:..\oraclexe\app\oracle\product\10.2.0\server\jdbc\lib\ojdbc14.jaribatis-3-core-3.0.0.208.jar总配置文件SqlMapConfig.xml-//ibatis.apache.org//DTDConfig3.0//EN=developmentenvironmentid=developmenttransactionManagertype=JDBC/dataSourcetype=POOLEDpropertyname=drivervalue=oracle.jdbc.driver.OracleDriver/propertyname=urlvalue=jdbc:oracle:thin:@localhost:1521:xe/propertyname=usernamevalue=puf/propertyname=passwordvalue=pufang890505//dataSource/environment/environmentsmappersmapperresource=cn/pf/ibatis/domain/Student.xml//mappers/configuration关于每个实体的映射文件(map)Student.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEmapperPUBLIC-//ibatis.apache.org//DTDMapper3.0//EN=cn.pf.ibatis.domain.StudentMapperselectid=selectStudentparameterType=intresultType=cn.pf.ibatis.domain.Studentselect*fromStudentwhereid=#{id}/select/mapper创建StudentPOJOpackagecn.pf.ibatis.domain;importjava.util.Date;/***学生PO*@authorpf*@version2010-3-16下午03:00:00*/publicclassStudent{/***学生编号*/privateintid;/***学生姓名*/privateStringname;/***学生专业*/privateStringmajor;/***学生生日*/privateDatebirth;/***学生分数*/privatedoublescore;/***...构造函数*/publicStudent(){super();}/***...构造函数*@paramid*@paramname*@parammajor*@parambirth*@paramscore*/publicStudent(intid,Stringname,Stringmajor,Datebirth,doublescore){super();this.id=id;this.name=name;this.major=major;this.birth=birth;this.score=score;}/***idgetter方法*@returntheid*/publicintgetId(){returnid;}/***idsetter方法*@paramidtheidtoset*/publicvoidsetId(intid){this.id=id;}/***namegetter方法*@returnthename*/publicStringgetName(){returnname;}/***namesetter方法*@paramnamethenametoset*/publicvoidsetName(Stringname){this.name=name;}/***majorgetter方法*@returnthemajor*/publicStringgetMajor(){returnmajor;}/***majorsetter方法*@parammajorthemajortoset*/publicvoidsetMajor(Stringmajor){this.major=major;}/***birthgetter方法*@returnthebirth*/publicDategetBirth(){returnbirth;}/***birthsetter方法*@parambirththebirthtoset*/publicvoidsetBirth(Datebirth){this.birth=birth;}/***scoregetter方法*@returnthescore*/publicdoublegetScore(){returnscore;}/***scoresetter方法*@paramscorethescoretoset*/publicvoidsetScore(doublescore){this.score=score;}/***转换对象为字符串*@return对象转换后的字符串*@seejava.lang.Object#toString()*/@OverridepublicStringtoString(){+returnStudent[birth=+birth+,id=+id+,major=+major,name=+name+,score=+score+];}}test.javaStringresource=SqlMapConfig.xml;Readerreader=null;try{//使用ibatis提供的Resources类读取资源文件reader=Resources.getResourceAsReader(resource);}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}//根据资源文件内容建立session工厂SqlSessionFactorysqlMapper=newSqlSessionFactoryBuilder().build(reader);//session工厂打开一个sessionSqlSessionsession=sqlMapper.openSession(true);Studentstudent;try{student=(Student)session.selectOne(cn.pf.ibatis.domain.StudentMapper.selectStudent,1);}finally{session.close();}System.out.println(student.toString());ibatis3果然改动很大,调试花了不少时间,很多细小的错误耽误了很久。那个ibatis3userguide缺少完整的实例真是不爽,自己一点点试探过来,继续研究各种配置,下一步完成一个基本的crud操作。上篇简单调试了一个实例,现在仔细看看ibatis3userguide上的内容,接下来的几篇准备整理userguide上的内容,昀后通过一个综合的例子来实践。关于实体映射文件中namespace,以前的版本是可选的内容,现在被用来与接口绑定,也就是说把接口的实现转移到xml文件中来了,以后的维护会相当的方便。第一篇的根据id查询学生的例子中,稍做修改:建立StudentDAO接口packagecn.pf.ibatis.dao;importjava.util.List;importcn.pf.ibatis.domain.Student;/***StudentDAO接口*@authorpf*@version2010-3-16下午03:05:36*/publicinterfaceStudentDAO{publicStudentqueryStudentById(intid);}更改student.xml中的mapper元素namespace属性test.java中调用改为Studentstudent;StudentDAOstudentDAO=session.getMapper(StudentDAO.class);try{//student=(Student)session.selectOne(cn.pf.ibatis.domain.StudentMapper.selectStudent,1);student=studentDAO.queryStudentById(1);}finally{session.close();}System.out.println(student.toString());生命周期SqlSessionFactoryBuilder:用于建立SqlSessionFactory的工具类,在创建SqlSessionFactory以后无需再让它存在于应用程序中,文档上说它的生命周期昀好在局部方法内,所以在代码中看到//根据资源文件内容建立session工厂SqlSessionFactorysqlMapper=newSqlSessionFactoryBuilder().build(reader);没用引用指向SqlSessionFactoryBuilder,会被gc回收。SqlSessionFactory:一旦建立,SqlSessionFactory就应该存在于整个应用程序生命周期,因为根据资源文件建立SqlSessionFactory对象需要很大的开销,所以保留在整个应用的生命周期中,昀佳实践中推荐使用spring等依赖注入的框架从而管理SqlSessionFactory的单例生命周期。SqlSession:每一个线程都应该有自己的SqlSession对象,SqlSession对象不是线程安全的所以不应该被共享,如果使用web框架,应该将SqlSession的生命周期看作HTTPRequest的生命周期,在返回HTTPResponse的时候关闭SqlSessionSqlSessionsession=sqlSessionFactory.openSession();try{//dowork}finally{session.close();}MapperInstances:MapperInstances是绑定ibatis映射文件实现的接口,MapperInstances的生命周期应该与SqlSession一样,但是MapperInstances昀好在一个方法中被创建,在方法结束时被销毁。SqlSessionsession=sqlSessionFactory.openSession();try{BlogMappermapper=session.getMapper(BlogMapper.class);//dowork}finally{session.close();}配置文件配置文件的层次结构如下:•configurationopropertiesosettingsotypeAliasesotypeHandlersoobjectFactoryopluginsoenvironmentsenvironment•transactionManager•dataSourceomappers1properties:用来定义外