mybatis_mybatis写mapper文件注意事项
xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响
- < <
- > >
- & &
- ' '
- " "
在mapper文件中写sql语句时,为避免不必要的麻烦(如<等),建议使用<![CDATA[ ]]>来标记不应由xml解析器进行解析的文本数据,由<![CDATA[ ]]>包裹的所有的内容都会被解析器忽略 <![CDATA[ sql语句 ]]>
- <select id="getAccountsByBranch" resultType="Account" parameterType="string">
- <![CDATA[SELECT * FROM t_acctreg_accounts where acctno < #{acctno}]]>
- </select>
将整个sql语句用<![CDATA[ ]]>标记来避免冲突,在一般情况下都是可行的,但是如果这样写
- <select id="getAccountErrorCount" resultType="int" parameterType="map">
- <![CDATA[
- select count(*) from t_acctreg_accounterror
- <where>
- <if test="enddate != null and enddate != ''">
- createdate <= #{enddate}
- </if>
- <if test="acctno != null and acctno != ''">
- AND acctno LIKE '%'||#{acctno}||'%'
- </if>
- </where>
- ]]>
- </select>
就会收到错误信息:
org.springframework.jdbc.UncategorizedSQLException: Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters. Cause: java.sql.SQLException: 无效的列类型: 1111 ; uncategorized SQLException for SQL []; SQL state [99999]; error code [17004]; 无效的列类型: 1111; nested exception is java.sql.SQLException: 无效的列类型: 1111
这是由于该sql配置中有动态语句(where,if),where,if 条件不能放在<![CDATA[ ]]>中,否则将导致无法识别动态判断部分,导致整个sql语句非法.应该缩小范围,只对有字符冲突部分进行合法性调整
- <select id="getAccountErrorCount" resultType="int" parameterType="map">
- select count(*) from t_acctreg_accounterror
- <where>
- <if test="enddate != null and enddate != ''">
- <![CDATA[createdate <= #{enddate}]]>
- </if>
- <if test="acctno != null and acctno != ''">
- <![CDATA[AND acctno LIKE '%'||#{acctno}||'%']]>
- </if>
- </where>
- </select>
还有在向oracle插入数据时,mybatis3报Error setting null parameter. Most JDBC drivers require that the JdbcType must be specified for all nullable parameters,是由于参数出现了null值,对于Mybatis,如果进行操作的时候,没有指定jdbcType类型的参数,mybatis默认jdbcType.OTHER导致,给参数加上jdbcType可解决(注意大小写)
http://code.google.com/p/mybatis/issues/detail?id=224&q=Error%20setting%20null%20parameter&colspec=ID
- <insert id="insertAccountError" statementType="PREPARED"
- parameterType="AccountError">
- INSERT INTO t_acctreg_accounterror(createdate,acctno, errorinfo)
- VALUES(#{createdate,jdbcType=DATE},#{acctno,jdbcType=VARCHAR},#{errorinfo,jdbcType=VARCHAR})
- </insert>
相关文章
- ssm(Spring+Spring mvc+mybatis)Dao层配置sql的文件——DeptDaoMapper.xml
- Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
- Mybatis+mysql动态分页查询数据案例——Mybatis的配置文件(mybatis-config.xml)
- Mybatis+mysql动态分页查询数据案例——配置映射文件(HouseDaoMapper.xml)
- mybatis反向生成实体类、dao层以及映射文件
- 使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件(转)
- ajax上传文件及进度显示
- 深入浅出Mybatis系列(三)---配置详解之properties与environments(mybatis源码篇)
- C++ 抓取和批量下载网站上的图片或文件
- Atitit mybatis使用简明教程 目录 1.1. Mybatis.xml 配置文件1 1.2. Datamp.xml mapper文件主要作用是分模块放sql语句2 1.3. 查询
- Atitit mybatis spring整合。读取spring、yml、文件的mysql url 步骤,读取yml,文件,使用ongl定位到url pwd usr 读取mybatis模板配置,
- xml 文件不给提示(以mybatis 的 mapper映射文件为例)
- 【MyBatis笔记08】Mybatis中常用的一些操作
- 【MyBatis笔记11】Mybatis中的一级缓存和二级缓存
- MyBatis快速入门——第四章、mybatis动态sql_foreach
- 【Android 逆向】Android 系统文件分析 ( 根目录下的目录和文件 | /data/ 应用和用户数据目录 | /mnt/ 挂载其它设备目录 )
- [DB][mybatis]MyBatis mapper文件引用变量#{}与${}差异
- Mybatis在IDEA中找不到mapper映射文件
- mybatis自己主动生成mapper,dao,映射文件
- CVE-2022-30190漏洞复现和检测——属于典型的无文件攻击场景之一
- 集合Set映射一对多(使用xml文件)
- Python学习笔记(20)-文件和文件夹的移动、复制、删除、重命名
- linux奇怪的知识---文件描述符,pid,inode,ulimit和它们之间的关系
- MyBatis之工作原理,简单实体的增加、修改、删除、查询_Mybatis-原理总结
- Mybatis源码分析之(六)mybatis拦截器(Interceptor)的实现原理
- .gitignore忽略文件提交服务器