标题:
java-spring与mongodb的整合方式一 自动注入xml(5)
[打印本页]
作者:
look_w
时间:
2019-4-19 19:32
标题:
java-spring与mongodb的整合方式一 自动注入xml(5)
相关配置文件xml:
common-beans.xml 可更改操作的数据库名称 目前为student 和 链接地址 192.168.0.121
<?xml version="1.0" encoding="utf-8"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/s ... ing-context-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
">
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongo" ref="mongo" />
<constructor-arg name="databaseName" value="student"/>
</bean>
<bean id="mongo" class="org.springframework.data.mongodb.core.MongoFactoryBean">
<property name="host" value="192.168.0.121"/>
<property name="port" value="27017"/>
</bean>
</beans>
info-reposity.xml
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
">
<!-- 配置DAO组件的模板 -->
<bean id="genericReposity" class="com.mongo.reposity.Impl.GenericReposityImpl"
parent="mongoTemplate">
</bean>
<bean id="userReposity" class="com.mongo.reposity.Impl.UserReposityImpl"
parent="mongoTemplate">
</bean>
</beans>
getDateAcition.java
package com.mongo.action;
import java.util.Map;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapreduce.MapReduceOptions;
import org.springframework.data.mongodb.core.mapreduce.MapReduceResults;
import com.mongo.entity.User;
import com.mongo.reposity.UserReposity;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MapReduceOutput;
import com.opensymphony.xwork2.ActionSupport;
public class getDataAction extends ActionSupport {
/**
*
*/
private UserReposity userReposity;
public void setUserReposity(UserReposity userReposity) {
this.userReposity = userReposity;
}
private JSONObject user;
private static final long serialVersionUID = 535742118015700220L;
@Override
public String execute() {
JsonConfig jsonConfig = new JsonConfig();
jsonConfig.setIgnoreDefaultExcludes(false);
// jsonConfig.setExcludes(new String[] { "name" });
jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
// 新增数据
// User u=new User();
// u.setName("zoe");
// u.setMessage("first blood");
// userReposity.saveObject(u);
// System.out.println("写入完成"+u.toString());
// //查询数据
// //获取全部
// List<User> us=userReposity.getAllObjects(User.class);
// //从数目上查询取0到1000
// List<User> us1=userReposity.getUsers(0, 1000);
// //条件查询
// List<Condition> cds=new ArrayList<Condition>();
// Condition cd=new Condition();
// cd.setKey("name");
// cd.setValue("zoe");
// cds.add(cd);
// List<User> us2=userReposity.getUsers(0, 1000, cds);
// //模糊查询
// List<User> us3=userReposity.getUsers("oe");
// //查询数量
// long count=userReposity.getCount("first blood");
// //查询属于的集合(in) 不属于(nin)
// List<String> names=new ArrayList<String>();
// names.add("zoe");
// names.add("koe");
// List<User> us4=userReposity.getUsersBynames(0, 1000,names);
//
// //更新
// User u=userReposity.getAllObjects(User.class).get(0);
// userReposity.updateUserUpdateName(u, "joe");
//
// //删除
// userReposity.deleteObject(User.class, "534cea00cf20ab0525bdc53e");
// mapreduce
MongoTemplate m = (MongoTemplate) userReposity;
DBCollection coll = m.getCollection("user"); //集合名
String map = "function() { emit(this.name, {count:1});}";
String reduce = "function(key, values) {var total = 0;for(var i=0;i<values.length;i++){total += values
.count;}return {count:total};}";
String result = "resultCollection";
MapReduceOutput mapReduceOutput = coll.mapReduce(map,
reduce.toString(), result, null);
DBCollection resultColl = mapReduceOutput.getOutputCollection();
DBCursor cursor = resultColl.find();
while (cursor.hasNext()) {
System.out.println(cursor.next());
// Map object = cursor.next().toMap();
// System.out.println(object.get("value"));
}
// MongoOperations mongoOps = (MongoOperations) userReposity;
// String map = "function() { emit(this.name, {count:1});}";
// String reduce = "function(key, values) {var total = 0;for(var i=0;i<values.length;i++){total += values
.count;}return {count:total};}";
// try {
// MapReduceResults<User> results = mongoOps.mapReduce("requesterBill",
// map,
// reduce,
//// new MapReduceOptions().outputCollection("mr_out"),
// User.class);
// for (User valueObject : results) {
// System.out.println(valueObject);
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// String jsonfacility =
// "{\"id\":\"PublicSecurity\",\"name\":\"数据\",\"value\":\"123\"}";
// user = JSONObject.fromObject(jsonfacility);
// userReposity.mapReduse();
return SUCCESS;
}
public JSONObject getUser() {
return user;
}
public void setUser(JSONObject user) {
this.user = user;
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0