【问题标题】:How to connect spring boot application with using hibernate and mongodb.如何使用 hibernate 和 mongodb 连接 Spring Boot 应用程序。
【发布时间】:2018-09-30 23:41:30
【问题描述】:

我已经使用 Spring boot、hibernate 和 mysql 开发了一个网站。但现在我想连接 MongoDB(不使用 mysql)。哪些 Maven 依赖项需要这样做???其次,我想知道如何将 mongodb 连接到我的配置。

这是我的 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>lk.slsi</groupId>
    <artifactId>tradeportal</artifactId>
    <packaging>war</packaging>
    <version>slsi-1.1.0-SNAPSHOT</version>
    <name>tradeportal Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.1.RELEASE</version>
    </parent>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <sonar.exclusions>**/public/**/*</sonar.exclusions>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>

        <!--handle servlet-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <!--<Email Dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
            <version>1.4.3.RELEASE</version>      
        </dependency>
        <!--Add mysql dependency-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.1</version>
        </dependency>
        <!--jasper-->
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>3.7.6</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>javax.mail-api</artifactId>
            <version>1.5.5</version>
            <type>jar</type>
        </dependency>
        <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web-services -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web-services</artifactId>
            <version>1.5.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.ws.security/wss4j -->
        <dependency>
            <groupId>org.apache.ws.security</groupId>
            <artifactId>wss4j</artifactId>
            <version>1.6.19</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-libs -->
        <dependency>
            <groupId>com.sun.xml.bind</groupId>
            <artifactId>jaxb-libs</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-security</artifactId>
            <version>2.2.0.RELEASE</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <finalName>tradeportal</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <useSystemClassLoader>false</useSystemClassLoader>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.2</version>
                <configuration>
                    <tagNameFormat>@{project.version}</tagNameFormat>
                    <autoVersionSubmodules>true</autoVersionSubmodules>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的应用程序配置

spring:
  main:
    web-environment: true
    banner-mode: console
  jpa:
    database-platform: org.hibernate.dialect.MySQL5Dialect
    show-sql: true
    hibernate:
      ddl-auto: create
  application:
    name: tradeportal
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/trade_portal_db?createDatabaseIfNotExist=true&useSSL=false
    username: abc
    password: ****

  http:
    multipart:
      max-file-size: 5MB
      max-request-size: 25MB
server:
  port: 8080
  context-path: /tradeportal
tradeportalgov:
  reportData: /reports/images
  # if file path is given relative to the webapp base use INTERNAL if the absolute path is given to baseLocation use EXTERNAL
  # EXTERNAL file download is not implemented
  filePathType : INTERNAL
  filePaths:
    baseLocation: uploads
    vat: va

这是我的状态

package lk.trad;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import javax.annotation.PreDestroy;

/**
 * Created by ignotus on 1/22/2017.
 */
@SpringBootApplication
@ComponentScan(basePackages = "lk.tradeportal")
@EnableWebMvc
@ImportResource(locations = "classpath:tradeportal-servlet-config.xml")
public class TRADEPORTALStarter extends SpringBootServletInitializer {

    private static final Logger slsiLogger = LogManager.getLogger(TRADEPORTALStarter.class);

    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        slsiLogger.info("Starting application");
        SpringApplication application = new SpringApplication(TRADEPORTALStarter.class);
        context = application.run(args);
        application.setRegisterShutdownHook(true);
    }

    @PreDestroy
    private static void closeAppContext(){
        context.close();
    }

    @Override
    protected final SpringApplicationBuilder configure(final SpringApplicationBuilder application) {
        return application.sources(TRADEPORTALStarter.class);
    }

}

我该怎么做。

【问题讨论】:

  • 您不能将 MongoDB 与 Hibernate 一起使用。 Hibernate 是一个 JPA 提供程序,因此它们只能在 JDBC API 之上工作,因此只能在 SQL 库上工作。但是,如果您使用 spring-boot-starter-data-mongodb,则可以将 MongoDB 与 Spring Data 一起使用。但是,从您的问题来看,您并不清楚您实际上在问什么。
  • @g00glen00b 我想知道如何将 mongodb 连接到我现有的项目...你确定 hibernate 不适用于 mongodb
  • @ShanuGupta 我看到了那个页面...我想知道 mongo db 的驱动程序类名称是什么...我把 mongodb.jdbc.MongoDriver...但是它解决了错误跨度>

标签: java mongodb hibernate spring-boot nosql


【解决方案1】:

你需要添加2个依赖“spring-boot-starter-data-mongodb”和“spring-boot-starter-data-mongodb-reactive”。

要访问 Mongo 数据库,您可以注入一个自动配置的 org.springframework.data.mongodb.MongoDbFactory。默认情况下,实例会尝试连接到位于 mongodb://localhost/test 的 MongoDB 服务器。以下示例显示了如何连接到 MongoDB 数据库:

import org.springframework.data.mongodb.MongoDbFactory;
import com.mongodb.DB;

@Component
public class MyBean {

    private final MongoDbFactory mongo;

    @Autowired
    public MyBean(MongoDbFactory mongo) {
        this.mongo = mongo;
    }

    // ...

    public void example() {
        DB db = mongo.getDb();
        // ...
    }

}

您可以通过设置spring.data.mongodb.uri属性来更改URL并配置副本集等附加设置,如下例所示:

spring.data.mongodb.uri=mongodb://user:secret@mongo1.example.com:12345,mongo2.example.com:23456/test 或者,只要您使用 Mongo 2.x,您就可以指定主机/端口。例如,您可以在 application.properties 中声明以下设置:

spring.data.mongodb.host=mongoserver spring.data.mongodb.port=27017

要将spring数据连接到mangodb,您可以使用@Entity注解并为Page或Object提出一个简单的spring数据请求。

import org.springframework.data.domain.*;
import org.springframework.data.repository.*;

public interface CityRepository extends Repository<City, Long> {

    Page<City> findAll(Pageable pageable);

    City findByNameAndCountryAllIgnoringCase(String name, String country);

}  

【讨论】:

  • 我应该把你的绳子放在我的项目中的什么地方
  • 你的第一句话有误。你不需要spring-boot-starter-data-mongodbspring-boot-starter-data-mongodb-reactive。您需要选择其中之一(取决于您是否需要反应式流)。
  • 好的...我可以在 mongodb 中使用 hibernate
  • 当然,对于jpa所有的数据库都是一样的,你只需要分配方言和url,或者你可以通过spring数据更容易,你配置配置文件然后你开始.祝你好运
猜你喜欢
  • 2017-10-26
  • 2017-07-10
  • 2019-11-02
  • 2020-03-22
  • 2020-04-07
  • 2019-03-29
  • 1970-01-01
  • 2015-05-31
  • 2020-01-22
相关资源
最近更新 更多