MapperRegistry不了解Mapper文件

时间:2019-05-16 03:43:09

标签: java spring-boot mybatis spring-mybatis

我正在尝试在Spring Boot上使用mybatis显示mysql数据库中的所有数据。但是,MapperRegistry不知道mapper.xml文件。

我尝试更改application.yaml的类路径。

application.yaml

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

商店类别

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

控制器

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

错误: org.apache.ibatis.binding.BindingException:MapperRegistry不知道类型为com.example.demo.repository.mybatis.ShopMapper的接口

1 个答案:

答案 0 :(得分:0)

您需要添加一个接口以匹配ShopMapper.xml

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