Mongodb Gridfs实现图片存储解决方案 发表于 2017-08-11 | 阅读次数 | 字数统计: 380(字) | 阅读时长: 2(分) 引入Maven依赖 mongodb 3.x 与2.x版本有很大不同 这里选用2.x版本 12345678910<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.13.0-rc0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.7.1.RELEASE</version> </dependency> mongoDB集成spring123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 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 "> <context:property-placeholder location="classpath*:conf/mongo.properties"/> <mongo:mongo-client host="${mongo.host}" port="${mongo.port}" id="mongo"> <mongo:client-options write-concern="SAFE" <!--有密码时为SAFE--> connections-per-host="${mongo.connectionsPerHost}" min-connections-per-host="${mongo.minConnectionsPerHost}" threads-allowed-to-block-for-connection-multiplier="${mongo.threadsAllowedToBlockForConnectionMultiplier}" connect-timeout="${mongo.connectTimeout}" max-wait-time="${mongo.maxWaitTime}" socket-keep-alive="${mongo.socketKeepAlive}" socket-timeout="${mongo.socketTimeout}" description="${mongo.description}" max-connection-idle-time="${mongo.maxConnectionIdleTime}" max-connection-life-time="${mongo.maxConnectionLifeTime}" heartbeat-socket-timeout="${mongo.heartbeatSocketTimeout}" heartbeat-connect-timeout="${mongo.heartbeatConnectTimeout}" min-heartbeat-frequency="${mongo.minHeartbeatFrequency}" heartbeat-frequency="${mongo.heartbeatFrequency}" /> </mongo:mongo-client> <!--</mongo:mongo>--> <!-- 设置使用的数据库 名--> <mongo:db-factory dbname="${mongo.defaultDbName}" mongo-ref="mongo" username="${mongo.user}" password="${mongo.pwd}"/> <!-- mongodb的模板 --> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/> </bean></beans> 属性文件12345678910111213141516171819202122232425#mongo.host=127.0.0.1mongo.port=27017mongo.defaultDbName=mongo_dbmongo.user=rootmongo.pwd=mypasswordmongo.connectionsPerHost=10mongo.threadsAllowedToBlockForConnectionMultiplier=5mongo.minConnectionsPerHost=5#连接超时时间mongo.connectTimeout=10000#等待时间mongo.maxWaitTime=120000#Socket超时时间mongo.socketTimeout=0mongo.socketKeepAlive=truemongo.description=bybon pic mongodbmongo.maxConnectionIdleTime=1500mongo.maxConnectionLifeTime=0#mongo slavemongo.heartbeatSocketTimeout=1000mongo.heartbeatConnectTimeout=1500mongo.minHeartbeatFrequency=5mongo.heartbeatFrequency=10 #待续 关注我的微信,共同分享与讨论! 关注 关注我