SpringData操作MongoDB

  • 时间:
  • 浏览:0
  • 来源:大发彩神下载—大发彩神APP

/**

<dependency>

 * 

Query query = new Query();

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

query.skip(start);

return member;

member.setBook(book);

测试类-会员

 * 

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

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

query.limit(size);

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

}

{

<version>1.10.4.RELEASE</version>

applicationContext.xml文件引入mongodb配置

if (book.getTheName() != null)

member.setPassword("password3");

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

protected Class<Book> getEntityClass()

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

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

this.id = id;

{

member.setUsername("username1");

public void updateMulti(Book book) throws Exception

book.setId("3");

return theName;

 * 

@Repository

{

}

this.username = username;

 * 更新库中所有数据

public void deleteBook() throws Exception

public void deleteById(String id)

@Autowired

}

{

{

public void setMember(Member member)

public void setId(String id)

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

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

}

public class MemberDao extends MongoGenDao<Member>

{

}

this.mongoTemplate = mongoTemplate;

}

{

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

基于DAO,对接MongoTemplate

public void updateMulti(Member member) throws Exception

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

return this.getPageCount(query);

for(Book book:bookList)

{

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

{

{

@Test

public String getId()

Member member = new Member();

if (member.getUsername() != null)

import org.springframework.stereotype.Repository;

public void save2() throws Exception

</dependency>

if(member == null)

this.theName = theName;

public void delete(T t)

}

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

{

member.setUsername("username3");

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

{

{

public void update() throws Exception

if (member.getUsername() != null)

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

 */

{

member.setId("1");

 */

 * 通过条件进行分页查询

this.member = member;

else

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

public void updateFirst(Member member) throws Exception

}

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

// 此处还还可以增加分页查询条件Criteria.以后query.addCriteria(criteria);

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

<type>jar</type>

{

public void setBook(Book book)

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

memberDao.save(member);//在Member和Book互相中含的状况下,若没有设置@DBRef,则此处会规模溢出

/**

 */

if(member.getBook() == null)

}

 * 实现钩子方式,返回反射的类型

pom.xml加带依赖

测试类-书本

{

{

{

{

else

}

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

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

Query query = new Query();

}

member.setBook(book);

member.setUsername("username1");

memberDao.updateFirst(member);

/**

{

{

/**

member.setPassword("password1");

 * 

public void save3() throws Exception

Update update = new Update();

{

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

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

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

public void queryBook() throws Exception

 */

{

}

Query query = new Query();

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

}

}

protected Class<Member> getEntityClass()

}

for(Book book:bookList)

 * 

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

private String password;

return book;

}

return id;

}

public Long getPageCount(Query query)

{

public void setId(String id)

/**

this.password = password;

@Transactional

/**

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

}

 * 

private String theName;

}

@DBRef

 */

}

}

}

public void execute() throws Exception

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

@Test

book.setTheName("book1");

}

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

 * 

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

System.out.println("该book没有会员");

public void getMember() throws Exception

{

 * 

}

/**

this.mongoTemplate.remove(t);

public void setPassword(String password)

{

public void deleteMember() throws Exception

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

{

</bean>

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

@Repository

for(Member member:memberList)

public abstract class MongoGenDao<T>

 * 删除对象

public T queryOne(Query query)

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

private String id;

@Autowired

<!-- MongoDB -->

else

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

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

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

 */

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

/**

member.setId("2");

}

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

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

<!-- spring data -->

return Member.class;

/**

{

}

/**

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

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

{

}

/**

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

System.out.println("没有会员");

private MemberDao memberDao;

{

}

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

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

 * 更新操作

Member member = new Member();

memberDao.save(member);

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

 */

this.book = book;

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

return Book.class;

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

/**

}

{

 * 根据Id从Collection中查询对象

}

{

<dependency>

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

{

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

}

}

{

 * 

}

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

<!-- 以后使用你这一 调用相应的方式操作 -->

}

{

else

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

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

{

}

import java.util.List;

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

 */

{

this.mongoTemplate.remove(query);

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

import api.BaseJunitTest;

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

}

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

@Test

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

 * 更新库中所有数据

 */

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

{

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

{

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

}

// 肯能主键为空,则不进行修改

}

private Member member;

if (member.getPassword() != null)

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

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

public void getBook() throws Exception

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

{

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

return id;

}

{

@Override

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

public Member getMember()

{

{

}

return username;

</mongo:mongo-client>

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

 * 通过条件查询单个实体

}

import org.junit.Test;

{

/**

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

测试类DAO-BOOK

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

 */

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

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

// 肯能主键为空,则不进行修改

</dependency>

public Long queryPageCount(Book book)

}

public void updateInser(Query query, Update update)

@Test

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

memberDao.delete(member);

@Autowired

if(bookList.size() == 0)

if (book.getTheName() != null)

Book book = new Book();

}

{

}

 */

{

}

member.setUsername("username_1");

// 肯能主键为空,则不进行修改

 * 更新操作

}

}

<scope>compile</scope>

public String getTheName()

Query query = new Query();

@Test

 * 

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

}

{

}

}

}

/**

}

public String getUsername()

private String id;

 */

public String getPassword()

 */

public T queryById(String id)

 * 为属性自动注入bean服务

 * 根据条件查询集合

Update update = new Update();

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

 * 

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

<dependency>

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

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

 * 

测试类

}

public void queryMember() throws Exception

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

{

 * 实现钩子方式,返回反射的类型

<version>1.2.1.RELEASE</version>

</beans>

 */

/**

}

public String getId()

{

{

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

// 肯能主键为空,则不进行修改

 * 

member.setId("3");

{

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

Query query = new Query();

@Override

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

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

}

public class TestMongo extends BaseJunitTest

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

{

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

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

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

@Test

if(book.getMember() == null)

Update update = new Update();

bookDao.delete(book);

book.setId("1");

 */

 */

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

protected MongoTemplate mongoTemplate;

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

}

@Test

this.id = id;

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

{

 * 

}

if(book != null)

/**

Update update = new Update();

if (null != criteria)

book.setMember(member);

else

}

 * 

 * 

 * 

}

 * 查找更新,肯能没有找到符合的记录,则将更新的记录插入库中

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

/**

else

{

{

{

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

public void updateFirst(Query query, Update update)

public void save(T t)

public class Book

{

{

 */

 * 

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

import java.util.List;

}

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

}

{

memberDao.save(member);

{

}

public void updateFirst(Book book) throws Exception

}

 */

public void setMongoTemplate(MongoTemplate mongoTemplate)

@Test

{

Member member = new Member();

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

 */

import org.springframework.transaction.annotation.Transactional;

System.out.println("该会员没有book");

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

{

}

@DBRef

{

{

 * 根据Id删除用户

 * 保存4个对象

 */

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

public void updateMulti(Query query, Update update)

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

public List<T> queryList(Query query)

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

}

 */

/**

{

{

}

}

public Book getBook()

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

// 此处还还可以增加分页查询条件Criteria.以后query.addCriteria(criteria);

import java.util.List;

}

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

for(Member member:memberList)

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

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

</dependency>

public void setUsername(String username)

{

/**

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

/**

{

{

if (member.getPassword() != null)

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

{

}

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

@Test

/**

{

 * 

this.mongoTemplate.save(t);

@Test

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

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

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

query.addCriteria(criteria);

}

public class BookDao extends MongoGenDao<Book>

}

{

return this.getPageCount(query);

import java.util.List;

return password;

Book book = new Book();

{

}

if(memberList.size() == 0)

 * 

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

}

private String username;

System.out.println("没有book");

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

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

 * 

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

import org.springframework.stereotype.Repository;

/**

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

{

protected abstract Class<T> getEntityClass();

}

 * 

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

Query query = new Query(criteria);

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

{

{

}

book.setTheName("book3");

}

public Long queryPageCount(Member member)

bookDao.save(book);

private BookDao bookDao;

member.setPassword("password1");

{

}

 */

测试类DAO-Member

}

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

public void setTheName(String theName)

<version>3.0.4</version>

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

{

}

<groupId>org.mongodb</groupId>

{

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

System.out.println("没有你这一 会员");

{

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

}

return lists;

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

private Book book;

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

public class Member