Search code examples
javaspring-bootmybatisspring-mybatis

Mapper file is not known to the MapperRegistry


I am trying to show all the data in mysql database with mybatis on Spring Boot. However, the mapper.xml file is not known to the MapperRegistry.

I have tried changing the classpath of application.yaml.

application.yaml

mybatis:
  mapper-locations: classpath:com/example/demo/repository/mybatis/*.xml

Shop Class

public class Shop {
    private String shopId;

    private String shopName;

    public Shop() {
        }

    public Shop(String shopId, String shopName) {
        this.shopId = shopId;
        this.shopName = shopName;
    }
}

ShopMapper.xml

    <!-- Mapping Shop Class to Shop tables -->
    <resultMap id="Shop"
               type="com.example.demo.domain.Shop">
        <id property="shopId" column="SHOP_ID"/>
        <result property="shopName" column="SHOP_NAME"/>
    </resultMap>


    <!-- Show all shops with the designated shopid -->
    <select id="get" resultMap="Shop">
        SELECT SHOP_ID, SHOP_NAME FROM SHOP
        WHERE SHOP_ID = #{shopId}
    </select>

</mapper>

ShopRepository

public Shop findOne(String shopId) {
    Shop shop = this.sqlSessionTemplate.getMapper(ShopMapper.class).get(shopId);
   return shop;
}

Controller

@RestController
    public class PageController {
    @GetMapping(path = "/{shopId}", produces = "application/json")
    public Shop get(@PathVariable String shopId) {
        return this.service.get(shopId);
    }
}

Error: org.apache.ibatis.binding.BindingException: Type interface com.example.demo.repository.mybatis.ShopMapper is not known to the MapperRegistry


Solution

  • you need add a interface to match the ShopMapper.xml

    @Mapper
    public interface ShopMapper {
        Shop get(Long shopId);
    }