Dao 层
package com.aaa.dao;
import java.util.*;
public interface copyDao {
//查询所有员工信息
List<Map<String,Object>> queryAllYg();
//添加员工信息
int add(Object[]obj );
//删除员工信息
int dele(int id);
//查询需要修改的信息
List<Map<String,Object>> upQuery(int id);
//修改员工信息
int update(Object[]obj);
//遍历查询证件类型
List<Map<String,Object>>Idquery();
//根据一个id查询一条数据
Map queryById(Object id);
}
daoimpl
package com.aaa.dao.impl;
import com.aaa.dao.BaseDao;
import com.aaa.dao.copyDao;
import java.util.List;
import java.util.Map;
public class copyDaoimpl implements copyDao {
@Override
public List<Map<String, Object>> queryAllYg() {
String sql = "select a.id,a.username,a.sex,a.age,b.job\n" +
"FROM yuangong a,bumen b\n" +
"where a.job = b.id";
return BaseDao.executeQuery(sql);
}
@Override
public int add(Object[] obj) {
String sql = "insert into yuangong (username,sex,age,job)values(?,?,?,?) ";
return BaseDao.insertDeleUp(sql,obj);
}
@Override
public int dele(int id) {
String sql = "delete from yuangong where id = ?";
return BaseDao.insertDeleUp(sql,id);
}
@Override
public List<Map<String, Object>> upQuery(int id) {
String sql = "select a.username,a.sex,a.age,b.job\n" +
"from yuangong a,bumen b \n" +
"where a.job = b.id and a.id = ?";
return BaseDao.executeQuery(sql,id);
}
@Override
public int update(Object[] obj) {
String sql = "update yuangong set username = ?,sex = ?, age = ?, job = ? \n" +
"where id = ?";
return BaseDao.insertDeleUp(sql,obj);
}
@Override
public List<Map<String, Object>> Idquery() {
String sql = "select * from bumen";
return BaseDao.executeQuery(sql);
}
@Override
public Map queryById(Object id) {
String sql = "select * from yuangong where id =?";
return BaseDao.executeQuery(sql,id).get(0);
}
}
service
package com.aaa.service;
import java.util.List;
import java.util.Map;
public interface copysetvice {
//查询所有员工信息
List<Map<String,Object>> queryAllYg();
//添加员工信息
int add(Object[]obj );
//删除员工信息
int dele(int id);
//查询需要修改的信息
List<Map<String,Object>> upQuery(int id);
//修改员工信息
int update(Object[]obj);
//遍历查询证件类型
List<Map<String,Object>>Idquery();
//根据一个id查询一条数据
Map queryById(Object id);
}
serviceImpl
package com.aaa.service.impl;
import com.aaa.dao.copyDao;
import com.aaa.dao.impl.copyDaoimpl;
import com.aaa.service.copysetvice;
import java.util.List;
import java.util.Map;
public class copyserviceimpl implements copysetvice {
copyDao copyDao = new copyDaoimpl();
@Override
public List<Map<String, Object>> queryAllYg() {
return copyDao.queryAllYg();
}
@Override
public int add(Object[] obj) {
return copyDao.add(obj);
}
@Override
public int dele(int id) {
return copyDao.dele(id);
}
@Override
public List<Map<String, Object>> upQuery(int id) {
return copyDao.upQuery(id);
}
@Override
public int update(Object[] obj) {
return copyDao.update(obj);
}
@Override
public List<Map<String, Object>> Idquery() {
return copyDao.Idquery();
}
@Override
public Map queryById(Object id) {
return copyDao.queryById(id);
}
}
servlet
package com.aaa.servlet;
import com.aaa.dao.BaseDao;
import com.aaa.service.bumenService;
import com.aaa.service.copysetvice;
import com.aaa.service.impl.bumenServiceimpl;
import com.aaa.service.impl.copyserviceimpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
@WebServlet("/copyServlet")
public class copyServlet extends HttpServlet {
copysetvice service = new copyserviceimpl();
bumenService bumenService = new bumenServiceimpl();
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String op = request.getParameter("op");
switch (op){
case "queryAll":
queryAll(request,response);
break;
case "add":
add(request,response);
break;
case "dele":
del(request,response);
break;
//查询修改
case "uodatequery":
queryUp(request,response);
break;
//修改
case "upMessage":
update(request,response);
break;
case "goadd":
goadd(request,response);
break;
}
}
//查询所有员工
public void queryAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Map<String,Object>> list = service.queryAllYg();
request.setAttribute("list",list);
request.getRequestDispatcher("allThings.jsp").forward(request,response);
}
//删除员工
public void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
if(service.dele(id)!=0);
response.sendRedirect("/copyServlet?op=queryAll");
}
//查询修改员工
public void queryUp(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//根据map来获取一条数据
int ids = Integer.parseInt(request.getParameter("id"));
Map map = service.queryById(ids);
request.setAttribute("map",map);
//部门类型下拉框
request.setAttribute("typeList",bumenService.queryType());
request.getRequestDispatcher("uodate.jsp").forward(request,response);
}
//修改员工
public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int ides = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("user");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String typeId = request.getParameter("bumen");
Object[]os = {name,sex,age,typeId,ides};
service.update(os);
response.sendRedirect("/copyServlet?op=queryAll");
}
//添加员工
public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("user");
String sex = request.getParameter("sex");
String age = request.getParameter("age");
String type = request.getParameter("bumen");
Object[]os = {name,sex,age,type};
service.add(os);
response.sendRedirect("/copyServlet?op=queryAll");
}
public void goadd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int ids = Integer.parseInt(request.getParameter("id"));
Map map = service.queryById(ids);
request.setAttribute("map",map);
request.setAttribute("typeList",bumenService.queryType());
request.getRequestDispatcher("add.jsp").forward(request,response);
}
}
下拉框类型查询
类型 Dao层
package com.aaa.dao;
import java.util.*;
public interface bumenDao {
//部门下拉框查询
List<Map<String,Object>> queryType();
}
类型 daoimpl
daoImpl
package com.aaa.dao.impl;
import com.aaa.dao.BaseDao;
import com.aaa.dao.bumenDao;
import java.util.List;
import java.util.Map;
public class bumenDaoimpl extends BaseDao implements bumenDao {
@Override
public List<Map<String, Object>> queryType() {
return BaseDao.executeQuery("select * from bumen");
}
}
``
service
package com.aaa.service;
import java.util.List;
import java.util.Map;
public interface bumenService {
//部门下拉框查询
List<Map<String,Object>> queryType();
}
serviceimp
package com.aaa.service.impl;
import com.aaa.dao.bumenDao;
import com.aaa.dao.impl.bumenDaoimpl;
import com.aaa.service.bumenService;
import java.util.List;
import java.util.Map;
public class bumenServiceimpl implements bumenService {
bumenDao dao = new bumenDaoimpl();
@Override
public List<Map<String, Object>> queryType() {
return dao.queryType();
}
}
allThings.jsp(主页面)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: dell
Date: 2019/12/23
Time: 16:01
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table border="1px solid black;" cellspacing="0px"style="width: 800px;height: 100px;font-size: 30px;text-align: center;">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>部门</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="c" varStatus="copy">
<tr>
<td>${copy.count}</td>
<td>${c.username}</td>
<td>${c.sex}</td>
<td>${c.age}</td>
<td>${c.job}</td>
<td><a href="/copyServlet?op=dele&&id=${c.id}">删除</a></td>
<td><a href="/copyServlet?op=uodatequery&&id=${c.id}">修改</a></td>
</tr>
</c:forEach>
</table>
<a href="/copyServlet?op=goadd&&id=${list.get(0).id}"><button>添加</button></a>
</body>
</html>
add.jsp(添加)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: dell
Date: 2019/12/23
Time: 16:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/copyServlet?op=add" method="post">
<table>
<tr>
<td>姓名:<input type="text" name = "user"></td>
<td>性别:男<input type="radio" value="男" name = "sex">
女<input type="radio" value="女" name = "sex"></td>
<td>年龄:<input type="text" name="age"></td>
<td>
部门
<select name="bumen">
<c:forEach items="${typeList}" var="type">
<option value="${type.id}"
<c:if test="${map.job == type.id}">
selected="selected"
</c:if>
>
${type.job}
</option>
</c:forEach>
</select>
</td>
</tr>
</table>
<input type="submit" value="添加">
</form>
</body>
</html>
update.jsp(修改)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: dell
Date: 2019/12/23
Time: 20:06
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="/copyServlet?op=upMessage&&id=${map.id}" method="post">
<table>
<tr>
<td>姓名:<input type="text" name="user" value="${map.username}"></td>
<td>年龄:<input type="text" name="age" value="${map.age}"></td>
<td>性别:<input type="text" name="sex" value="${map.sex}"></td>
<td>
部门
<select name="bumen">
<c:forEach items="${typeList}" var="type">
<option value="${type.id}"
<c:if test="${map.job == type.id}">
selected="selected"
</c:if>
>
${type.job}
</option>
</c:forEach>
</select>
</td>
</tr>
</table>
<input type="submit" value="提交">
</form>
</body>
</html>
这篇博客详细介绍了在Java Web环境中,如何实现员工信息的增删改查功能。涵盖了Dao层、DaoImpl实现、Service接口及其实现、Servlet控制层以及前端页面如allThings.jsp主页面、add.jsp添加页和update.jsp修改页的设计和交互,特别提到了下拉框类型的查询操作和不同类型的数据操作在DaoImpl和服务层的具体实现。

795

被折叠的 条评论
为什么被折叠?



