Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-boot-mongodb
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cemarose
spring-boot-mongodb
Commits
70eb2a20
Commit
70eb2a20
authored
Jul 16, 2018
by
jwrwst
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化
parent
46963fe2
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
10 deletions
+28
-10
MongoConfig.java
src/main/java/com/cemarose/mongodb/config/MongoConfig.java
+28
-10
No files found.
src/main/java/com/cemarose/mongodb/config/MongoConfig.java
View file @
70eb2a20
package
com
.
cemarose
.
mongodb
.
config
;
package
com
.
cemarose
.
mongodb
.
config
;
import
com.mongodb.*
;
import
com.mongodb.*
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.parsing.Location
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.mongodb.MongoDbFactory
;
import
org.springframework.data.mongodb.MongoDbFactory
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.data.mongodb.core.SimpleMongoDbFactory
;
import
org.springframework.data.mongodb.core.SimpleMongoDbFactory
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
@Component
@Component
@ConfigurationProperties
(
prefix
=
"spring.data.mongodb"
)
public
class
MongoConfig
{
public
class
MongoConfig
{
private
String
host
;
private
Integer
port
;
private
String
uri
;
/**
/**
* 初始化mongodb客户端
* 初始化mongodb客户端
...
@@ -17,7 +23,7 @@ public class MongoConfig {
...
@@ -17,7 +23,7 @@ public class MongoConfig {
*/
*/
@Bean
@Bean
public
MongoClient
client
()
{
public
MongoClient
client
()
{
return
new
MongoClient
(
new
ServerAddress
(
"127.0.0.1"
,
27017
));
return
new
MongoClient
(
new
ServerAddress
(
host
,
port
));
}
}
/**
/**
...
@@ -27,7 +33,7 @@ public class MongoConfig {
...
@@ -27,7 +33,7 @@ public class MongoConfig {
*/
*/
@Bean
@Bean
public
MongoDbFactory
mongoDbFactory
()
throws
Exception
{
public
MongoDbFactory
mongoDbFactory
()
throws
Exception
{
String
database
=
new
MongoClientURI
(
"mongodb://localhost/dbtest"
).
getDatabase
();
String
database
=
new
MongoClientURI
(
uri
).
getDatabase
();
return
new
SimpleMongoDbFactory
(
client
(),
database
);
return
new
SimpleMongoDbFactory
(
client
(),
database
);
}
}
...
@@ -41,13 +47,25 @@ public class MongoConfig {
...
@@ -41,13 +47,25 @@ public class MongoConfig {
return
new
MongoTemplate
(
mongoDbFactory
);
return
new
MongoTemplate
(
mongoDbFactory
);
}
}
/**
* test
*/
public
void
example
(
MongoDbFactory
mongoDbFactory
)
{
DB
db
=
mongoDbFactory
.
getDb
();
DBCollection
collection
=
db
.
getCollection
(
"users"
);
//getter and setter
public
String
getHost
()
{
return
host
;
}
public
void
setHost
(
String
host
)
{
this
.
host
=
host
;
}
public
Integer
getPort
()
{
return
port
;
}
public
void
setPort
(
Integer
port
)
{
this
.
port
=
port
;
}
public
String
getUri
()
{
return
uri
;
}
public
void
setUri
(
String
uri
)
{
this
.
uri
=
uri
;
}
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment