In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
mybatisplus @Select注解中拼写动态sql异常问题的解决方法,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
mybatisplus @Select注解中拼写动态sql异常
使用mybatisplus后,手写SQL语句很少了,偶尔使用@Select时,
之前一直用实体类传递参数,完全能够正常使用,今天换成了参数传递,报下面的错误
@Select("" +"select * from mi_taobao where 1=1" +"" +"and status = #{status}" +"" +"") public List getTaobao(Integer status);
Caused by: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'status' in 'class java.lang.Interger'
纳闷了,我就传个普通参数,和getter方法有啥关系呢,查找了一下资料
出现原因
这里出现的问题是在mapper方法中定义的参数 与 实体中定义的属性不一致 导致的,不知道mybatis在想什么。
解决方案
mapper层加@Param("xxxx")注解即可,修改如下就可以了
@Select("" +"select * from mi_taobao where 1=1" +"" +"and status = #{status}" +"" +"") public List getTaobao(@Param("status") Integer status);在注解上使用动态SQL(@select使用if)用script标签包围
然后像xml语法一样书写
@Select({"", "SELECT * FROM tbl_order", "WHERE 1=1", "", "AND mydate = #{mydate}", "", ""})用Provider去实现SQL拼接
例如:
public class OrderProvider { private final String TBL_ORDER = "tbl_order"; public String queryOrderByParam(OrderPara param) { SQL sql = new SQL().SELECT("*").FROM(TBL_ORDER); String room = param.getRoom(); if (StringUtils.hasText(room)) { sql.WHERE("room LIKE #{room}"); } Date myDate = param.getMyDate(); if (myDate != null) { sql.WHERE("mydate LIKE #{mydate}"); } return sql.toString(); }} public interface OrderDAO { @SelectProvider(type = OrderProvider.class, method = "queryOrderByParam") List queryOrderByParam(OrderParam param); }
注意:方式1有个隐患就是当传入参数为空的时候,可能会造成全表查询。
复杂SQL用方式2会比较灵活(当然,并不建议写复杂SQL),而且可以抽象成通用的基类,使每个DAO都可以通过这个基类实现基本的通用查询,原理类似Spring JDBC Template。
说明
如果XML元素嵌入在XML元素中,则可以在注释值中为动态SQL使用XML元素:
@Select("SELECT ...")
但是使用元素会触发SQL Mapper配置解析异常,由以下原因引起:
org.apache.ibatis.builder.BuilderException: Unknown element in SQL statement. at org.apache.ibatis.scripting.xmltags.XMLScriptBuilder.parseDynamicTags
如果nodeHandlers在课堂中检查方法org.apache.ibatis.builder.BuilderException,将注意到支持的元素有:
trim
where
set
foreach
if
choose
when
otherwise
bind
然而,包括基于注释的查询中的片段是不可能的。
看完上述内容,你们掌握mybatisplus @Select注解中拼写动态sql异常问题的解决方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.