Spring之xml配置方式快速整合MongoDB

Spring之xml配置方式快速整合MongoDB

文章目录

  • Spring之xml配置方式快速整合MongoDB
  • [1. MongoDB](#1. MongoDB)
    • [1. 引依赖](#1. 引依赖)
    • [2. application.properties](#2. application.properties)
    • [3. application-context-mongodb.xml](#3. application-context-mongodb.xml)

1. MongoDB

MongoDB 版本: 4.2.21

1. 引依赖

xml 复制代码
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>3.4.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-jpa</artifactId>
    <version>2.7.2</version>
</dependency>

2. application.properties

properties 复制代码
mongo.username=test
mongo.password=test
mongo.host=127.0.0.1
mongo.port=27017
mongo.dbname=test_doc
mongo.authenticationDbname=admin
mongo.authMechanism=SCRAM-SHA-1

3. application-context-mongodb.xml

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context.xsd
          http://www.springframework.org/schema/data/mongo
          http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- 有认证的配置 -->
	<mongo:db-factory id="mongoDbFactory" client-uri="mongodb://${mongo.username}:${mongo.password}@${mongo.host}:${mongo.port}/${mongo.dbname}?authSource=${mongo.authenticationDbname}&amp;authMechanism=${mongo.authMechanism}"/>

	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
	</bean>
	
	<!-- 自动扫描 -->
	<mongo:mapping-converter base-package="com.yuan.mongo.page.*" />
	
	<context:annotation-config />
	
	<!-- 自定义注解类 -->
	<context:component-scan base-package="com.yuan.mongo,caom.yuan.utils" />
	
	<bean class="org.springframework.data.mongodb.core.mapping.event.LoggingEventListener" />
	
</beans>
相关推荐
程序猿进阶11 分钟前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot
你的微笑,乱了夏天9 小时前
linux centos 7 安装 mongodb7
数据库·mongodb
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭9 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
李小白6610 小时前
Spring MVC(上)
java·spring·mvc
GoodStudyAndDayDayUp10 小时前
IDEA能够从mapper跳转到xml的插件
xml·java·intellij-idea
见欢.11 小时前
XXE靶场
xml
Lojarro12 小时前
【Spring】Spring框架之-AOP
java·mysql·spring
zjw_rp13 小时前
Spring-AOP
java·后端·spring·spring-aop
撒呼呼16 小时前
# 起步专用 - 哔哩哔哩全模块超还原设计!(内含接口文档、数据库设计)
数据库·spring boot·spring·mvc·springboot