Spring boot 带MyBatis的Spring数据JDBC未找到自定义查询

Spring boot 带MyBatis的Spring数据JDBC未找到自定义查询,spring-boot,mybatis,spring-mybatis,spring-data-jdbc,Spring Boot,Mybatis,Spring Mybatis,Spring Data Jdbc,我试图将Spring数据JDBC与MyBatis结合使用。我对使用Crudepository提供的现成CRUD方法以及编写MyBatis XML文件中定义的其他自定义查询感兴趣 从阅读其中的说明来看,我所需要做的似乎就是创建一个实现我想要的方法的Mapper(遵循规则“语句的名称是通过将实体类型的完全限定名与Mapper连接而构造的”)并将方法签名添加到我的crudepository。我已经这样做了,但Spring数据似乎从未找到我的方法,并给出以下错误: org.springframework

我试图将Spring数据JDBC与MyBatis结合使用。我对使用Crudepository提供的现成CRUD方法以及编写MyBatis XML文件中定义的其他自定义查询感兴趣

从阅读其中的说明来看,我所需要做的似乎就是创建一个实现我想要的方法的
Mapper
(遵循规则“语句的名称是通过将实体类型的完全限定名与Mapper连接而构造的”)并将方法签名添加到我的
crudepository
。我已经这样做了,但Spring数据似乎从未找到我的方法,并给出以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'fooApplication': Invocation of init method failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: No query specified on findByChangeOwnerId; nested exception is java.lang.IllegalStateException: No query specified on findByChangeOwnerId
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.3.RELEASE.jar:2.1.3.RELEASE]
    at com.xxx.fooApplication.main(fooApplication.java:42) [classes/:na]
Caused by: org.springframework.dao.InvalidDataAccessApiUsageException: No query specified on findByChangeOwnerId; nested exception is java.lang.IllegalStateException: No query specified on findByChangeOwnerId
我也遵循了来自的例子,但是对于我来说没有太多的东西可以继续下去

问题是:

  • 我可以在Crudepository中添加额外的方法来调用MyBatis吗?如果是的话,你能解释我在下面遗漏了什么吗
  • 我假设对于任何附加方法,我都可以传入我想要的任何参数,而不是总是必须传入
    MyBatisContext
  • 我的积垢

    package com.xxx.repository.jdbc;
    
    import com.xxx.model.Foo;
    import org.springframework.data.repository.CrudRepository;
    import org.springframework.data.repository.query.Param;
    
    import java.util.List;
    
    public interface FooBmtRepository extends CrudRepository<Foo, String> {  
      List<Foo> findByChangeOwnerId(@Param("id") String id);
    }
    
    映射器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="com.xxx.model.ItsmMapper">
        <!--<select id="findByChangeOwnerId" resultType="com.xxx.model.Itsm" parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext">-->
        <!--where change_owner_id=#{changeOwnerId}-->
        <select id="findByChangeOwnerId" resultMap="itsmResultMap" parameterType="String">
            select *
            from [myschema].[tbl_itsms_bmt]
            where change_owner_id=#{id}
        </select>
    
        <resultMap id="itsmResultMap" type="com.xxx.model.Itsm">
            <result property="changeNumber" column="change_number"/>
            <result property="itsmUrl" column="itsm_url"/>
            <result property="changeTitle" column="change_title"/>
            <result property="category" column="category"/>
            <result property="status" column="status"/>
            <result property="createdDate" column="created_date"/>
        </resultMap>
    
    </mapper>
    

    链接到的部分的开头为:

    对于
    crudepository
    中的每个操作,Spring数据JDBC都会运行多个语句。如果应用程序上下文中有一个
    SqlSessionFactory
    ,Spring数据会检查每个步骤SessionFactory是否提供语句。如果找到一个,则使用该语句(包括其到实体的配置映射)

    这仅适用于
    crudepository
    中的方法。 其他方法不是这样工作的

    您可以实现一个完整的自定义方法实现,调用MyBatis


    或者,您也可以为MyBatis支持的命名查询创建一个问题,甚至可能创建一个PR。

    我已经阅读了您所注意到的内容,但不确定我是否正确解释了该声明-感谢您的澄清。现在我已经打开了一个增强请求。谢谢如果您知道如何改进文档以使其更清晰,我也将不胜感激。
    <?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="com.xxx.model.ItsmMapper">
        <!--<select id="findByChangeOwnerId" resultType="com.xxx.model.Itsm" parameterType="org.springframework.data.jdbc.mybatis.MyBatisContext">-->
        <!--where change_owner_id=#{changeOwnerId}-->
        <select id="findByChangeOwnerId" resultMap="itsmResultMap" parameterType="String">
            select *
            from [myschema].[tbl_itsms_bmt]
            where change_owner_id=#{id}
        </select>
    
        <resultMap id="itsmResultMap" type="com.xxx.model.Itsm">
            <result property="changeNumber" column="change_number"/>
            <result property="itsmUrl" column="itsm_url"/>
            <result property="changeTitle" column="change_title"/>
            <result property="category" column="category"/>
            <result property="status" column="status"/>
            <result property="createdDate" column="created_date"/>
        </resultMap>
    
    </mapper>
    
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jdbc</artifactId>
        <version>1.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>2.1.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.1.6.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
        <version>2.1.3.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-autoconfigure</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>2.0.0</version>
    </dependency>