eclipse--ssm maven项目接mongodb
来源:互联网 发布:如何登陆阿里云服务器 编辑:程序博客网 时间:2024/05/17 02:38
pom.xml:
application.xml:
package com.xy.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Properties;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
public class MongoDBUtil {
// 定义默认配置,1、IP地址 2、端口号 3、用户名 4、密码 5、配置文件位置名 6、数据库名
private static final String MONGODB_ADDRESS = "192.168.10.203";
private static final int MONGODB_PORT = 27017;
private static final String MONGODB_USERNAME = "root";
private static final String MONGODB_PASSWORD = "XYYYServer003";
private static final String MONGODB_RESOURCE_FILE = "mongodb.properties";
private static final String MONGODB_DBNAME = "Xview";
private static final String MONGODB_COLLECTIONNAME = "test";
// 定义静态变量,1、Mongo对象(代表数据库连接)2、DB对象(代表数据库)3、集合名4、数据库相关配置映射集合5、已获取的数据库连接
private static Mongo mongo;
private static DB db;
private static DBCollection collection;
private static Map<String, String> cfgMap = new HashMap<String, String>();
private static Hashtable<String, DB> mongoDBs = new Hashtable<String, DB>();
/**
* 初始化Mongo的数据库
*/
static {
init();
}
/**
* 获取配置文件中配置的DB对象
*/
public static DB getDB() {
return db;
}
/**
* 获取配置文件中配置的DBCollection对象
*/
public static DBCollection getDBCollection() {
return collection;
}
/**
* 根据数据库名称,得到数据库 如果不存在,则创建一个该名称的数据库,并设置用户名和密码为配置文件中的参数值
*
* @param dbName
* @return DB
*/
@SuppressWarnings("deprecation")
public static DB getDBByName(String dbName) {
DB db = mongo.getDB(dbName);
if (!mongoDBs.contains(db)) {
System.out.println("add");
db.addUser(cfgMap.get("mongo.db.username"),
cfgMap.get("mongo.db.password").toCharArray());
mongoDBs.put(dbName, db);
}
return db;
}
// ————————————————————————————————————初始化过程————————————————————————————————————
/**
* 获取配置文件mongedb.cfg.properties的文件对象
*/
public static File getConfigFile() {
String path = MongoDBUtil.class.getResource("/").getPath();
String fileName = path + MONGODB_RESOURCE_FILE;
System.out.println(fileName);
File file = new File(fileName);
if (file.exists()) {
return file;
}
return null;
}
/**
* 通过mongedb.cfg.properties配置文件初始化配置映射集合,如果没有编写配置文件,则加载程序指定的默认配置
*/
@SuppressWarnings("unchecked")
private static void initCfgMap() {
File file = getConfigFile();
if (file != null) {
Properties p = new Properties();
try {
p.load(new FileInputStream(file));
for (Enumeration enu = p.propertyNames(); enu.hasMoreElements();) {
String key = (String) enu.nextElement();
String value = (String) p.getProperty(key);
cfgMap.put(key, value);
}
} catch (IOException e) {
System.out.println("加载Mongo配置文件失败!");
e.printStackTrace();
}
} else { // 如果没有编写配置文件,则加载默认配置
cfgMap.put("mongo.db.address", MONGODB_ADDRESS);
cfgMap.put("mongo.db.port", String.valueOf(MONGODB_PORT));
cfgMap.put("mongo.db.username", MONGODB_USERNAME);
cfgMap.put("mongo.db.password", MONGODB_PASSWORD);
cfgMap.put("mongo.db.dbname", MONGODB_DBNAME);
cfgMap.put("mongo.db.collectionname", MONGODB_COLLECTIONNAME);
}
}
/**
* 初始化Mongo的数据库(将db指向相应对象引用,将collection指向相应对象引用,通过mongoDBs记录现有数据库对象)
*/
@SuppressWarnings("deprecation")
private static void init() {
initCfgMap();
try {
String address = cfgMap.get("mongo.db.address");
int port = Integer.parseInt(cfgMap.get("mongo.db.port").toString());
String dbName = cfgMap.get("mongo.db.dbname");
String username = cfgMap.get("mongo.db.username");
String password = cfgMap.get("mongo.db.password");
String collectionName = cfgMap.get("mongo.db.collectionname");
mongo = new Mongo(address, port);
if (dbName != null && !"".equals(dbName)) {
db = mongo.getDB(dbName);
if (username != null && !"".equals(username)) {
db.addUser(username, password.toCharArray());
if (collectionName != null && !"".equals(collectionName)) {
collection = db.getCollection(collectionName);
}
}
mongoDBs.put(dbName, db);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
mongodb.context.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:context="http://www.springframework.org/schema/context"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo-1.8.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<!-- 加载mongodb的属性配置文件 -->
<!-- <context:property-placeholder location="classpath:mongodb.properties" /> -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath:config/properties/mongodb.properties</value>
</list>
</property>
</bean> -->
<mongo:mongo-client host="${mongo.host}" port="${mongo.port}" credentials="${mongo.user}:${mongo.pwd}@${mongo.defaultDbName}" id="mongoClient">
<mongo:client-options
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>
<!-- dbname="${mongo.defaultDbName}" host="${mongo.host}"
port="${mongo.port}" username="${mongo.user}" password="${mongo.pwd}"
mongo-ref="mongoClient" -->
<mongo:db-factory id="mongoDbFactory"
dbname="${mongo.defaultDbName}"
mongo-ref="mongoClient"/>
<!-- 1.Spring提供的mongodb操作模板-->
<mongo:template id="mongoTemplate" db-factory-ref="mongoDbFactory" write-concern="NORMAL"/>
<!-- 2.Spring提供的mongodb操作模板-->
<!--<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory" />
</bean>-->
<!-- MongoDB GridFS Template 支持,操作mongodb存放的文件 -->
<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"
base-package="com.xy.dao"/>
<mongo:gridFsTemplate id="gridFsTemplcate" db-factory-ref="mongoDbFactory" converter-ref="converter" />
<!-- mongodb bean的仓库目录,会自动扫描扩展了MongoRepository接口的接口进行注入 -->
<mongo:repositories base-package="com.xy.*" />
<!--激活注解-->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
</beans>
mongodb.properties:
mongo.host=192.168.10.203
mongo.port=27017
mongo.defaultDbName=Pano
mongo.user=zhangjian
mongo.pwd=111111
mongo.connectionsPerHost=10
mongo.threadsAllowedToBlockForConnectionMultiplier=5
mongo.minConnectionsPerHost=5
mongo.connectTimeout=10000
mongo.maxWaitTime=120000
mongo.socketTimeout=0
mongo.socketKeepAlive=true
mongo.description=joyven test mongodb database
mongo.maxConnectionIdleTime=1500
mongo.maxConnectionLifeTime=0
mongo.heartbeatSocketTimeout=1000
mongo.heartbeatConnectTimeout=1500
mongo.minHeartbeatFrequency=5
mongo.heartbeatFrequency=10
sql语句也可以写在mapper里,sql语句是在网上找的,这里就不写了!
- eclipse--ssm maven项目接mongodb
- eclipse maven 搭建【SSM】项目
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
- 【SSM】Eclipse使用Maven创建Web项目+整合SSM框架
- Eclipse+Maven创建webapp项目(用于SSM项目搭建)
- maven+eclipse+SSM框架
- eclipse+maven+ssm框架
- JAVA Eclipse使用Maven构建web项目详解(SSM框架)
- eclipse下创建ssm+MySQL+maven多模块项目
- eclipse maven打包ssm项目,漏掉mapper.xml文件解决方法
- SSM整合 -- 在Eclipse怎么创建一个Maven项目
- maven+eclipse搭建项目工程ssm整合(一)
- maven+eclipse搭建项目工程ssm整合(二)
- maven+eclipse搭建项目工程ssm整合(三)
- eclipse下创建ssm+MySQL+maven多模块项目
- Mysql常用语句摘要
- P1868 饥饿的奶牛(区间问题)
- GitLab使用总结
- 玩转ADB命令(ADB命令使用大全)
- java设计模式之工厂方法模式
- eclipse--ssm maven项目接mongodb
- 关于You must not call setTag() on a view Glide is targeting错误处理
- ajax天气预报
- maven中跳过单元测试
- leetcode 34. Search for a Range
- 为爱追寻
- php自动刷新与关闭页面后继续执行,定时任务的实现方式
- Linux安装java,JDK
- Cookie/Session机制详解