#【开源Mongdb驱动】SpringBoot+Mybatis+Mongdb无缝融合使用教程
介绍
本文介绍一款基于JAVA开源的mongodb jdbc驱动为基础的无缝与springboot+mybatis融合使用案例
mongodb JDBC 使用案例
https://blog.csdn.net/gongbing798930123/article/details/135002530
《基于开源的JAVA mongodb jdbc 驱动 使用教程》
功能价值
- 与mybaits融合:复用mybatis的功能特性,如:缓存,if动态判断标签等特性
- 开发更加便捷: 统一dao层,使代码更具维护性,同时操作mongodb 便捷
- 产品融合能力: 作为底层组件,可以与BI、低代码平台等需要JDBC特性产品融合能力
开源地址
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 追加
Dao层
package com.mongo.jdbc.example.dao;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import com.mongo.jdbc.example.entity.UserEntity;
/**
*
* @author:binggong
* @createtime: 2023年12月10日 下午7:18:22
* @version:V.0.1
* @tag:
*
*/
@Mapper
public interface IUserDao {
/**
* 写入数据
* @param data
* @throws Exception
*/
public void addData(UserEntity data) throws Exception;
/**
* 查询数据
* @param dname
* @return
* @throws Exception
*/
public Map getData(String dname) throws Exception;
}
Mybatis Mapper文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mongo.jdbc.example.dao.IUserDao">
<insert id="addData" parameterType="com.mongo.jdbc.example.entity.UserEntity">
db.tb_user.insert({"deptno":#{deptno},"dname":#{dname},"loc":#{loc}})
</insert>
<select id="getData" resultType="java.util.Map" parameterType="java.lang.String">
db.tb_user.find({"dname":#{dname}})
</select>
</mapper>