赞
踩
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
- <display-name>MongoDB</display-name>
- <welcome-file-list>
- <welcome-file>index.html</welcome-file>
- <welcome-file>index.htm</welcome-file>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>default.html</welcome-file>
- <welcome-file>default.htm</welcome-file>
- <welcome-file>default.jsp</welcome-file>
- </welcome-file-list>
- <servlet>
- <description></description>
- <display-name>MongoDB</display-name>
- <servlet-name>MongoDB</servlet-name>
- <servlet-class>org.newyear.servlet.MongoDBServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>MongoDB</servlet-name>
- <url-pattern>/MongoDB</url-pattern>
- </servlet-mapping>
- </web-app>
- /**
- *修改页面
- */
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ include file="/common/common.jsp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.4.min.js"></script>
- <script src="<%=request.getContextPath() %>/js/myJs/commonJs/commonJs.js"></script>
- <script src="<%=request.getContextPath()%>/js/myJs/emp/emp.js"></script>
- <title>修改页面</title>
- </head>
- <body onload="getContextPath('<%=request.getContextPath()%>');selectMenu(${emp.sex})">
- <form action="<%=request.getContextPath() %>/MongoDB?action=updateEmp" method="post" >
- <table>
- <tr>
- <td>员工名称:</td>
- <td>
- <input type="hidden" name="id" value="${emp.id}">
- <input type="text" name="name" value="${emp.name}">
- </td>
- </tr>
- <tr>
- <td>员工性别:</td>
- <td>
- <input type="radio" name="sex" value="1"/> 男
- <input type="radio" name="sex" value="2"/> 女
- </td>
- </tr>
- <tr>
- <td>员工年龄:</td>
- <td><input type="text" name="age" value="${emp.age}"></td>
- </tr>
- <tr>
- <td>入职日期:</td>
- <td>
- <input type="text" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy年MM月dd日'})" value="${emp.brith}" name="brith">
- </td>
- </tr>
- <tr>
- <td><input type="submit" value="修改员工"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- 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;
- List<Employee> listEmp=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
- */
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ include file="/common/common.jsp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>添加员工</title>
- </head>
- <body>
- <form action="<%=request.getContextPath() %>/MongoDB?action=addEmp" method="post" >
- <table>
- <tr>
- <td>员工名称:</td>
- <td><input type="text" name="name"></td>
- </tr>
- <tr>
- <td>员工性别:</td>
- <td>
- <input type="radio" name="sex" value="1"> 男
- <input type="radio" name="sex" value="2"/> 女
- </td>
- </tr>
- <tr>
- <td>员工年龄:</td>
- <td><input type="text" name="age"></td>
- </tr>
- <tr>
- <td>入职日期:</td>
- <td>
- <input type="text" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy年MM月dd日'})" name="brith">
- </td>
- </tr>
- <tr>
- <td><input type="submit" value="添加员工"></td>
- </tr>
- </table>
- </form>
- </body>
- </html>
- /**
- * 展示页面
- * @param
- * @return
- */
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ include file="/common/common.jsp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <link href="<%=request.getContextPath() %>/css/showLoading.css" rel="stylesheet" media="screen" />
- <script type="text/javascript" src="<%=request.getContextPath()%>/js/jquery-1.4.4.min.js"></script>
- <script type="text/javascript" src="<%=request.getContextPath() %>/js/jsframework/showLoading/jquery.showLoading.min.js"></script>
- <script src="<%=request.getContextPath() %>/js/myJs/commonJs/commonJs.js"></script>
- <script src="<%=request.getContextPath()%>/js/myJs/emp/emp.js"></script>
- <title>展示页面</title>
- </head>
- <body onload="getContextPath('<%=request.getContextPath()%>')">
- <form>
- <table onkeypress="EnterPress()">
- <tr>
- <td>姓名:</td>
- <td><input type="text" id="name" size="5"></td>
- </tr>
- <tr>
- <td>性别:</td>
- <td>
- <input type="radio" name="sex" value="1"> 男
- <input type="radio" name="sex" value="2"/> 女
- </td>
- </tr>
- <tr>
- <td>年龄:</td>
- <td>
- <input type="text" id="minAge" size="5">到
- <input type="text" id="maxAge" size="5">
- </td>
- </tr>
- <tr>
- <td>入职日期:</td>
- <td>
- <input type="text" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy年MM月dd日'})" id="minBrith">到
- <input type="text" class="Wdate" onClick="WdatePicker({dateFmt:'yyyy年MM月dd日'})" id="maxBrith">
- </td>
- </tr>
- <tr>
- <td>
- <input type="button" value="搜索" onclick="search(1)"/>
- </td>
- <td>
- <input type="reset" value="重置"/>
- <input type="button" value="添加员工" onclick="addEmp()"/>
- <input type="button" value="删除员工" onclick="delEmp()"/>
- </td>
- </tr>
- </table>
- </form>
- <div id="empId">
- <jsp:include page="/WEB-INF/view/mongodb/PageListEmp.jsp"></jsp:include>
- </div>
- </body>
- </html>
- <%@ page language="java" contentType="text/html; charset=UTF-8"
- pageEncoding="UTF-8"%>
- <%@ include file="/common/common.jsp" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <title>展示页面</title>
- <style>
- #emp {
- text-align:center;
- width: 500px;
- border-collapse: collapse;
- }
- #emp th, #emp td {
- border: 1px solid #666;
- border-collapse: collapse;
- }
- </style>
- </head>
- <body>
- <table id="emp">
- <tr>
- <td>姓名</td>
- <td>年龄</td>
- <td>性别</td>
- <td>入职日期</td>
- <td>操作 <input type="button" value="反选" onclick="fanxuan()"/></td>
- </tr>
- <c:forEach items="${listEmp}" var="emp">
- <tr>
- <td>${emp.name}</td>
- <td>${emp.age}</td>
- <td>${emp.sexView}</td>
- <td>${emp.brith}</td>
- <td>
- <input type="checkbox" name="cb" value='${emp.id}'/>
- <input type="button" value="修改信息" onclick="updateEmp('${emp.id}')" />
- </td>
- </tr>
- </c:forEach>
- </table>
- <jsp:include page="/common/ajaxpage.jsp"></jsp:include>
- </body>
- </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);
- }
- }
- 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;
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。