spring boot jpa和mongodb怎么联合使用

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot jpa和mongodb怎么联合使用相关的知识,希望对你有一定的参考价值。

参考技术A springboot连接mongodb方法: pom.xml <project xmlns="" xmlns:xsi="" xsi:schemaLocation=" "> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>myproject</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>myproject</name> <url></url> <!-- 要继承的项目 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.2.7.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>log4j-over-slf4j</artifactId> <groupId>org.slf4j</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-MongoDB</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-snapshots</id> <url></url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <url></url> </repository> </repositories> </project> package com.example.myproject.dao; import Java.util.List; import com.mongodb.DBObject; public interface UserDao long count(); List<DBObject> find(); package com.example.myproject.dao.impl; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import com.example.myproject.dao.UserDao; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.DBObject; import com.mongodb.MongoClient; @Repository public class UserDaoImpl implements UserDao @Autowired MongoClient client; DBCollection dbCollection; //初始化方法 @PostConstruct public void init() DB db = client.getDB("test"); dbCollection = db.getCollection("users"); public long count() return dbCollection.count(); public List<DBObject> find() DBCursor it = dbCollection.find(); List<DBObject> result=new ArrayList<DBObject>(); while(it.hasNext()) DBObject object = it.next(); result.add(object); return result; 测试类: @RestController @EnableAutoConfiguration @ComponentScan public class Example @Autowired ConnectionSettings connection; @Autowired UserDaoImpl userDao; private Logger logger = Logger.getLogger(Example.class); @RequestMapping("/") String home() throws UnknownHostException long count = userDao.count(); System.out.println(count); List<DBObject> find = userDao.find(); for(int i=0;i<find.size();i++) DBObject DBObject = find.get(i); System.out.println(DBObject); logger.info(connection.getName() + "--" + connection.getAge()); return connection.getName() + connection.getAge() + connection.getSex(); public static void main(String[] args) throws Exception SpringApplication.run(Example.class, args);

Spring Boot 整合 mongoDB 利用 JPA 方式交互

参考技术A compile'org.springframework.boot:spring-boot-starter-data-mongodb'

spring.data.mongodb.uri=mongodb://username:password@host:port/database

3.1  定义与 mongoDB 交互的Entity

@Document 定义 mongoDB 文档名称

@Indexed 创建索引

    unique = true 创建唯一索引

    background = true 后台创建索引

    expireAfterSeconds = 10 * 60 设置600秒过期时间(默认秒,到期便会自动删除该条数据)

3.2 创建 Repository 层 (继承 MongoRepository 类)

3.3 具体 Service 实现

以上是关于spring boot jpa和mongodb怎么联合使用的主要内容,如果未能解决你的问题,请参考以下文章

无法将 Spring Data MongoDB + Spring Data JPA 与 Spring Boot 一起使用

Spring Boot MongoDB JPA 简化开发

在Spring Boot应用程序中的模型类中同时使用JPA和MongoDB注释的问题

使用 Spring Boot 和 mongodb 运行 camunda

Spring boot整合Mongodb

spring boot jpa 配置了yml文件后怎么扫描包