文章目錄
Java操作MongoDB
操作系統:Win10 1803
jdk環境:jdk1.8.201
運行軟件:Idea Ultima Education 2019.1
項目框架:Maven
MongoDB版本:MongoDB 4.0.5
搭建基礎Java Maven項目
1.創建Maven項目
這裏默認大家都會創建maven項目,如果不會搭建的話可以安照自己使用的IDE參考對應的博客
+Idea開發Maven項目
+ Idea配置Maven環境
+ Idea搭建Maven項目
- Eclipse開發Maven項目
2.配置Maven pom.xml
- 導入java連接MongoDB需要的jar包
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.10.1</version> </dependency>
- 爲了方便所以我添加了一些輔助jar包
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.10.1</version> </dependency> <!--1.0.8 輔助工具 --> <dependency> <groupId>com.0opslab</groupId> <artifactId>0opslab-utils</artifactId> <version>1.0.8</version> <exclusions> <exclusion> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.56</version> </dependency> </dependencies> <build> <!-- 放行所有配置文件 --> <resources> <resource> <directory>src/main/java</directory><!--所在的目錄--> <includes><!--包括目錄下的.properties,.xml文件都會掃描到--> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build>
創建MongoDB數據庫連接對象
1.創建一個簡單測試類,來創建一個簡單的數據庫連接
- 數據庫連接核心代碼
//創建一個MongoDB連接對象 MongoClient
MongoClient mongoClient = new MongoClient("127.0.0.1",27017);
//然後創建一個數據庫操作對象通過mongoClient.getDatabase(你的庫名)獲取
MongoDatabase mongoDatabase = mongoClient.getDatabase("test");
- 這裏我創建了一個測試類查看能否連接上MongoDB中的test庫
需要被連接的test數據庫
測試後結果
2.封裝數據庫連接操作
因爲每次要連接數據庫的時候都需要寫這兩行代碼,何不如將這兩行代碼封裝成類並且通過配置文件獲取MongoDB連接字符串,並且將其設置爲單例模式從而提升代碼運行效率呢
- 將數據庫連接操作封裝成一個對象(Connection),並且設爲單例模式
Connection.java
import com.mongodb.MongoClient;
import com.mongodb.client.MongoDatabase;
public class Connection {
private ConnectionProperty connectionProperty;
private static Connection connection;
private MongoClient mongoClient;
private Connection(){
connectionProperty = new ConnectionProperty();
}
public void setConnectionProperty(ConnectionProperty connectionProperty) {
this.connectionProperty = connectionProperty;
}
/**
*
* @return 返回MongoDB數據庫操作連接對象MongoDataBase,爲增加對數據庫的操作安全性所以沒有將此對象設置爲單例對象
* @throws Exception 拋出數據庫連接異常
*/
public MongoDatabase getMongodb() throws Exception {
//創建一個數據庫連接對象
mongoClient = new MongoClient(connectionProperty.getHost(),connectionProperty.getPort());
//獲取數據庫操作對象,並連接數據庫
MongoDatabase mongodb = mongoClient.getDatabase(connectionProperty.getDatabase());
if (mongodb.getName()!=null)
return mongodb;
else
throw new Exception("數據庫連接出錯");
}
public static Connection getInstance() {
if (connection != null)
return connection;
else{
connection = new Connection();
}
return connection;
}
}
- 將讀取配置文件的類封裝成一個對象(ConnectionProperty),並且將其設爲參數傳入數據庫連接對象中
ConnectionProperty.java
import com.opslab.util.PropertiesUtil;
import java.net.URL;
public class ConnectionProperty {
private String host;
private int port;
private String database;
private String path = "src/main/resources/properties/db.properties";
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getHost() {
return host;
}
public int getPort() {
return port;
}
public String getDatabase() {
return database;
}
public ConnectionProperty(){
UpdateProperty(path);
}
public void UpdateProperty(String path){
this.path = path;
PropertiesUtil propertiesUtil = new PropertiesUtil();
try{
host = propertiesUtil.GetValueByKey(path,"mongodb.host");
port = Integer.parseInt(propertiesUtil.GetValueByKey(path,"mongodb.port"));
database = propertiesUtil.GetValueByKey(path,"mongodb.database");
}catch (Exception e){
//這裏添加一層文件保護,爲防止讀取不到配置文件導致出錯
e.printStackTrace();
}
}
}
- 創建db.properties
db.properties
mongodb.host = 127.0.0.1
mongodb.port = 27017
mongodb.database = test
- 項目代碼框架
db.properties存放在/src/main/resources/properties