【开源Mongdb驱动】SpringBoot+Mybatis+Mongdb融合使用教程

2023-12-14 20:32:56

#【开源Mongdb驱动】SpringBoot+Mybatis+Mongdb无缝融合使用教程

介绍

本文介绍一款基于JAVA开源的mongodb jdbc驱动为基础的无缝与springboot+mybatis融合使用案例

开源地址

https://gitee.com/bgong/jdbc-mongodb-driver

代码操作指南

meavn pom文件依赖

<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.mongo.jdbc.example</groupId>
  <artifactId>mongo-jdbc-springboot-example</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
   <properties>
          <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
          <spring-nacos.version>2.2.8.RELEASE</spring-nacos.version>
          <gson.version>2.9.0</gson.version>
          <spring-cloud.version>2020.0.2</spring-cloud.version>
  
  </properties>
  
   <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/>
    </parent>
  
  <dependencies>
  
  <!-- 核心包  start   --->
  <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver</artifactId>
        <version>3.11.2</version>
    </dependency>
    
      <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-core</artifactId>
        <version>3.11.2</version>
    </dependency>

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>bson</artifactId>
        <version>3.11.2</version>
    </dependency>
    
    		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.2</version>
		</dependency>
    
  <dependency>
     <groupId>com.mongo.jdbc</groupId>
  <artifactId>jdbc-mongodb-driver</artifactId>
  <version>1.0.0</version>
      <scope>system</scope>
       <systemPath>${project.basedir}/src/main/resources/libs/mongo-connector-java-1.0.0.jar</systemPath>
</dependency>
  
       <!-- 核心包  end   --->

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            
            
             <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
  
         <!-- 热部署  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>provided</scope>
        </dependency>
        
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>${gson.version}</version>
        </dependency>
        
          <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
        </dependency>
        
         <dependency>
    		<groupId>commons-lang</groupId>
   	 		<artifactId>commons-lang</artifactId>
   	 		    <version>2.6</version>
   	 		
		</dependency>
          
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        
        
             <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
           
        </dependency>
  
  
  </dependencies>
  
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
  
   <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                        <exclude>**/*.properties</exclude>
                </excludes>
            </resource>
          </resources>
        <plugins>
        
       
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <!--<source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>-->
                    <includeSystemScope>true</includeSystemScope>
                    <executable>true</executable>
                    <fork>true</fork>
                </configuration>
                <version>2.7.0</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>



application-dev.properties 配置


spring.datasource.driverClassName=com.mongo.jdbc.Driver
spring.datasource.url=jdbc:mongo://10.28.55.226:27017/demo_db?isSSL=false
spring.datasource.username=
spring.datasource.password=
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration,org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration
spring.datasource.initial-size=2

JDBC连接串说明

例如:jdbc:mongo://localhost:27017/db_name?isSSL=false
jdbc:mongo://为固定标示,标示为mongodb jdbc 驱动连接串
localhost:27017 :连接的主机和短裤
db_name :为数据库名称
isSSL=false :mongodb连接配置参数,如有多配置参加可以在后面用&keyxx=valuex 追加

详细源码下载

在这里插入图片描述

文章来源:https://blog.csdn.net/gongbing798930123/article/details/135003268
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。