我正在尝试将Spring Data JDBC与MyBatis一起使用。我对使用CrudRepository提供的开箱即用的CRUD方法以及编写在MyBatis XML文件中定义的其他自定义查询感兴趣。
通过阅读jdbc.mybatis的说明,听起来我需要做的只是创建一个mapper
来实现我想要的方法(遵循“语句的名称是通过将实体类型的完全限定名与mapper连接来构造的”这一规则),并向我的CrudRepository
添加一个方法签名。我已经这样做了,但Spring Data似乎永远找不到我的方法,并给出了以下错误:
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
我也遵循了mybatis-spring-boot-sample中的示例,但没有太多的内容可供我继续使用。
问题是:
MyBatisContext
?我的CrudRepository
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);
}
mybatis:
mapper-locations: classpath:/mapper/**/*.xml
configuration:
map-underscore-to-camel-case: true
<?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>
您链接到的部分的开头是:
对于crudrepository
中的每个操作,Spring Data JDBC运行多个语句。如果应用程序上下文中有SQLSessionFactory
,Spring数据在每个步骤中检查SessionFactory是否提供语句。如果找到一个,则使用该语句(包括其配置到实体的映射)。
这只适用于crudrepository
中的方法。其他方法不是这样工作的。
New in Django 1.7. Django为过滤提供了大量的内建的查找(例如,exact和icontains)。这篇文档阐述了如何编写自定义查找,以及如何修改现存查找的功能。关于查找的API参考,详见查找API参考。 一个简单的查找示例 让我们从一个简单的自定义查找开始。我们会编写一个自定义查找ne,提供和exact相反的功能。Author.objects.filter(name__ne
我正在尝试实现一个自定义的 Spring 存储库。我有界面: 其中RataEntity类是 它的实施 和以下存储库类 当我运行我的应用程序时,我得到这个错误: 未能创建方法公共摘要的查询java.util.Listit.aubay.PreliosPAN.repositories.RataRepositoryCustom.getRateFromTipo频率zaRimborso(java.lang.字
错误: 棱角版本:
嗨,我正在尝试学习hashcode()和equals()方法的目的。我尝试了以下程序。 输出: 我有两个疑问: 1) 我认为HashMap将包含一个条目,因为两个对象(ob1和ob2)的hascode是相同的。有人能解释为什么HashMap中有两个条目吗? 2)为什么返回false?
Maven告诉我:您的过滤器与任何原型都不匹配。我的系统: null 我已采取的步骤是: Create my project Navigate to my project in command prompt and run the command: cd返回到“我的项目”文件夹并运行命令 运行该命令后,我会看到:
它找不到我添加到库中的费率栏库。 下面是我如何将我的库添加到应用程序中的 根级文件 依赖关系{...实现“xxx.xxx.xxxx:xxxx:1.0.6”...}