SpringData操作MongoDB

  • 时间:
  • 浏览:1
  • 来源:大发5分排列3_大发5分排列3官方

}

public Long getPageCount(Query query)

this.mongoTemplate.updateMulti(query, update, this.getEntityClass());

import org.springframework.data.mongodb.core.query.Query;

}

this.updateMulti(Query.query(Criteria.where("_id").is(member.getId())), update);

 * @author http://www.chisalsoft.com

public void setMongoTemplate(MongoTemplate mongoTemplate)

if(member.getBook() == null)

}

@Test

}

}

<scope>compile</scope>

public String getId()

<type>jar</type>

}

<bean id="memberDao" class="mongoDB.MemberDao" />

 */

 */

测试类DAO-Member

public class BookDao extends MongoGenDao<Book>

public void setId(String id)

}

public void updateMulti(Book book) throws Exception

}

}

{

List<Book> bookList = bookDao.queryList(new Query());

{

{

public void delete(T t)

import org.springframework.data.mongodb.core.mapping.DBRef;

memberDao.save(member);

 * 为属性自动注入bean服务

<!-- MongoDB -->

@Override

<version>1.10.4.RELEASE</version>

<bean id="bookDao" class="mongoDB.BookDao" />

}

{

Criteria criteria = Criteria.where("_id").is(id);

for(Member member:memberList)

 * @author http://www.chisalsoft.com

this.updateMulti(Query.query(Criteria.where("_id").is(book.getId())), update);

if (null != criteria)

}

public void updateMulti(Member member) throws Exception

private String id;

xmlns:xsi="http://www.w3.org/1501/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"

throw new Exception("Update data Id is Null");

 * 

else

{

return id;

{

{

public void setMember(Member member)

            http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">

memberDao.save(member);

}

public void queryMember() throws Exception

bookDao.save(book);

Member member = new Member();

}

return theName;

public void setTheName(String theName)

public void setUsername(String username)

<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />

 * 更新库中所有数据

else

 * @author http://www.chisalsoft.com

}

 */

public void updateFirst(Book book) throws Exception

}

import org.springframework.beans.factory.annotation.Autowired;

if(memberList.size() == 0)

<artifactId>mongo-java-driver</artifactId>

{

{

private String password;

 * 通过条件进行分页查询

基于DAO,对接MongoTemplate

/**

this.book = book;

<mongo:db-factory id="mongoDbFactory" dbname="mongoTest" mongo-ref="mongo" />

import org.springframework.data.mongodb.core.query.Update;

return this.mongoTemplate.find(query, this.getEntityClass());

/**

}

{

 * @author http://www.chisalsoft.com

{

private Book book;

import org.junit.Test;

<version>1.2.1.RELEASE</version>

public List<Member> queryPage(Member member, Integer start, Integer size)

}

 */

 * @author http://www.chisalsoft.com

{

 */

}

memberDao.updateFirst(member);

{

 * 

</mongo:mongo-client>

import org.springframework.data.mongodb.core.query.Criteria;

{

book.setTheName("book3");

 */

{

{

}

{

/**

Query query = new Query(criteria);

 * 

 * 

private String username;

 * 更新满足条件的所有记录

@Test

public String getUsername()

<context:component-scan base-package="mongoDB" />

if (book.getTheName() != null)

{

this.mongoTemplate.remove(t);

 * 

Query query = new Query();

List<Member> memberList = memberDao.queryList(new Query());

<mongo:client-options write-concern="SAFE" />

</dependency>

public void save3() throws Exception

 * @author http://www.chisalsoft.com

this.mongoTemplate = mongoTemplate;

 * 根据条件查询集合

@Override

}

 */

}

xmlns:mongo="http://www.springframework.org/schema/data/mongo"

               http://www.springframework.org/schema/data/mongo  

{

Update update = new Update();

 * @author http://www.chisalsoft.com

import java.util.List;

return member;

import org.springframework.data.mongodb.core.query.Update;

}

// 随后主键为空,则不进行修改

throw new Exception("Update data Id is Null");

{

}

if (null == book.getId() || "".equals(book.getId().trim()))

}

import org.springframework.data.mongodb.core.query.Query;

 * 更新操作

{

 * 查询满足分页的记录总数

{

 * @author http://www.chisalsoft.com

// 随后主键为空,则不进行修改

{

{

/**

{

{

this.username = username;

 * 

 * @author http://www.chisalsoft.com

Query query = new Query();

@Test

<artifactId>spring-data-mongodb-cross-store</artifactId>

Book book = new Book();

import org.springframework.beans.factory.annotation.Autowired;

{

/**

{

member.setPassword("password1");

{

@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=false)

update.set("theName", book.getTheName());

public List<T> getPage(Query query, int start, int size)

}

@Test

}

Member member = new Member();

}

 */

 */

System.out.println("该book有会员:"+book.getMember().getUsername());

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">

 * @author http://www.chisalsoft.com

return lists;

update.set("username", member.getUsername());

}

 */

import java.util.List;

public class Book

}

}

public String getId()

{

member.setId("2");

this.member = member;

public Long queryPageCount(Book book)

 * 

if(book != null)

if(member == null)

if(book.getMember() == null)

<dependency>

Query query = new Query();

 * 实现钩子法律法律依据,返回反射的类型

this.updateFirst(Query.query(Criteria.where("_id").is(book.getId())), update);

Query query = new Query();

/**

}

if (null == member.getId() || "".equals(member.getId().trim()))

}

public void updateInser(Query query, Update update)

<import resource="mongodb-context.xml"/>

book.setId("3");

/**

@DBRef

<mongo:mongo-client host="数据库IP" port="数据库端口" credentials="数据库用户名:数据库密码@数据库名称" id="mongo">

<version>3.0.4</version>

}

}

protected MongoTemplate mongoTemplate;

 * @author http://www.chisalsoft.com

System.out.println("这麼 这一 会员");

private String id;

 * 

public void update() throws Exception

 * @author http://www.chisalsoft.com

return this.getPage(query, (start - 1) * size, size);

if (null != query && this.queryOne(query) != null)

 */

{

public void deleteMember() throws Exception

this.password = password;

return book;

public class MemberDao extends MongoGenDao<Member>

{

member.setId("3");

 * @author http://www.chisalsoft.com

bookDao.delete(book);

{

query.addCriteria(criteria);

 */

</bean>

/**

{

@Autowired

public class TestMongo extends BaseJunitTest

if (member.getUsername() != null)

}

 */

}

测试类-会员

import org.springframework.data.mongodb.core.query.Query;

return Book.class;

{

if (null == member.getId() || "".equals(member.getId().trim()))

<!-- 导入mongodb的配置文件 -->

}

}

if (member.getUsername() != null)

 * 

book.setTheName("book1");

import org.springframework.data.mongodb.core.mapping.DBRef;

throw new Exception("Update data Id is Null");

member.setBook(book);

}

return id;

 */

 * 

 * @author http://www.chisalsoft.com

}

{

{

}

/**

@Repository

}

/**

}

}

this.updateFirst(Query.query(Criteria.where("_id").is(member.getId())), update);

return this.getPageCount(query);

book.setId("1");

}

{

public void getMember() throws Exception

}

 * 分页查询 对应mongodb操作中的 db.member.find().skip(10).limit(10);

System.out.println("保存完成");

this.mongoTemplate.updateFirst(query, update, this.getEntityClass());

 * @author http://www.chisalsoft.com

private MemberDao memberDao;

 * 查找更新,随后这麼 找到符合的记录,则将更新的记录插入库中

import org.springframework.stereotype.Repository;

{

import api.BaseJunitTest;

 * 

{

@Autowired

/**

{

// 此处能并能增加分页查询条件Criteria.随后query.addCriteria(criteria);

 */

else

{

}

System.out.println(book.getTheName());

}

<beans xmlns="http://www.springframework.org/schema/beans"

测试类-书本

return this.mongoTemplate.findOne(query, this.getEntityClass());

 * 实现钩子法律法律依据,返回反射的类型

Book book = bookDao.queryById("3");

{

public T queryById(String id)

return this.getPageCount(query);

<!-- spring连接mongodb数据库的配置 -->

               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  

 * 根据Id删除用户

@DBRef

{

/**

{

return this.mongoTemplate.findOne(query, this.getEntityClass());

return password;

for(Book book:bookList)

{

}

 * 

public void execute() throws Exception

}

 * 删除对象

}

System.out.println("该会员有book:"+member.getBook().getTheName());

<groupId>org.springframework.data</groupId>

// 此处能并能增加分页查询条件Criteria.随后query.addCriteria(criteria);

}

else

System.out.println("这麼 book");

@Repository

{

@Test

/**

public Member getMember()

{

for(Book book:bookList)

public void updateFirst(Query query, Update update)

 * 

</beans>

}

// 随后主键为空,则不进行修改

Book book = new Book();

}

this.id = id;

/**

 */

Member member = new Member();

 * 查询满足分页的记录总数

if (member.getPassword() != null)

public class Member

<dependency>

member.setPassword("password1");

 * 

}

import org.springframework.data.mongodb.core.MongoTemplate;

 * @author http://www.chisalsoft.com

public void deleteById(String id)

{

}

@Test

public abstract class MongoGenDao<T>

 */

import org.springframework.stereotype.Repository;

测试类

memberDao.save(member);//在Member和Book互相蕴含的情况汇报下,若这麼 设置@DBRef,则此处会规模溢出

}

{

}

pom.xml上加依赖

member.setUsername("username_1");

{

{

{

List<Member> memberList = memberDao.queryList(new Query());

 * 

return this.getPage(query, (start - 1) * size, size);

 * @author http://www.chisalsoft.com

 * @author http://www.chisalsoft.com

 */

import java.util.List;

 */

public void updateMulti(Query query, Update update)

public List<T> queryList(Query query)

<artifactId>spring-data-mongodb</artifactId>

if (member.getPassword() != null)

{

}

{

}

}

 */

xsi:schemaLocation="http://www.springframework.org/schema/beans   

 * 更新库中所有数据

{

</dependency>

Member member = memberDao.queryById("1");

else

protected abstract Class<T> getEntityClass();

/**

}

 * 分页查询 对应mongodb操作中的 db.book.find().skip(10).limit(10);

applicationContext.xml文件引入mongodb配置

update.set("password", member.getPassword());

this.mongoTemplate.save(t);

<!-- 只要使用这一 调用相应的法律法律依据操作 -->

public void getBook() throws Exception

member.setUsername("username1");

 * 

System.out.println("这麼 找到这本书");

{

}

return this.mongoTemplate.count(query, this.getEntityClass());

/**

@Test

<groupId>org.mongodb</groupId>

if (book.getTheName() != null)

}

System.out.println(book.getTheName());

if (null == book.getId() || "".equals(book.getId().trim()))

{

 * 根据条件查询库中符合记录的总数,为分页查询服务

 * @author http://www.chisalsoft.com

mongodb数据库源配置,mongodb-context.xml

@Transactional

memberDao.delete(member);

System.out.println("该book这麼 会员");

{

public Book getBook()

private BookDao bookDao;

{

List<T> lists = this.mongoTemplate.find(query, this.getEntityClass());

System.out.println(member.getUsername());

}

{

public T queryOne(Query query)

{

/**

public Long queryPageCount(Member member)

}

{

import org.springframework.data.mongodb.core.query.Update;

 * 更新操作

Criteria criteria = Criteria.where("_id").in(id);

import org.springframework.transaction.annotation.Transactional;

private String theName;

System.out.println("这麼 会员");

<!-- spring data -->

import java.util.List;

List<Book> bookList = bookDao.queryList(new Query());

{

/**

 * 

Update update = new Update();

{

{

{

}

@Autowired

Update update = new Update();

}

protected Class<Member> getEntityClass()

Query query = new Query();

member.setUsername("username1");

query.skip(start);

this.theName = theName;

{

public void setBook(Book book)

book.setMember(member);

{

return Member.class;

{

{

 * 更新满足条件的第一一两个 记录

{

public void save2() throws Exception

member.setUsername("username3");

}

 * @author http://www.chisalsoft.com

/**

System.out.println("该会员这麼 book");

{

private Member member;

public void save(T t)

this.mongoTemplate.remove(query);

this.id = id;

System.out.println(member.getUsername());

protected Class<Book> getEntityClass()

}

<?xml version="1.0" encoding="UTF-8"?>

</dependency>

// 随后主键为空,则不进行修改

}

public List<Book> queryPage(Book book, Integer start, Integer size)

/**

}

 */

 * @author http://www.chisalsoft.com

public void queryBook() throws Exception

Member member = memberDao.queryById("3");

update.set("theName", book.getTheName());

@Test

 * 

update.set("password", member.getPassword());

测试类DAO-BOOK

throw new Exception("Update data Id is Null");

return username;

{

{

}

/**

{

public void deleteBook() throws Exception

@Test

<dependency>

{

/**

}

import org.springframework.data.mongodb.core.query.Criteria;

<groupId>org.springframework.data</groupId>

Update update = new Update();

 */

}

 * 通过条件查询单个实体

 * 

}

@Test

query.limit(size);

import org.springframework.test.context.transaction.TransactionConfiguration;

{

public String getTheName()

member.setPassword("password3");

else

public void setId(String id)

import org.springframework.data.mongodb.core.query.Query;

if(bookList.size() == 0)

member.setBook(book);

}

 * 根据Id从Collection中查询对象

}

{

{

update.set("username", member.getUsername());

 * 保存一一两个 对象

 * 

member.setId("1");

public String getPassword()

this.mongoTemplate.upsert(query, update, this.getEntityClass());

}

public void setPassword(String password)

public void updateFirst(Member member) throws Exception

}

 * @author http://www.chisalsoft.com

 * 

 * 

for(Member member:memberList)

import org.springframework.data.mongodb.core.query.Criteria;