您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 交通运输 > ibatis学习笔记整理
ibatis:ibatis学习笔记整理疯狂代码ĵ:http:/CrazyCoder.cn/Java/Article6474.html1.目前的框架,不论是ORM的还是其它大都利用xml来解耦合。更加符合OO的思想,ibatis也不例外。其运行流程如下:DAO(其它主体如jsp或service)-------读取------ibatis的config.xml(文件内包括javabean和属性文件的映射)-----解析----à通过文件与bean的映射来解析对应关系读取一般利用java.io.Reader.片段如下:Readerreader=Resources.getResourceAsReader(“config.xml”);送给ibatis解析config.xmlSqllMapClientsqlMapper=SqlMapClientBuilder.buildSqlMapClient(reader);如果config.xml和属性映射文件正确的话就ok了,可以利用Dao来访问了。Config.xml和属性文件如下:!--config.xml--viewplaincopytoclipboardprint??xmlversion=1.0encoding=UTF-8?!DOCTYPEsqlMapConfigPUBLIC-//iBATIS.com//DTDSQLMapConfig2.0//EN=trueenhancementEnabled=truelazyLoadingEnabled=trueerrorTracingEnabled=truemaxRequests=32maxSessions=10maxTransactions=5useStatementNamespaces=false/transactionManagertype=JDBCdataSourcetype=SIMPLEpropertyname=JDBC.Drivervalue=net.sourceforge.jtds.jdbc.Driver/propertyname=JDBC.ConnectionURLvalue=jdbc:jtds:sqlserver://localhost:1433/ibatis/propertyname=JDBC.Usernamevalue=sa/propertyname=JDBC.Passwordvalue=1234//dataSource/transactionManagersqlMapresource=com/luyu/ibatis/User.xml//sqlMapConfig?xmlversion=1.0encoding=UTF-8?!DOCTYPEsqlMapConfigPUBLIC-//iBATIS.com//DTDSQLMapConfig2.0//EN=trueenhancementEnabled=truelazyLoadingEnabled=trueerrorTracingEnabled=truemaxRequests=32maxSessions=10maxTransactions=5useStatementNamespaces=false/transactionManagertype=JDBCdataSourcetype=SIMPLEpropertyname=JDBC.Drivervalue=net.sourceforge.jtds.jdbc.Driver/propertyname=JDBC.ConnectionURLvalue=jdbc:jtds:sqlserver://localhost:1433/ibatis/propertyname=JDBC.Usernamevalue=sa/propertyname=JDBC.Passwordvalue=1234//dataSource/transactionManagersqlMapresource=com/luyu/ibatis/User.xml//sqlMapConfigviewplaincopytoclipboardprint?!--属性文件.xml--!--属性文件.xml--viewplaincopytoclipboardprint?PREclass=csharpname=code?xmlversion=1.0encoding=UTF-8?!DOCTYPEsqlMapPUBLIC-//iBATIS.com//DTDSQLMap2.0//EN=UsertypeAliasalias=usertype=com.luyu.ibatis.bean.User/selectid=getUserparameterClass=java.lang.StringresultClass=user![CDATA[selectname,sexfromt_userwherename=#name#]]/selectselectid=findAllUserresultClass=user![CDATA[selectname,sexfromt_user]]/selectupdateid=updateUserparameterClass=user![CDATA[UPDATEt_userSETname=#name#,sex=#sex#WHEREid=#id#]]/updateinsertid=insertUserparameterClass=userINSERTINTOt_user(name,sex)VALUES(#name#,#sex#)/insertdeleteid=deleteUserparameterClass=java.lang.Stringdeletefromt_userwhereid=#value#/delete/sqlMap/PREPREclass=csharpname=code/PREPREclass=csharpname=codeDao中的第一个让问如下:/PREPREclass=csharpname=codepublicstaticListUserselectAllAccounts()throwsSQLException{/PREPREclass=csharpname=codereturnsqlMapper.queryForList(findAllUser);/PREPREclass=csharpname=code}/PREviewplaincopytoclipboardprint??xmlversion=1.0encoding=UTF-8?!DOCTYPEsqlMapPUBLIC-//iBATIS.com//DTDSQLMap2.0//EN=UsertypeAliasalias=usertype=com.luyu.ibatis.bean.User/selectid=getUserparameterClass=java.lang.StringresultClass=user![CDATA[selectname,sexfromt_userwherename=#name#]]/selectselectid=findAllUserresultClass=user![CDATA[selectname,sexfromt_user]]/selectupdateid=updateUserparameterClass=user![CDATA[UPDATEt_userSETname=#name#,sex=#sex#WHEREid=#id#]]/updateinsertid=insertUserparameterClass=userINSERTINTOt_user(name,sex)VALUES(#name#,#sex#)/insertdeleteid=deleteUserparameterClass=java.lang.Stringdeletefromt_userwhereid=#value#/delete/sqlMap?xmlversion=1.0encoding=UTF-8?!DOCTYPEsqlMapPUBLIC-//iBATIS.com//DTDSQLMap2.0//EN=UsertypeAliasalias=usertype=com.luyu.ibatis.bean.User/selectid=getUserparameterClass=java.lang.StringresultClass=user![CDATA[selectname,sexfromt_userwherename=#name#]]/selectselectid=findAllUserresultClass=user![CDATA[selectname,sexfromt_user]]/selectupdateid=updateUserparameterClass=user![CDATA[UPDATEt_userSETname=#name#,sex=#sex#WHEREid=#id#]]/updateinsertid=insertUserparameterClass=userINSERTINTOt_user(name,sex)VALUES(#name#,#sex#)/insertdeleteid=deleteUserparameterClass=java.lang.Stringdeletefromt_userwhereid=#value#/delete/sqlMapviewplaincopytoclipboardprint?viewplaincopytoclipboardprint?Dao中的第一个让问如下:Dao中的第一个让问如下:viewplaincopytoclipboardprint?publicstaticListUserselectAllAccounts()throwsSQLException{publicstaticListUserselectAllAccounts()throwsSQLException{viewplaincopytoclipboardprint?returnsqlMapper.queryForList(findAllUser);returnsqlMapper.queryForList(findAllUser);viewplaincopytoclipboardprint?}}共有2个jar1.jdts对sqlserver2000的jar2.ibatis2.3.2.715.jar所有代码如下:User.javaviewplaincopytoclipboardprint?packagecom.luyu.ibatis
本文标题:ibatis学习笔记整理
链接地址:https://www.777doc.com/doc-4893070 .html