MongoDBindex.htmlindex.htmindex.jspdefault.htmldefault.htmdefault.jspMongoDBMongoDBorg.newyear.servlet.MongoDBServletMongoDB/MongoDB
/**
*修改页面
*/
/js/jquery-1.4.4.min.js" cdata_tag="script" _ue_custom_node_="true">/js/myJs/commonJs/commonJs.js" cdata_tag="script" _ue_custom_node_="true">/js/myJs/emp/emp.js" cdata_tag="script" _ue_custom_node_="true">修改页面
员工名称:
员工性别:
男 女
员工年龄:
入职日期:
package org.newyear.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.newyear.dao.MongodbDao;
import org.newyear.dao.MongodbDaoImpl;
import org.newyear.model.Employee;
import org.newyear.model.Page;
/**
* Servlet implementation class MongoDB
*/
public class MongoDBServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
@SuppressWarnings("unused")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=UTF-8");
MongodbDao mongdbDao=new MongodbDaoImpl();
String action = request.getParameter("action");
Employee emp=new Employee();
//跳转到添加页面
if(action!=null && action.equals("toAddEmp")){
request.getRequestDispatcher("/WEB-INF/view/mongodb/addEmp.jsp").forward(request, response);
//添加员工
}else if(action!=null && action.equals("addEmp")){
setEmp(request, emp);
mongdbDao.addEmp(emp);
response.sendRedirect(request.getContextPath()+"/MongoDB?action=getEmpList");
//获取员工并且分页 和 根据条件搜索
}else if(action!=null && action.equals("getEmpList")){
String name = request.getParameter("name");
if(name!=null && !name.equals("")){
emp.setName(name);
}
String sex = request.getParameter("sex");
if(sex!=null && !sex.equals("")){
emp.setSex(Integer.parseInt(sex));
}
String minAge = request.getParameter("minAge");
if(minAge!=null && !minAge.equals("")){
emp.setMinAge(Integer.parseInt(minAge));
}
String maxAge = request.getParameter("maxAge");
if(maxAge!=null && !maxAge.equals("")){
emp.setMaxAge(Integer.parseInt(maxAge));
}
String minBrith = request.getParameter("minBrith");
if(minBrith!=null && !minBrith.equals("")){
emp.setMinBrith(minBrith);
}
String maxBrith = request.getParameter("maxBrith");
if(maxBrith!=null && !maxBrith.equals("")){
emp.setMaxBrith(maxBrith);
}
int count=mongdbDao.getCountEmp(emp);
String pageIndex = request.getParameter("emp.pageIndex");
if(pageIndex!=null && !pageIndex.equals("")){
emp.setPageIndex(Integer.parseInt(pageIndex));
}
emp.setTotalCount(count);
emp.calculatePage();
Page page=emp;
ListlistEmp=mongdbDao.getEmpList(emp);
request.setAttribute("page", page);
request.setAttribute("listEmp", listEmp);
String parameter = request.getParameter("flag");
int flag = 0;
if(parameter!=null && ! parameter.equals("")){
flag=Integer.parseInt(parameter);
}
if(flag==1){
request.getRequestDispatcher("/WEB-INF/view/mongodb/PageListEmp.jsp").forward(request, response);
}else{
request.getRequestDispatcher("/WEB-INF/view/mongodb/getListInfoEmp.jsp").forward(request, response);
}
//跳转到修改页面
}else if(action!=null && action.equals("toUpdateEmp")){
String eid = request.getParameter("eid");
if(eid!=null){
emp.setId(Integer.parseInt(eid));
}
emp=mongdbDao.findEmp(emp);
request.setAttribute("emp", emp);
request.getRequestDispatcher("/WEB-INF/view/mongodb/updateEmp.jsp").forward(request, response);
//修改员工
}else if(action!=null && action.equals("updateEmp")){
setEmp(request, emp);
String id = request.getParameter("id");
emp.setId(Integer.parseInt(id));
mongdbDao.updateEmp(emp);
response.sendRedirect(request.getContextPath()+"/MongoDB?action=getEmpList");
//修改员工
}else if(action!=null && action.equals("delEmp")){
String ids = request.getParameter("ids");
mongdbDao.delEmp(ids);
response.sendRedirect(request.getContextPath()+"/MongoDB?action=getEmpList");
}
}
private void setEmp(HttpServletRequest request, Employee emp) {
String brith = request.getParameter("brith");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
if(brith!=null){
emp.setBrith(brith);
}
if(age!=null){
emp.setAge(Integer.parseInt(age));
}
if(name!=null){
emp.setName(name);
}
if(sex!=null){
emp.setSex(Integer.parseInt(sex));
}
}
}
/**
* 添加页面
* @param
*/
添加员工
员工名称:
员工性别:
男 女
员工年龄:
入职日期:
/**
* 展示页面
* @param
* @return
*/
/css/showLoading.css" rel="stylesheet" media="screen"/>/js/jquery-1.4.4.min.js" cdata_tag="script" _ue_custom_node_="true">/js/jsframework/showLoading/jquery.showLoading.min.js" cdata_tag="script" _ue_custom_node_="true">/js/myJs/commonJs/commonJs.js" cdata_tag="script" _ue_custom_node_="true">/js/myJs/emp/emp.js" cdata_tag="script" _ue_custom_node_="true">展示页面
姓名:
性别:
男 女
年龄:
到
入职日期:
到
展示页面
姓名
年龄
性别
入职日期
操作
${emp.name}
${emp.age}
${emp.sexView}
${emp.brith}
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 ListgetEmpList(Employee emp) {
ListlistInfo=new ArrayList();
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) {
//>=和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);
}
}
package org.newyear.model;
public class Employee extends Page{
private int id;
private String name;
private int sex;
private int age;
private String sexView;
private int minAge;
private int maxAge;
private String brith;
private String minBrith;
private String maxBrith;
public String getMinBrith() {
return minBrith;
}
public void setMinBrith(String minBrith) {
this.minBrith = minBrith;
}
public String getMaxBrith() {
return maxBrith;
}
public void setMaxBrith(String maxBrith) {
this.maxBrith = maxBrith;
}
public String getBrith() {
return brith;
}
public void setBrith(String brith) {
this.brith = brith;
}
public int getMinAge() {
return minAge;
}
public void setMinAge(int minAge) {
this.minAge = minAge;
}
public int getMaxAge() {
return maxAge;
}
public void setMaxAge(int maxAge) {
this.maxAge = maxAge;
}
public String getSexView() {
return sexView;
}
public void setSexView(String sexView) {
this.sexView = sexView;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSex() {
return sex;
}
public void setSex(int sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}