Java spring数据库中的自定义方法
我需要为spring DataCouchbase存储库编写一个自定义方法。这是我的密码 CBsampleRepositoryCustom.javaJava spring数据库中的自定义方法,java,spring-data,couchbase,spring-data-couchbase,Java,Spring Data,Couchbase,Spring Data Couchbase,我需要为spring DataCouchbase存储库编写一个自定义方法。这是我的密码 CBsampleRepositoryCustom.java public interface CBsampleRepositoryCustom { public void addIndex() ; } CBsampleRepositoryImpl.java public class CBsampleRepositoryImpl implements CBsampleRepositoryCustom { @
public interface CBsampleRepositoryCustom {
public void addIndex() ;
}
CBsampleRepositoryImpl.java
public class CBsampleRepositoryImpl implements CBsampleRepositoryCustom {
@Override
public void addIndex() {
System.out.println("CBsampleRepositoryCustomImpl createIndex");
}
}
CBsampleRepository.java
@Repository
public interface CBsampleRepository extends CouchbaseRepository<Content,String> , CBsampleRepositoryCustom{
}
Main.java
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class);
repository.addIndex();
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,new CBsampleRepositoryImpl());
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,custom);
repository.addIndex();
}
但在运行时,它显示了一个错误
线程“main”org.springframework.dao.InvalidDataAccessResourceUsageException中出现异常:无法加载设计文档“内容”的视图“addIndex”;嵌套异常为com.couchbase.client.protocol.views.InvalidViewException:无法为设计文档“内容”加载视图“addIndex”
我们需要将实现类对象传递到getRepository函数中 Main.java
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class);
repository.addIndex();
}
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(
CouchBaseBeansConfiguration.class);
CouchbaseTemplate template = context.getBean("couchbaseTemplate",
CouchbaseTemplate.class);
RepositoryFactorySupport factory = new CouchbaseRepositoryFactory(
template);
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,new CBsampleRepositoryImpl());
CBsampleRepository repository = factory.getRepository(CBsampleRepository.class,custom);
repository.addIndex();
}
Spring Data Couchbase文档包括以下内容: 对于快速的高级概述,您可以使用所需的客户方法为您的实现创建一个接口。然后为这些方法创建实现类,其名称与Crudepository接口相同,但后缀为“Impl”;这个后缀很重要。然后,当您创建扩展Spring Crudepository的接口时,您不仅扩展了Crudepository,还扩展了为自定义方法创建的接口。然后,当您以普通方式构建时,Spring应该处理使用您指定的自定义方法生成Crudepository的问题 下面是上面引用的Spring文档中的一个快速示例。首先是自定义界面:
interface UserRepositoryCustom {
public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
// Declare query methods here
}
现在,此接口的实现:
interface UserRepositoryCustom {
public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
// Declare query methods here
}
最后是Crudepository的典型界面,但也扩展了自定义界面:
interface UserRepositoryCustom {
public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
interface UserRepository extends CrudRepository<User, Long>, UserRepositoryCustom {
// Declare query methods here
}
interface UserRepository扩展了crudepository、UserRepositoryCustom{
//在此声明查询方法
}
有关更多详细信息,请参考上面链接的Spring Data Couchbase文档