<
pre
name
=
"code"
class
=
"html"
>package org.newyear.dao;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import org.bson.BasicBSONObject;
import org.newyear.model.Employee;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
public class MongodbDaoImpl implements MongodbDao {
static Mongo mongo;
static DB db;
static DBCollection empInfo;
static{
try {
mongo = new Mongo("localhost", 27017);
// 得到数据库java1211b
db = mongo.getDB("dudu");
empInfo = db.getCollection("emp");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 添加员工
* @param emp
*/
@Override
public void addEmp(Employee emp) {
BasicDBObject bd = new BasicDBObject();
if(empInfo.getCount()==0){
bd.put("_id", 1);
}else{
DBObject orderBy=new BasicDBObject();
orderBy.put("_id", -1);
DBCursor cursor = empInfo.find().sort(orderBy).limit(1);
DBObject next = cursor.next();
bd.put("_id", Integer.parseInt(((BasicBSONObject) next).getString("_id"))+1);
}
bd.put("name", emp.getName());
bd.put("sex", emp.getSex());
bd.put("age", emp.getAge());
bd.put("brith", emp.getBrith());
empInfo.save(bd);
}
/**
* 获取员工列表
* @param emp
* @return
*/
@Override
public List<
Employee
> getEmpList(Employee emp) {
List<
Employee
> listInfo=new ArrayList<
Employee
>();
DBObject orderBy=new BasicDBObject();
orderBy.put("_id", -1);
DBObject dbo = extracted(emp);
DBCursor cursor = empInfo.find(dbo).sort(orderBy).skip(emp.getStartPos()).limit(emp.getPageSize());
while(cursor.hasNext()){
Employee employee=new Employee();
BasicDBObject bdbObj = (BasicDBObject) cursor.next();
if(bdbObj != null){
employee.setId(Integer.parseInt(bdbObj.getString("_id")));
employee.setAge(bdbObj.getInt("age"));
employee.setName(bdbObj.getString("name"));
employee.setBrith(bdbObj.getString("brith"));
employee.setSex(bdbObj.getInt("sex"));
}
listInfo.add(employee);
}
for (Employee employeeInfo : listInfo) {
if(employeeInfo.getSex()==1){
employeeInfo.setSexView("男");
}else if(employeeInfo.getSex()==2){
employeeInfo.setSexView("女");
}
}
return listInfo;
}
/**
* 获取员工总条数
* @param emp
* @return
*/
@Override
public int getCountEmp(Employee emp) {
DBObject dbo = extracted(emp);
return (int) empInfo.getCount(dbo);
}
private DBObject extracted(Employee emp) {
//>=和<=操作
DBObject dbo=new BasicDBObject();
//根据年龄进行搜索
DBObject greateAndLess=null;
if(emp.getMinAge()!=0){
if(greateAndLess==null){
greateAndLess=new BasicDBObject();
greateAndLess.put("$gte", emp.getMinAge());
}
}
if(emp.getMaxAge()!=0){
if(greateAndLess==null){
greateAndLess=new BasicDBObject();
greateAndLess.put("$lte", emp.getMaxAge());
}else{
greateAndLess.put("$lte", emp.getMaxAge());
}
}
if(greateAndLess!=null){
dbo.put("age", greateAndLess);
}
//根据入职日期进行搜索
DBObject brithGL=null;
if(emp.getMinBrith()!=null && emp.getMinBrith().length()>0){
if(brithGL==null){
brithGL=new BasicDBObject();
brithGL.put("$gte", emp.getMinBrith());
}
}
if(emp.getMaxBrith()!=null && emp.getMaxBrith().length()>0){
if(brithGL==null){
brithGL=new BasicDBObject();
brithGL.put("$lte", emp.getMaxBrith());
}else{
brithGL.put("$lte", emp.getMaxBrith());
}
}
if(brithGL!=null){
dbo.put("brith", brithGL);
}
//根据性别进行搜素
BasicDBList count=null;
if(emp.getSex()!=0){
count=new BasicDBList();
count.add(emp.getSex());
}
if(count!=null){
dbo.put("sex", new BasicDBObject("$in",count));
}
//根据名字进行模糊搜索
if(emp.getName()!=null && emp.getName().length()>0){
Pattern pattern=Pattern.compile("^.*"+emp.getName()+".*$", Pattern.CASE_INSENSITIVE);
dbo.put("name", pattern);
}
return dbo;
}
/**
*修改回填 根据id中啊到对应的对象
* @param emp
* @return
*/
@Override
public Employee findEmp(Employee emp) {
Employee employeeInfo=new Employee();
DBObject findEmp=new BasicDBObject();
findEmp.put("_id", emp.getId());
DBCursor find = empInfo.find(findEmp);
while(find.hasNext()){
BasicDBObject next = (BasicDBObject) find.next();
employeeInfo.setId(Integer.parseInt(next.getString("_id")));
employeeInfo.setAge(next.getInt("age"));
employeeInfo.setName(next.getString("name"));
employeeInfo.setSex(next.getInt("sex"));
employeeInfo.setBrith(next.getString("brith"));
}
return employeeInfo;
}
/**
* 修改员工
* @param emp
*/
@Override
public void updateEmp(Employee emp) {
BasicDBObject bd = new BasicDBObject();
BasicDBObject bdInfo = new BasicDBObject();
bd.put("name", emp.getName());
bd.put("sex", emp.getSex());
bd.put("age", emp.getAge());
bd.put("brith", emp.getBrith());
bdInfo.put("_id", emp.getId());
empInfo.update(bdInfo, bd);
}
/**
* 批量删除
* @param ids
*/
@Override
public void delEmp(String ids) {
String[] split = ids.split(",");
BasicDBObject bd = new BasicDBObject();
BasicDBList count=new BasicDBList();
for (int i = 0; i <
split.length
; i++) {
count.add(Integer.parseInt(split[i]));
}
bd.put("_id", new BasicDBObject("$in",count));
empInfo.remove(bd);
}
}
</pre>