2016-04-09 45 views
1

mybatis-spring-boot-sampleMyBatis的彈簧引導,org.apache.ibatis.binding.BindingException:無效的約束聲明

我添加服務通過@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] 
+0

我沒有改變任何其他文件,除了我展示給你的。 –

+0

錯誤提示您缺少 –

回答

0

是否有任何特定的原因爲您的實施?

錯誤建議你缺乏這通常是通過以下方式之一做了映射器:

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> 
+0

CityMapper不是一個接口,它是一個普通的類。 –

+0

就像我所說的,我所說的是mybatis的一個更常見的用法,如果您更喜歡普通的課程方式,請閱讀http://www.mybatis.org/spring/sqlsession.html –

0

這是mybatis-spring-boot-starter中的一個錯誤。你的界面CityMapper被自動註冊爲一個映射器,它不應該。 1.1.0修復了這個問題。

0

就我而言,我生成器的使用mapper.xml MyBatis的發電機,

和點是,我這個文件移動到另一個包: com.xxx.cust.mapper到com.xxx.frame .mapper,所以<mapper namespace="com.xxx.cust.mapper.XXXMapper">是錯誤的。

0

在我而言,這是完全以不同 我有2個罐子和錯誤都JARS有相同的XML文件在同一個命名空間

這使問題來來去去,因爲錯誤是依賴於順序加載tomcat內的罐子