IT俱乐部 Java mybatis xml注释sql的注意事项及说明

mybatis xml注释sql的注意事项及说明

mybatis xml注释sql的注意事项

说明:在mybatis 的sql里不是不能写注释,而是注释不能有#{},mybatis仍旧会把#{}算成一个带注入的参数

如代码段2所示,mysql数据库更换为oracle数据库时,#{date}需要明确jdbcType,而该代码段中仅将适用于mysql的sql注释,依旧会报同样的错,最好是将注释的sql删除。

1
2
3
4
5
--         select slagsteel_ratio FROM process_history WHERE r furnace_num = #{furnaceNum}
--         ORDER BY real_time DESC limit 0,1
select slagsteel_ratio FROM(
 select slagsteel_ratio FROM process_history WHERE real_time <= #{date,jdbcType=TIMESTAMP}
 ORDER BY real_time DESC ) WHERE ROWNUM = 1

注意事项

1、where语句等用到大于小于等符号时候,需要使用 ]]>或者 进行识别。

2、如果sql文中有动态sql文标签,比如sql文,此时wwww为传递的参数,不需用#{wwww }取值,直接使用即可。

3、如果超过一个参数,需要在接口方法的每个参数定义时候使用@Param注解。

4、order by语句可以使用动态sql

1
ORDER BY eeee ORDER BY rrrrrr

5、SELECT

1
2
CASE
WHEN  条件1 THEN 字段1

WHEN 条件2 THEN 字段2

1
2
3
         ELSE    字段3
         END
FROM.....

只要满足一个WHEN,后面的WHEN就不会执行了,跳出CASE WHEN 语句

mybatis xml 注释问题

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.type.TypeException: Could not set parameters for mapping: ParameterMapping{property=‘mybatis_plus_first’, mode=IN, javaType=long, jdbcType=null, numericScale=null, resultMapId=‘null’, jdbcTypeName=‘null’, expression=‘null’}. Cause: org.apache.ibatis.type.TypeException: Error setting non null for parameter #1 with JdbcType null . Try setting a different JdbcType for this parameter or a different configuration property. Cause: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).] with root cause

java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

解决办法

在mybatis里面注释语句的时候,不要用–,用 。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/java/8809.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部