加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

org.springframework.jdbc.BadSqlGrammarException

发布时间:2020-12-14 18:05:44 所属栏目:大数据 来源:网络整理
导读:这个错误在MyBatis中实际上很常见,就是SQL写错了。通常通过先在MySQL命令行执行一遍sql看有没有错误,如果有就更改,没有就基本上可以用了。 注意,我说的基本上可用并不代代表完全可用,比如今天我就遇到一个非常恶心的问题。 sql代码如下(这句sql经过在my

这个错误在MyBatis中实际上很常见,就是SQL写错了。通常通过先在MySQL命令行执行一遍sql看有没有错误,如果有就更改,没有就基本上可以用了。
注意,我说的基本上可用并不代代表完全可用,比如今天我就遇到一个非常恶心的问题。

sql代码如下(这句sql经过在mysql命令行中测试,能够获取数据,完全没有问题):

<select id="categoreListInfo" resultMap="BaseResultMap">
    SELECT
    term.term_id,term.name,term.slug,tax.taxonomy,tax.description,tax.count FROM wp_terms AS term
    LEFT JOIN wp_term_taxonomy AS tax ON(term.term_id=tax.term_id)
    where>
        if test="name != null or name != ''"
            and term.name like concat('%',#{name},'%')
        </iftest ="taxonomy != null or  taxonomy !=''"
            and tax.taxonomy = #{taxonomy}
        >
    
    limit #{start},#{size}
select>

但是我用如下单元测试就出现了问题,单元测试代码如下:

@Test
public void testPageListInfo() throws Exception {
    
    MapString,Object> paramMap = new HashMap();
    paramMap.put("name","");
    paramMap.put("start","0");
    paramMap.put("size","10");
    paramMap.put("taxonomy","post_tag");
    
    
    ListTerms list = termService.categoreListInfo(paramMap);
    
    int count = termService.categoryTotalCount(paramMap);
    
    System.out.println("总数:"+count);
    
    for (Terms terms : list) {
        
        System.out.println("terms:"+terms.getName());
        
        ListTermTaxonomy taxList = terms.getTax();
        
        for (TermTaxonomy termTaxonomy : taxList) {
            
            System.out.println("tax:"+termTaxonomy.getDescription()+"||"+termTaxonomy.getTaxonomy());
        }
    }
    
}

单元测试并没有写错,错的是参数问题,关键点是这个:

paramMap.put("start","0");
paramMap.put("size","10");

在mysql中limit两个参数实际为int类型,非字符串,而我此时在此传字符串,所以就出现org.springframework.jdbc.BadSqlGrammarException,说sql有问题。

所以大家切记在命令行执行sql以后,千万别掉以轻心,还是要细心,否则不必要的错误非常让人糟心。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读