【代码片-7】 基于jsp+javabean+servlet+mongodb 增删改查

来源:互联网 发布:淘宝运营职位 编辑:程序博客网 时间:2024/05/23 18:31

作者:u013045746----感谢原创,感谢作者!!

标签:mongodb    增删改查    javabean    servlet    jsp    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<prename="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>