我正在尝试在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>


商店资料库

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:类型接口com.example.demo.repository.mybatis.ShopMapper对于MapperRegistry是未知的

最佳答案

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

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

关于java - MapperRegistry未知Mapper文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56160503/

10-10 11:18