【问题标题】:mybatis-spring-boot, org.apache.ibatis.binding.BindingException: Invalid bound statementmybatis-spring-boot, org.apache.ibatis.binding.BindingException: 无效的绑定语句
【发布时间】:2016-07-30 14:29:40
【问题描述】:

mybatis-spring-boot-sample

我添加了一项服务以通过@Autowired 获取 CityMapper,但它失败了。 我更改了一些内容,如下所示:

@SpringBootApplication
public class SampleMybatisApplication implements CommandLineRunner {

    @Autowired
    private CityService cityService;

    public static void main(String[] args) {
        SpringApplication.run(SampleMybatisApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println(this.cityService.getCityById(1L));
    }

}

添加服务:

public interface CityService {
    City getCityById(Long id);
}

它的实现:

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityMapper cityMapper;

    @Override
    public City getCityById(Long id) {
        City city = null;
        try{
            city = cityMapper.selectCityById(id);
            // maybe, I want to do something else over here.
        }catch (Exception e) {
            e.printStackTrace();
        }
        return city;
    }

}

和 CityMapper :

@Repository
public class CityMapper {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

    public City selectCityById(long id) {
        return this.sqlSessionTemplate.selectOne("selectCityById", id);
    }

}

并且错误显示:

2016-04-09 16:38:47.400 ERROR 2017 --- [           main] o.s.boot.SpringApplication               : Application startup failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:795) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:776) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:763) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:356) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:295) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1112) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1101) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]
    at sample.mybatis.SampleMybatisApplication.main(SampleMybatisApplication.java:35) [classes/:na]
Caused by: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): sample.mybatis.service.CityService.getCityById
    at org.apache.ibatis.binding.MapperMethod$SqlCommand.<init>(MapperMethod.java:196) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.binding.MapperMethod.<init>(MapperMethod.java:44) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.binding.MapperProxy.cachedMapperMethod(MapperProxy.java:59) ~[mybatis-3.3.0.jar:3.3.0]
    at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:52) ~[mybatis-3.3.0.jar:3.3.0]
    at com.sun.proxy.$Proxy35.getCityById(Unknown Source) ~[na:na]
    at sample.mybatis.SampleMybatisApplication.run(SampleMybatisApplication.java:40) [classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:792) [spring-boot-1.3.0.RELEASE.jar:1.3.0.RELEASE]

【问题讨论】:

  • 我没有更改任何其他文件,除了我向您展示的内容。
  • 错误提示你缺少

标签: spring-boot mybatis spring-mybatis


【解决方案1】:

我用这个方法,可以通过compile。所以mapper和SQL.xml绑定的问题是个问题

public interface CityMapper { @Select("your sql statement") public City selectCityById(long id); }

【讨论】:

    【解决方案2】:

    在我的情况下,这完全不同 我有 2 个 JARS,但两个 JARS 错误地在同一个命名空间中有相同的 XML 文件

    这使得问题来来去去,因为错误取决于在 tomcat 中加载 jar 的顺序

    【讨论】:

      【解决方案3】:

      在我的例子中,我使用 mybatis-generator 生成了一个 mapper.xml,

      重点是,我将此文件移动到另一个包中: com.xxx.cust.mapper 到 com.xxx.frame.mapper,所以&lt;mapper namespace="com.xxx.cust.mapper.XXXMapper"&gt; 是错误的。

      【讨论】:

        【解决方案4】:

        这是 mybatis-spring-boot-starter 中的一个错误。您的界面 CityMapper 已自动注册为映射器,它不应该。 1.1.0 修复了这个问题。

        【讨论】:

          【解决方案5】:

          您的实施有什么特别的原因吗?

          该错误表明您缺少通常通过以下方式之一完成的映射器:

          public interface CityMapper {
              @Select("your sql statement")
              public City selectCityById(long id);
          }
          

          或者

          public interface CityMapper{
              public City selectCityById(long id);
          }
          

          CityMapper.xml

          <?xml version="1.0" encoding="UTF-8" ?>
          <!DOCTYPE mapper
          PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
          <mapper namespace="CityMapper">
              <select id="findById" resultType="City"></select>
          </mapper>
          

          【讨论】:

          • CityMapper 不是一个接口,它是一个普通的类。
          • 就像我说的,我说明的是mybatis的一个更常见的用法,如果你更喜欢普通的类方法,请阅读更多mybatis.org/spring/sqlsession.html
          猜你喜欢
          • 1970-01-01
          • 2017-11-01
          • 2021-04-23
          • 1970-01-01
          • 1970-01-01
          • 2021-01-11
          • 2015-11-11
          • 1970-01-01
          • 2018-03-21
          相关资源
          最近更新 更多