mongoDB整合spring

自己寫的mongodb整合spring,有興趣可以看一看,歡迎大家的點評!

開發環境

OS:Win7
VM Workstation版本:11
Linux版本:CentOS 6.7
Xshell版本:5

MongoDB版本:3.2
Spring版本:4.2

步驟 1 : 導入JAR

這裏寫圖片描述

步驟 2 : 編輯spring.xml

<?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:aop="http://www.springframework.org/schema/aop"   
xmlns:context="http://www.springframework.org/schema/context"  
xmlns:jee="http://www.springframework.org/schema/jee"  
xmlns:lang="http://www.springframework.org/schema/lang"  
xmlns:util="http://www.springframework.org/schema/util"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xmlns:mvc="http://www.springframework.org/schema/mvc"    
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/aop    
 http://www.springframework.org/schema/aop/spring-aop.xsd   
 http://www.springframework.org/schema/jee    
 http://www.springframework.org/schema/jee/spring-jee.xsd   
 http://www.springframework.org/schema/lang    
 http://www.springframework.org/schema/lang/spring-lang.xsd   
 http://www.springframework.org/schema/context    
 http://www.springframework.org/schema/context/spring-context.xsd   
 http://www.springframework.org/schema/tx    
 http://www.springframework.org/schema/tx/spring-tx.xsd   
 http://www.springframework.org/schema/util    
 http://www.springframework.org/schema/util/spring-util.xsd   
 http://www.springframework.org/schema/mvc    
 http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
 http://www.springframework.org/schema/data/mongo       
 http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd ">

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

    <mongo:mongo id="mongo" host="ip地址" port="27017"/>

    <bean id="mongoDbFactory" class="org.springframework.data.mongodb.core.SimpleMongoDbFactory">
        <constructor-arg ref="mongo"></constructor-arg>
        <constructor-arg value="admin"></constructor-arg>
    </bean>

    <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
        <constructor-arg name="mongoDbFactory" ref="mongoDbFactory"></constructor-arg>
    </bean>

</beans>

步驟3: 實體類

package bean;

import java.util.List;

public class Student{

    private Double user_id;

    private String user_name;

    private List<Study> studyList;

    public void setUser_id(Double user_id) {
        this.user_id = user_id;

    }

    public Double getUser_id() {
        return user_id;
    }

    public void setUser_name(String user_name) {
        this.user_name = user_name;
    }

    public String getUser_name() {
        return user_name;
    }

    public void setStudyList(List<Study> studyList) {


        this.studyList = studyList;
    }

    public List<Study> getStudyList() {


        return studyList;
    }

}

步驟 4 : 在DAO中實現常用功能

/**
 * 
 */
package dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
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.stereotype.Repository;

import bean.Student;

/**
 * 類描述:
 * 作者:xxx
 * 日期:2016-6-27下午11:28:46
 *
 */
@Repository
public class StudentDao {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 方法描述:無條件查詢
     * 作者:xxx
     * 日期:2016-6-29 下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentList(){

        List<Student> studentList = mongoTemplate.find(null, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:單條件查詢
     * 作者:xxx
     * 日期:2016-6-29下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentList(String name){

        // 查詢條件:用戶姓名
        Criteria criteria = Criteria.where("user_name").is(name);

        Query query = new Query(criteria);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:多條件查詢
     * 作者:xxx
     * 日期:2016-6-29下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentList(Double id, String name){

        // 查詢條件:用戶ID大於參數1,並且用戶姓名爲參數2
        Criteria criteria = Criteria.where("user_id").gt(id).and("user_name").is(name);

        Query query = new Query(criteria);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:模糊查詢(單條件)
     * 作者:xxx
     * 日期:2016-6-29 下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentListWithSingleFuzzy(String name){

        // 查詢條件:用戶ID大於參數1,並且用戶姓名爲參數2
        Criteria criteria = Criteria.where("user_name").regex(name);

        Query query = new Query(criteria);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:模糊查詢(多條件)
     * 作者:xxx
     * 日期:2016-6-29 下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentListWithMultiFuzzy(Student student){

        // 查詢條件:用戶姓名模糊匹配[參數1],並且用戶ID大於[參數2]
        Criteria criteria = Criteria.where("user_name").regex(student.getUser_name()).and("user_id").gt(student.getUser_id());

        Query query = new Query(criteria);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:分頁(無條件)
     * 作者:xxx
     * 日期:2016-6-29下午11:42:16
     * @param page   頁數(從1開始)
     * @param pageSize  每頁顯示記錄數量
     * @param name  查詢條件
     * @return
     */
    public List<Student> getStudentListWithPaging(int page, int pageSize){

        Query query = new Query();

        // 設置分頁
        query.skip((page - 1) * pageSize).limit(pageSize);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:分頁(帶條件)
     * 作者:xxx
     * 日期:2016-6-29下午11:42:16
     * @param page   頁數(從1開始)
     * @param pageSize  每頁顯示記錄數量
     * @param name  查詢條件
     * @return
     */
    public List<Student> getStudentListWithPaging(int page, int pageSize, String name){

        // 查詢條件:用戶ID大於參數1,並且用戶姓名爲參數2
        Criteria criteria = Criteria.where("user_name").regex(name);

        Query query = new Query(criteria);

        // 設置分頁
        query.skip((page - 1) * pageSize).limit(pageSize);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:按單字段排序(不帶條件)
     * 作者:xxx
     * 日期:2016-6-29 下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentListWithSingleSort(Direction direction){

        Query query = new Query();

        // 設置排序
        Sort sort = new Sort(direction, "user_id");
        query.with(sort);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:按多字段排序(不帶條件)
     * 作者:xxx
     * 日期:2016-6-29 下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentListWithMultiSort(Direction direction){

        Query query = new Query();

        // 設置多個排序字段
        Sort sort = new Sort(direction, "user_id").and(new Sort(direction, "user_name"));
        query.with(sort);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:按單字段排序(帶條件)
     * 作者:xxx
     * 日期:2016-6-29 下午11:28:11
     * @param name
     * @return
     */
    public List<Student> getStudentListWithSingleSort(Direction direction, String name){

        // 查詢條件:用戶姓名中包含參數[name]
        Criteria criteria = Criteria.where("user_name").regex(name);

        Query query = new Query(criteria);

        // 設置排序
        Sort sort = new Sort(direction, "user_id");
        query.with(sort);

        List<Student> studentList = mongoTemplate.find(query, Student.class, "t_user");

        return studentList;
    }

    /**
     * 方法描述:插入
     * 作者:xxx
     * 日期:2016-6-30 上午12:12:40
     * @param student
     */
    public void insert(Student student){

        mongoTemplate.insert(student, "t_user");
    }

    /**
     * 方法描述:更新單條記錄
     * 作者:xxx
     * 日期:2016-6-30 上午12:12:40
     * @param student
     */
    public void update(Student student){

        // 更新條件
        Criteria criteria = Criteria.where("user_id").is(student.getUser_id());
        Query query = new Query(criteria);

        // 更新內容
        Update update = new Update();
        update.set("user_name", student.getUser_name());

        mongoTemplate.updateFirst(query, update, "t_user");
    }

    /**
     * 方法描述:更新多條記錄
     * 作者:xxx
     * 日期:2016-6-30 上午12:12:40
     * @param student
     */
    public void updateBatch(Student student){

        // 更新條件:用戶ID >= [參數]
        Criteria criteria = Criteria.where("user_id").gte(student.getUser_id());
        Query query = new Query(criteria);

        // 更新內容:用戶姓名 = [參數]
        Update update = new Update();
        update.set("user_name", student.getUser_name());

        mongoTemplate.updateMulti(query, update, "t_user");
    }

    /**
     * 方法描述:刪除符合條件的記錄
     * 作者:xxx
     * 日期:2016-6-30 上午12:12:40
     * @param student
     */
    public void delete(Student student){

        // 刪除條件:用戶ID = [參數]
        Criteria criteria = Criteria.where("user_id").is(student.getUser_id());
        Query query = new Query(criteria);

        mongoTemplate.remove(query, "t_user");
    }

    /**
     * 方法描述:級聯查詢
     * 作者:xxx
     * 日期:2016-6-30 上午09:24:48
     * @param student
     */
    public void getStudentListWithCascate(Student student){

        // 查詢條件:用戶姓名 中包含 [參數]
        Criteria criteria = Criteria.where("user_name").regex(student.getUser_name());
        Query query = new Query(criteria);

        mongoTemplate.find(query, Student.class, "t_user");
    }

}

步驟 5 : 運行

/**
 * 
 */
package demo;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bean.Student;

import dao.StudentDao;

/**
 * 類描述:整合演示
 * 作者:xxx
 * 日期:2016-6-29下午11:23:05
 *
 */
public class MongoDB_Spring {

    /**
     * @param args
     */
    public static void main(String[] args) {

        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");

        StudentDao studentDao = (StudentDao) ac.getBean("studentDao");

        Student student = new Student();

        student.setUser_name("李");
        student.setUser_id(7d);

        List<Student> studentList = studentDao.getStudentListWithMultiFuzzy(student);

        for (Student student2 : studentList) {

            System.out.println(student2.getUser_name());
        }

        System.out.println("end");
    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章