mongodb版本:3.4.x
参考:http://blog.csdn.net/zhouzhiwengang/article/details/51446784
参考:http://blog.csdn.net/congcong68/article/details/44545253
1、maven下载相关依赖
<!-- spring-data-mongodb -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.6.RELEASE</version>
</dependency>
<!-- mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.5.0</version>
</dependency>
2、mongodb的xml配置
xml的配置参考(很详细的配置):http://blog.csdn.net/u010088951/article/details/54949021
<?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:p="http://www.springframework.org/schema/p"
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-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">
<!-- mongodb 版本3.4.7 -->
<!--mongodb credentials的配置形式是:用户名:密码@默认数据库 -->
<!-- 有密码认证需要添加这个 credentials="${db.user}:${db.pwd}@${db.name} -->
<mongo:mongo-client id="mongoClient" host="${db.host}" port="${db.port}" credentials="${db.user}:${db.pwd}@${db.name}"></mongo:mongo-client>
<mongo:db-factory id="mongoDbFactory" dbname="${db.name}" mongo-ref="mongoClient"/>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg ref="mongoDbFactory" />
</bean>
</beans>
对应配置
#mongodb-config
db.port=27017
db.host=127.0.0.1
db.user=dev
db.pwd=123456
db.name=isdb
加载该配置
3、编写dao层
基类MongoBase接口
import java.util.List;
import java.util.Map;
public interface MongoBase<T> {
/**
* 添加
* @param object
* @param collectionName
*/
public void insert(T object,String collectionName);
/**
* 根据条件查找
* @param params
* @param collectionName
* @return
*/
public T findOne(Map<String,Object> params,String collectionName);
/**
* 查找所有
* @param params
* @param collectionName
* @return
*/
public List<T> findAll(Map<String,Object> params,String collectionName);
/**
* 修改
* @param params
* @param collectionName
*/
public void update(Map<String,Object> params,String collectionName);
/**
* 创建集合
* @param collectionName
*/
public void createCollection(String collectionName);
/**
* 根据条件删除
* @param params
* @param collectionName
*/
public void remove(Map<String,Object> params,String collectionName);
}
子类接口
import com.bluedon.useradmin.datainteraction.model.UploadData;
public interface UploadDataDao extends MongoBase<UploadData> {
}
实现类
import java.util.List;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;
import com.bluedon.useradmin.datainteraction.model.UploadData;
import com.mongodb.WriteConcern;
import com.mongodb.WriteResult;
@Repository("uploadDataDao")
public class UploadDataDaoImpl extends BaseDao<UploadData> implements UploadDataDao {
@Override
public String insert(UploadData object, String collectionName) {
//设置写入承认
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
mongoTemplate.insert(object, collectionName);
return object.getId();
}
@Override
public UploadData findOne(Query query, String collectionName) {
//new Query(Criteria.where("id").is(params.get("id")))
return mongoTemplate.findOne(query,UploadData.class,collectionName);
}
@Override
public List<UploadData> findAll(Query query, String collectionName) {
List<UploadData> result = mongoTemplate.find(query, UploadData.class,collectionName);
return result;
}
@Override
public int updateAndInsert(Query query,Update update, String collectionName) {
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
WriteResult wr = mongoTemplate.upsert(query,update, UploadData.class,collectionName);
return wr.getN();
}
@Override
public int updateFirst(Query query,Update update, String collectionName) {
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
WriteResult wr = mongoTemplate.updateFirst(query,update, UploadData.class,collectionName);
return wr.getN();
}
@Override
public int updateAll(Query query,Update update, String collectionName) {
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
WriteResult wr = mongoTemplate.updateMulti(query,update, UploadData.class,collectionName);
return wr.getN();
}
@Override
public int remove(Query query, String collectionName) {
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
WriteResult wr = mongoTemplate.remove(query,UploadData.class,collectionName);
return wr.getN();
}
@Override
public boolean exist(Query query, String collectionName) {
return mongoTemplate.exists(query,collectionName);
}
}
4、实体类
import java.util.Date;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.mongodb.core.mapping.Document;
@Document(collection="uploaddata")
public class UploadData {
@Id
private String id;
/**
* 系统编码
*/
private String systemCode;
/**
* 批次
*/
private String batch;
/**
* 是否同步数据成功
*/
private boolean status;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date modifyTime;
public UploadData() {
super();
// TODO Auto-generated constructor stub
}
@PersistenceConstructor
public UploadData(String id, String systemCode, String batch, boolean status, Date createTime, Date modifyTime) {
super();
this.id = id;
this.systemCode = systemCode;
this.batch = batch;
this.status = status;
this.createTime = createTime;
this.modifyTime = modifyTime;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSystemCode() {
return systemCode;
}
public void setSystemCode(String systemCode) {
this.systemCode = systemCode;
}
public String getBatch() {
return batch;
}
public void setBatch(String batch) {
this.batch = batch;
}
public boolean isStatus() {
return status;
}
public void setStatus(boolean status) {
this.status = status;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
}
5、service层
service接口类
public interface TestService {
public void addUser();
}
service实现类
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.bluedon.dao.VolunteerDao;
import com.bluedon.model.Volunteer;
@Service("testServiceImpl")
public class TestServiceImpl implements TestService{
@Resource
private VolunteerDao volunteerDto;
@Override
public void addUser(){
String collectionName ="users";
Volunteer user =new Volunteer();
user.setXm("张三");
volunteerDto.insert(user, collectionName);
}
}