接口IMongDaoFind:
package com.net.test.mongdb.dao; public interface IMongDaoFind { public void findUsers(); public void findUserByName(String name); public void findUserLikeName(String name); public void findUserPs(); public void findUserAndOperator(); public void findUserOrOperator(); public void findUserOrOperator2(); }
实现类MongDaoFindImp:
package com.net.test.mongdb.dao.imp; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; 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.stereotype.Repository; import com.net.test.mongdb.dao.IMongDaoFind; import com.net.test.mongdb.entity.User; import com.net.test.mongdb.entity.UserP; import com.net.test.mongdb.entity.UserPsg; /** * @author *** * @Time:2017年8月4日 上午10:40:33 * @version 1.0 * Function: TODO */ @Repository public class MongDaoFindImp implements IMongDaoFind{ @Resource protected MongoTemplate mongoTemplate; /** * @description 查询所有用户信息 */ @Override public void findUsers() { List<User> list = mongoTemplate.findAll(User.class); for(User user : list) { System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex()); } } /** * @description 根据用户名,单条查询用户信息.如果有重复名称,默认查询第一条 * @param name */ public void findUserByName(String name) { Query query = Query.query(Criteria.where("name").is(name)); User user = mongoTemplate.findOne(query,User.class); System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex()); } /** * @description 模糊查询 * @param name */ @Override public void findUserLikeName(String name) { Query query = new Query(); query.addCriteria(Criteria.where("name").regex(name)); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex()); } } /** * @description 表名与pojo不相同进行映射 */ @Override public void findUserPs() { List<UserP> list = mongoTemplate.findAll(UserP.class, "user"); for(UserP user : list) { System.out.println("name :" + user.getName() + " , age :" + user.getAge() + " , sex :" + user.getSex() + " , hobby :" + user.getHobby()); } } /** * @description 测试多条件进行查询,测试and连接符 */ @Override public void findUserAndOperator() { Query query = new Query(); Criteria c = new Criteria(); query.addCriteria(c.andOperator(Criteria.where("name").regex("车"), Criteria.where("age").is(18))); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { //基础信息 String name = user.getName(); int age = user.getAge(); String sex = user.getSex(); //内置对象 UserPsg psg = user.getPsg(); //内置数组 ArrayList<String> listHobby = (ArrayList<String>) user.getHobby(); StringBuffer bf = new StringBuffer(); if(null != listHobby && listHobby.size() > 0) { for(String hobby : listHobby) { bf.append(hobby + ","); } }else { bf.append(""); } System.out.println( "name : " + name + ", age : " + age + ", sex : " + sex + ", hobby : " + bf.toString() + ", UserPsg : " + psg ); } } /** * @description 测试多条件进行查询,测试平行并列or */ @Override public void findUserOrOperator() { Query query = new Query(); Criteria c = new Criteria(); query.addCriteria(c.orOperator(Criteria.where("name").regex("车"), Criteria.where("age").is(10))); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { //基础信息 String name = user.getName(); int age = user.getAge(); String sex = user.getSex(); //内置对象 UserPsg psg = user.getPsg(); //内置数组 ArrayList<String> listHobby = (ArrayList<String>) user.getHobby(); StringBuffer bf = new StringBuffer(); if(null != listHobby && listHobby.size() > 0) { for(String hobby : listHobby) { bf.append(hobby + ","); } }else { bf.append(""); } System.out.println( "name : " + name + ", age : " + age + ", sex : " + sex + ", hobby : " + bf.toString() + ", UserPsg : " + psg ); } } /** * @description 测试多条件进行查询,测试包含关系or连接符,此处的or和and有些相似 */ @Override public void findUserOrOperator2() { Query query = new Query(); query.addCriteria(Criteria.where("name").regex("车").orOperator(Criteria.where("age").is(19))); List<User> list = mongoTemplate.find(query, User.class); for(User user : list) { //基础信息 String name = user.getName(); int age = user.getAge(); String sex = user.getSex(); //内置对象 UserPsg psg = user.getPsg(); //内置数组 ArrayList<String> listHobby = (ArrayList<String>) user.getHobby(); StringBuffer bf = new StringBuffer(); if(null != listHobby && listHobby.size() > 0) { for(String hobby : listHobby) { bf.append(hobby + ","); } }else { bf.append(""); } System.out.println( "name : " + name + ", age : " + age + ", sex : " + sex + ", hobby : " + bf.toString() + ", UserPsg : " + psg ); } } }