springboot使用jpa+mongodb时,xxxRepository不能Autowired的问题

springboot启动类:

@SpringBootApplication
public class MainApp {

    public static void main(String[] args) {

        SpringApplication.run(MainApp.class, args);
    }
}

jpa数据库操作类:

public interface UserDao extends MongoRepository<User, String> {
}

单元测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(MainApp.class)
public class UserDaoTest {
    @Autowired
    private UserDao userDao;

    @Autowired
    private ObjectMapper objectMapper;

    @Test
    public void add() throws JsonProcessingException {
        for(int i = 1; i <= 5; i++) {
            User user = new User();
            user.setName("测试" + i);
            user.setSex((i%2 == 0) ? 0 : 1);
            user.setPhone("1511111000" + i);
            user.setCreateTime(new Date());
            User save = userDao.save(user);
            System.out.println("保存结果:" + objectMapper.writeValueAsString(save));
        }
    }
}

执行结果:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘UserDaoTest‘: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.mlxs.springboot04.mongodb.dao.UserDao UserDaoTest.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.mlxs.springboot04.mongodb.dao.UserDao] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

解决方法:

在springboot启动类中添加@EnableMongoRepositories注解,标注mongoRepository类的路径

@SpringBootApplication
@EnableMongoRepositories(basePackages = {"com.mlxs.springboot04.mongodb.dao"})
public class MainApp {

    public static void main(String[] args) {

        SpringApplication.run(MainApp.class, args);
    }
}
时间: 2024-10-06 01:02:37

springboot使用jpa+mongodb时,xxxRepository不能Autowired的问题的相关文章

用SpringBoot+MySql+JPA实现对数据库的增删改查和分页

使用SpringBoot+Mysql+JPA实现对数据库的增删改查和分页      JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中.  使用Springboot和jpa对数据库进行操作时,能够大大减少我们的工作量,在jpa中,已经在底层封装好了增删查的功能和sql语句,可以使我们进行快速开发 项目流程 一.新建一个项目 二.配置文件 #数据源配置 spring.dat

带你搭一个SpringBoot+SpringData JPA的环境

前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://github.com/ZhongFuCheng3y/3y 不知道大家对SpringBoot和Spring Data JPA了解多少,如果你已经学过Spring和Hibernate的话,那么SpringBoot和SpringData JPA可以分分钟上手的.SpringBoot和SpringData JPA的好处我就不说了,当时我学习的时候也粗略做过笔记,有兴趣的同学可以去看看 SpringBoot就是这么简

Springboot 使用JPA

Springboot 使用jpa maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId>

springboot之jpa的支持

1.springboot之jpa支持 2.Springboot+bootstrap界面版之增删改查及图片上传 springboot之jpa支持 导入相关pom依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> application.

springboot对JPA的支持

目标 1.springboot之jpa支持 2.Springboot+bootstrap界面版之增删改查及图片上传 spring boot之jpa支持 导入pom依赖 1 <dependency> 2 <groupId>org.springframework.boot</groupId> 3 <artifactId>spring-boot-starter-data-jpa</artifactId> 4 </dependency> 配置

【SpringBoot】SpringBoot 与JPA整合(十四)

本章介绍SpringBoot与JPA整合 整合流程 1.新建一个SpringBoot Web项目 1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:s

远程连接mongodb时,27017端口连接不上的解决办法

一.背景描述: 我在linux  RED7上安装了mongodb,并没有修改mongodb的配置文件.然后通过另外一台电脑用pymongo连接mongodb时,报错:timeout. ping IP 是成功的. telnet IP 27017 的时候,提示:27017端口连接不上. 二.解决过程: 各种百度,远程连接mongodb失败,网上资料显示原因有两个: 1.mongodb的配置文件中的bind_ip 默认为127.0.0.1,默认只有本机可以连接.  此时,需要将bind_ip配置为0.

启动MongoDB时,提示:error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory

启动MongoDB时,提示: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory [[email protected] ~]# /usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data --logpath /usr/local/mongodb/logs/mon

springboot使用jpa,删除功能sql报错解决

springboot使用jpa时报错: 只有删除时报错了, "data": "删除失败,原因:Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failur