Spring-MongoDB简单操作

1、简单的配置

<?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:mongo="http://www.springframework.org/schema/data/mongo"
    xsi:schemaLocation=" 
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
                http://www.springframework.org/schema/data/mongo
                  http://www.springframework.org/schema/data/mongo/spring-mongo-1.2.xsd">

  <mongo:mongo host="127.0.0.1" port="27017"/>
  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongo"/>
    <constructor-arg name="databaseName" value="app_DB"/>
  </bean>
</beans>

2、通用DAO
  1)接口

package cn.luxh.app.repository;

import java.util.List;

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

public interface MongoDBDao {
    
    void save(Object obj);
    
    <T> T findOne(Class<T> clazz, Query query);
    
    <T> List<T> findAll(Class<T> clazz);
    
    <T> T findById(Class<T> clazz,Object id);
    
    <T> List<T> find(Class<T> clazz, Query query);
    
    <T> List<T> findList(Class<T> clazz,Query query,int currentPage,int pageSize);
    
    <T> long findCount(Class<T> clazz,Query query);
    
    <T> int update(Query query,Update update,Class<T> clazz);
}

  2)接口实现

package cn.luxh.app.repository;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

@Repository
public class MongoDBDaoImpl implements MongoDBDao{
    
    @Autowired
    private MongoTemplate mongoTemplate;
    
    @Override
    public <T> T findOne(Class<T> clazz, Query query) {
        return mongoTemplate.findOne(query, clazz);
    }

    @Override
    public void save(Object obj) {
        mongoTemplate.save(obj);
    }

    @Override
    public <T> List<T> findAll(Class<T> clazz) {
        return mongoTemplate.findAll(clazz);
    }

    @Override
    public <T> T findById(Class<T> clazz, Object id) {
        return mongoTemplate.findById(id, clazz);
    }

    @Override
    public <T> List<T> find(Class<T> clazz, Query query) {
        return mongoTemplate.find(query, clazz);
    }

    @Override
    public <T> List<T> findList(Class<T> clazz, Query query, int currentPage,
            int pageSize) {
        //计算起始位置
        int startIndex = ((currentPage - 1)<0?0:(currentPage - 1))*pageSize;
        query.skip(startIndex);
        query.limit(pageSize);
        return mongoTemplate.find(query,clazz);
    }

    @Override
    public <T> long findCount(Class<T> clazz, Query query) {
        return mongoTemplate.count(query, clazz);
    }

    @Override
    public <T> int update(Query query, Update update, Class<T> clazz) {
        return mongoTemplate.updateFirst(query, update, clazz).getN();
    }

}

3、通用Service
 

  1)接口

package cn.luxh.app.service;

import java.util.List;

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

import cn.luxh.app.util.Pagination;

public interface MongoDBService {
    
    void save(Object obj);
    
    <T> T findOne(Class<T> clazz, Query query);
    
    <T> List<T> findAll(Class<T> clazz);
    
    <T> List<T> find(Class<T> clazz, Query query);
    
    /**
     * 分页查询
     */
    <T> Pagination<T> getPagination(Class<T> clazz,Query query,int currentPage,int pageSize);
    
    <T> int update(Query query, Update update, Class<T> clazz);
}

  2)实现

package cn.luxh.app.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import cn.luxh.app.repository.MongoDBDao;
import cn.luxh.app.util.Pagination;

@Service
public class MongoDBServiceImpl implements MongoDBService{
    
    @Autowired
    private MongoDBDao mongoDBDao;
    
    @Override
    public void save(Object obj) {
        mongoDBDao.save(obj);
    }

    @Override
    public <T> T findOne(Class<T> clazz, Query query) {
        return mongoDBDao.findOne(clazz, query);
    }

    @Override
    public <T> List<T> findAll(Class<T> clazz) {
        return mongoDBDao.findAll(clazz);
    }

    @Override
    public <T> List<T> find(Class<T> clazz, Query query) {
        return mongoDBDao.find(clazz, query);
    }

    @Override
    public <T> Pagination<T> getPagination(Class<T> clazz, Query query,
            int currentPage, int pageSize) {
        List<T> recordList = mongoDBDao.findList(clazz, query, currentPage, pageSize);
        long recordCount = mongoDBDao.findCount(clazz, query);
        return new Pagination<T>(currentPage,pageSize,recordCount,recordList);
    }

    @Override
    public <T> int update(Query query, Update update, Class<T> clazz) {
        return mongoDBDao.update(query, update, clazz);
    }

}

4、测试

package cn.luxh.app.test;

import java.util.List;
import java.util.Random;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import cn.luxh.app.entity.system.Book;
import cn.luxh.app.service.MongoDBService;
import cn.luxh.app.util.Pagination;

@RunWith(SpringJUnit4ClassRunner.class)  
@ContextConfiguration(locations={"classpath:app-context.xml","classpath:app-mongo.xml"})
public class MongoDBTester {
    
    @Autowired
    private MongoDBService mongoDBService;
    
    @Test
    public void testSave() {
        /*Book book = new Book();
        book.setId(2);
        book.setBookName("建筑的永恒之道");
        book.setPrice(30.6d);
        mongoDBService.save(book);*/
        
        for(int i=3;i<50;i++) {
            Book book = new Book();
            book.setId(i);
            book.setBookName("建筑的永恒之道"+i);
            book.setPrice(new Random().nextDouble()*100);
            mongoDBService.save(book);
        }
    }
    
    @Test
    public void testQuery1() {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").in(1));
        Book book = mongoDBService.findOne(Book.class, query);
        System.out.println("book---"+book.getBookName());
    }
    
    @Test
    public void testQuery2() {
        List<Book> books = mongoDBService.findAll(Book.class);
        for(Book book : books) {
            System.out.println(book.getBookName());
        }
    }
    
    @Test
    public void testQuery3() {
        Query query = new Query();
        query.addCriteria(Criteria.where("price").gt(40.0d));
        List<Book> books = mongoDBService.find(Book.class,query);
        for(Book book : books) {
            System.out.println(book.getBookName()+"----"+book.getPrice());
        }
    }
    
    @Test
    public void testQuery4() {
        Query query = new Query();
        Pagination<Book> pagination = mongoDBService.getPagination(Book.class, query, 1, 10);
        for(Book book : pagination.getRecordList()) {
            System.out.println(book.getBookName()+"----"+book.getPrice());
        }
    }
    
    @Test
    public void testUpdate() {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(10));
        Update update = new Update();
        update.set("bookName", "编程珠玑");
        update.set("price", 100.0d);
        int i = mongoDBService.update(query, update, Book.class);
        System.out.println(i);
        
    }
}

 

 

 

你可能感兴趣的:(mongodb)