1
votes

mybatis-spring-boot-sample

I add a service to get CityMapper by @Autowired, but it failed. I changed something as the following shows:

@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));
    }

}

add a service :

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

and it's implementation:

@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;
    }

}

and the CityMapper :

@Repository
public class CityMapper {

    @Autowired
    private SqlSessionTemplate sqlSessionTemplate;

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

}

and the Error shows:

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]
5
and I didn't change any other file except what I show to you. - Jarvis
The error suggests that you are lacking of - Ian Lim

5 Answers

0
votes

Is there any particular reason for your implementation?

The error suggest that you are lacking of a mapper which is normally done in one of the following ways:

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

Or

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

with

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
votes

This is a bug in mybatis-spring-boot-starter. Your interface CityMapper was auto-registered as a mapper and it should not. 1.1.0 fixes this.

0
votes

In my case,I generator a mapper.xml using mybatis-generator,

and the point is, I move this file to another package: com.xxx.cust.mapper to com.xxx.frame.mapper,so <mapper namespace="com.xxx.cust.mapper.XXXMapper"> is wrong.

0
votes

In my case this was totaly different i had 2 JARS and by mistake both JARS had the same XML files in the same namespace

This made the problem come and go , since the error was dependent on the order of loading of the jars inside tomcat

0
votes

I use this method ,can pass compile 。so the mapper and SQL.xml binding problem is a problem

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