1.pom文件添加:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.14.2</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.7.2.RELEASE</version> </dependency>
2.Spring配置文件的头信息,其中只需关注mongo的三行就行:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<mongo:mongo-client host="172.16.10.247" port="27017" id="mongo">
<mongo:client-options write-concern="SAFE"/>
</mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="db_mongo" mongo-ref="mongo" /> //注释,这句为了说明有账号密码而已,实际项目中没有这句<mongo:db-factory dbname="db_mongo" mongo-ref="mongo" username="qqw_reward" password="Reward_New001"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
3.DAO层代码:
package com.companyname.publicbase.dao; import java.util.List; import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Update; public interface BaseMongoDao { public <T> List<T> find(Class<T> clazz); public <T> T findOne(Query query, Class<T> clazz); public <T> void update(Query query, Update update, Class<T> T); //批量更新 public <T> void updatemulti(Query query, Update update, Class<T> clazz); public <T> void save(T entity); public <T> T findById(String id, Class<T> clazz); public <T> long count(Query query, Class<T> clazz); public <T> void remove(Query query, Class<T> clazz); }
4.DAO实现类的代码:
package com.companyname.publicbase.dao.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.query.Query; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; @Repository @Transactional(rollbackFor=Exception.class) public class BaseMongoDaoImpl<T>{ /*@Resource MongoTemplate mongoTemplate; public List<T> find(Query query, Class<T> clazz) { return (List<T>) mongoTemplate.find(query, clazz); }*/ @Resource private MongoTemplate mongoTemplate; public List<T> find(Query query, Class<T> clazz) { System.out.println(mongoTemplate); return (List<T>) mongoTemplate.find(query, clazz); } /** * spring mongodb 集成操作类 */ /*@Autowired private MongoTemplate mongoTemplate; public List<T> find(Query query, Class<T> clazz) { return (List<T>) mongoTemplate.find(query, clazz); } */ /*public T findOne(Query query, Class<T> clazz) { return (T) mongoTemplate.findOne(query, clazz); } public void update(Query query, Update update, Class<T> clazz) { mongoTemplate.findAndModify(query, update, clazz); } //批量更新 public void updatemulti(Query query, Update update, Class<T> clazz) { mongoTemplate.updateMulti(query, update, clazz); } public void save(T entity) { mongoTemplate.insert(entity); } public T findById(String id, Class<T> clazz) { return (T) mongoTemplate.findById(id, clazz); } public long count(Query query, Class<T> clazz){ return mongoTemplate.count(query, clazz); } public void remove(Query query, Class<T> clazz){ mongoTemplate.remove(query, clazz); } */ }
这样Spring4整合mongo3.X就结束了,如果报这个异常:NoClassDefFoundError: org/springframework/objenesis/ObjenesisStd,
就说明你用的不是spring4.x,到此恭喜你已经整合完毕了。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面就是整合mongo2.x的内容了,这个spring3.2起码是没有问题的,来!下面开始整理:
1.pom文件:
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>2.12.4</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-mongodb</artifactId> <version>1.6.1.RELEASE</version> </dependency>
2.Spring文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc.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-1.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd ">
<mongo:mongo host="172.16.10.247" port="27017"/>
<mongo:db-factory dbname="db_mongo" mongo-ref="mongo" username="qqw_reward" password="Reward_New001"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
</bean>
</beans>
3.java调用就跟上面一样了,dao和实现类都是一样的,就这样两套都整理完了,有什么要完善的再回来改,之前测试过是没有问题的,百分百能跑,
也可以看看这个怎么写的http://blog.csdn.net/chenpy/article/details/50458787