???? 1环境:ibatis2.3,spring2.5.
?????2最近项目里用 spring2.5+ibatis2.3做开发。ibatis主要是用它的动态sql.用ibatis有一段时间了。准备给自己备个案,把ibatis2.3使用过程中需要注意的地方给记录下来。并且分享给大家。
???? 3下面写几点自己遇到过的要注意的问题。
?????A:平常的sql语句比如select * from table where username='admin' and password='admin'(这是个容易被注入的语句)在where后面的第一个条件是不需要and的,从第二个查询条件起才需要and.但在配置ibatis的动态sql时一定要在第一个条件前加上and,否则会报错。代码如下:
?? <select id="Recycle_getPageList"
?????????? parameterClass="com.hzmc.common.domain.Page"
?????????? resultMap="recycle_codeList">
????????? ?select * from (select rownum rownum_,row_.* from (select * from
????????? MC$$ASSET_RECYCLE_CODE
?????? <dynamic prepend="WHERE">
?????????????? ?<isNotEmpty prepend="AND" property="searchParameters.owner">//此处第一个条件要加and
???????????????????? upper(owner) like upper('%$searchParameters.owner$%')
??????????????? </isNotEmpty>
??????????????? <isNotEmpty prepend="AND"property="searchParameters.otype">
??????????????????????upper(otype) like upper('%$searchParameters.otype$%')
??????????????? </isNotEmpty>
??
???????</dynamic>
?????????? )row_)
????????? <isEqual prepend="WHERE" property="paged" compareValue="true">
?????????????? ??rownum_ between #startNumber# and #endNumber# order by OPTIME desc
???????? ?</isEqual>
?? </select>
?
?
?? B :ibatis在调用过程或函数的过程中,大括号不能分成两行。代码如下:
???? <procedure id="auditselect_getById" parameterMap="AuditSelectshowDetails">??
??????? ??{? = call tlgadmin.GetSingleDetailrows(?)}??
??? ?</procedure>??
???? 假如改成:
??? <procedure id="auditselect_getById" parameterMap="AuditSelectshowDetails">??
?????? ?{
????????? ? = call tlgadmin.GetSingleDetailrows(?)
??????? }??
? ?</procedure>??
这样会报错。
?
?
? C关于sqlmap-config.xml文件的问题:假如一个配置文件要引用另一个配置文件,那么被应用的配置文件要放在引用配置文件的前面。否则,ibatis会报找不到错误(这应该是ibatis2.3的一个bug).
?<?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>
?<settings cacheModelsEnabled="true" lazyLoadingEnabled="true"
??enhancementEnabled="true" errorTracingEnabled="true"
??useStatementNamespaces="false" maxRequests="30" maxSessions="20"
??maxTransactions="10" />
?????1 <sqlMap
???????? ?resource="com/hzmc/capaa/domain/persistence/trustuser/AdminUser.xml" />
?????2?<sqlMap
???????? ?resource="com/hzmc/capaa/domain/persistence/verifyfactor/IpAddress.xml" />
??? ?3<sqlMap
???????? ?resource="com/hzmc/capaa/domain/persistence/verifyfactor/OsUser.xml" />
</sqlMapConfig>
???? 如上:假如1要应用2中的配置,那么这样写就会报错,必须方2在1的前面。
<?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>
?<settings cacheModelsEnabled="true" lazyLoadingEnabled="true"
??enhancementEnabled="true" errorTracingEnabled="true"
??useStatementNamespaces="false" maxRequests="30" maxSessions="20"
??maxTransactions="10" />
???? ?2?<sqlMap
???????? ?resource="com/hzmc/capaa/domain/persistence/verifyfactor/IpAddress.xml" />
?????1 <sqlMap
???????? ?resource="com/hzmc/capaa/domain/persistence/trustuser/AdminUser.xml" />
?????????3<sqlMap
???????? ?resource="com/hzmc/capaa/domain/persistence/verifyfactor/OsUser.xml" />
</sqlMapConfig>
这样ibatis才能找到。
先写到这吧。
1 用ibatis自带的log打出来的sql文太难看,全部是一行的,调起来很麻烦。不知道ibatis能不能改一下。
1 用ibatis自带的log打出来的sql文太难看,全部是一行的,调起来很麻烦。不知道ibatis能不能改一下。
一行的难道不好么?我觉一行反而更加方便。。。。
如果不喜欢一行的,用记事本自动换行就OK了
1 用ibatis自带的log打出来的sql文太难看,全部是一行的,调起来很麻烦。不知道ibatis能不能改一下。
一行的难道不好么?我觉一行反而更加方便。。。。
如果不喜欢一行的,用记事本自动换行就OK了
当遇到1000多行的sql文时,就知道写成一行是多么痛苦了
例如,map.get("colum1"),有时候就得到类型为byte[],,,郁闷 ,不知大家遇到过这种情况没?
邮箱:[email protected]
邮箱:[email protected]
这个文章里面有的:http://chenhua-1984.iteye.com/blog/368823
另外ibatis的配置xml中有个很恶心的bug,,,中文注释<!--xxxxxxx-->如果是奇数个字节,会报xml解析异常
这样我需要在sqlMap里配置一个动态的存储过程——就是过程名和参数都是由外面传进来的,如下。
<procedure id="ODSLog.callProcedure" parameterClass="java.lang.String">
{call $procedureName$}
</procedure>
现在的问题是,过程能成功调用,但是当过程执行完毕以后就会报错,如下。请大家帮我看看。
Exception in thread "pool-2-thread-1" org.springframework.jdbc.UncategorizedSQLException: SqlMapClient operation; uncategorized SQLException for SQL []; SQL state [null]; error code [0];
--- The error occurred in sqlmap/extract/ODSLogSqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the ODSLog.callProcedure-InlineParameterMap.
--- Check the output parameters.
--- Cause: java.lang.ArrayIndexOutOfBoundsException: 0; nested exception is com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in sqlmap/extract/ODSLogSqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the ODSLog.callProcedure-InlineParameterMap.
--- Check the output parameters.
--- Cause: java.lang.ArrayIndexOutOfBoundsException: 0
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:212)
at org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(SqlMapClientTemplate.java:271)
at com.delochi.etlmonitor.extract.dao.impl.ODSLogDAO.callProcedure(ODSLogDAO.java:43)
at com.delochi.etlmonitor.extract.service.impl.ODSLogService.callProcedure(ODSLogService.java:75)
at com.delochi.etlmonitor.extract.service.impl.ODSLogService.callProcedure(ODSLogService.java:1)
at com.delochi.etlmonitor.task.ExtractExecuteTask.run(ExtractExecuteTask.java:39)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in sqlmap/extract/ODSLogSqlMap.xml.
--- The error occurred while applying a parameter map.
--- Check the ODSLog.callProcedure-InlineParameterMap.
--- Check the output parameters.
--- Cause: java.lang.ArrayIndexOutOfBoundsException: 0
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:204)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryForObject(MappedStatement.java:120)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:518)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:493)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:106)
at org.springframework.orm.ibatis.SqlMapClientTemplate$1.doInSqlMapClient(SqlMapClientTemplate.java:273)
at org.springframework.orm.ibatis.SqlMapClientTemplate.execute(SqlMapClientTemplate.java:209)
... 8 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
at com.ibatis.sqlmap.engine.exchange.PrimitiveDataExchange.setData(PrimitiveDataExchange.java:51)
at com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap.refreshParameterObjectValues(ParameterMap.java:141)
at com.ibatis.sqlmap.engine.mapping.statement.ProcedureStatement.postProcessParameterObject(ProcedureStatement.java:26)
at com.ibatis.sqlmap.engine.mapping.statement.MappedStatement.executeQueryWithCallback(MappedStatement.java:193)
... 14 more
我也想过这个方法,但是还没有测试,想把他作为后路,实在不行了再弄,谢谢你哈
我就是不相信iBatis的开发人员没有考虑过这样的情况。
remapResults="true"的加入,就是考虑了动态过程的情况呀,希望只是我没发现怎么弄。