自己写的mongodb整合spring,有兴趣可以看一看,欢迎大家的点评!
开发环境
OS:Win7
VM Workstation版本:11
Linux版本:CentOS 6.7
Xshell版本:5
MongoDB版本:3.2
Spring版本:4.2
步骤 1 : 导入JAR
步骤 2 : 编辑spring.xml
<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 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 studyList) {
this.studyList = studyList;
}
public List 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 getStudentList(){
List studentList = mongoTemplate.find(null, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:单条件查询
* 作者:xxx
* 日期:2016-6-29下午11:28:11
* @param name
* @return
*/
public List getStudentList(String name){
// 查询条件:用户姓名
Criteria criteria = Criteria.where("user_name").is(name);
Query query = new Query(criteria);
List studentList = mongoTemplate.find(query, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:多条件查询
* 作者:xxx
* 日期:2016-6-29下午11:28:11
* @param name
* @return
*/
public List 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 studentList = mongoTemplate.find(query, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:模糊查询(单条件)
* 作者:xxx
* 日期:2016-6-29 下午11:28:11
* @param name
* @return
*/
public List getStudentListWithSingleFuzzy(String name){
// 查询条件:用户ID大于参数1,并且用户姓名为参数2
Criteria criteria = Criteria.where("user_name").regex(name);
Query query = new Query(criteria);
List studentList = mongoTemplate.find(query, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:模糊查询(多条件)
* 作者:xxx
* 日期:2016-6-29 下午11:28:11
* @param name
* @return
*/
public List 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 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 getStudentListWithPaging(int page, int pageSize){
Query query = new Query();
// 设置分页
query.skip((page - 1) * pageSize).limit(pageSize);
List 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 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 studentList = mongoTemplate.find(query, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:按单字段排序(不带条件)
* 作者:xxx
* 日期:2016-6-29 下午11:28:11
* @param name
* @return
*/
public List getStudentListWithSingleSort(Direction direction){
Query query = new Query();
// 设置排序
Sort sort = new Sort(direction, "user_id");
query.with(sort);
List studentList = mongoTemplate.find(query, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:按多字段排序(不带条件)
* 作者:xxx
* 日期:2016-6-29 下午11:28:11
* @param name
* @return
*/
public List getStudentListWithMultiSort(Direction direction){
Query query = new Query();
// 设置多个排序字段
Sort sort = new Sort(direction, "user_id").and(new Sort(direction, "user_name"));
query.with(sort);
List studentList = mongoTemplate.find(query, Student.class, "t_user");
return studentList;
}
/**
* 方法描述:按单字段排序(带条件)
* 作者:xxx
* 日期:2016-6-29 下午11:28:11
* @param name
* @return
*/
public List 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 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 studentList = studentDao.getStudentListWithMultiFuzzy(student);
for (Student student2 : studentList) {
System.out.println(student2.getUser_name());
}
System.out.println("end");
}
}