1、创建addemp.jsp
导入jstl配置与bootstrap配置(几个标签)
去bootstrap手册找到弹出模态框 (Modal)或者表单选个合适的
编辑addemp.jsp
2、因为JSP文件放到了WEB-INF文件里,不能直接访问,所以添加一个访问jsp文件的servlet
AddEmpViewServlet 之后直接访问http://localhost:8080/addempview即可访问到addemp.jsp
@WebServlet(urlPatterns = "/addempview")
public class AddEmpViewServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.getRequestDispatcher("\\WEB-INF\\addemp.jsp").forward(req,resp);
}
}
3、addemp.jsp
注意:
1.添加name=“”属性 提交数据需要name
2.注意表单提交位置和格式
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
添加员工信息
4、写增加表单的逻辑AddEmpServlet,在此之前写mybatis那一套
EmpMapper.xml
insert into t_emp(name,age,sex,salary) value (#{name},#{age},#{sex},#{salary})
EmpMapper
int saveEmp(Emp emp);
EmpService
int saveEmp(Emp emp);
EmpServiceImpl
@Override
public int saveEmp(Emp emp) {
SqlSession sqlSession = MybatisUtil.getSqlSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
int row = empMapper.saveEmp(emp);
sqlSession.close();
return row;
}
5、写增加表单的逻辑AddEmpServlet
注意:@WebServlet(urlPatterns = {"/addemp"})
地址中/
@WebServlet(urlPatterns = {"/addemp"})
public class AddEmpServlet extends HttpServlet {
private EmpService empService = new EmpServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");/*防止提交表单中文乱码,告诉服务器utf-8解码*/
String name = req.getParameter("name");
Integer age = Integer.parseInt(req.getParameter("age"));
String sex = req.getParameter("sex");
BigDecimal salary = new BigDecimal(req.getParameter("salary"));
// String deptId = req.getParameter("deptId");
Emp parm = new Emp();
parm.setName(name);
parm.setAge(age);
parm.setSex(sex);
parm.setSalary(salary);
/* System.out.println(name);
System.out.println(age);
System.out.println(sex);
System.out.println(salary);*/
empService.saveEmp(parm);
// 跳转页面到员工列表页 重定向
resp.sendRedirect("emplist");
}
}
============================================================================
处理数据库中 部门相关部分 使之显示并可添加
第一个点
1、列表页添加添加页的入口
emplist.jsp
增加员工
2、员工对应部门 多对一 mybatis里简化成一对一 由此:
·首先要根据开闭原则:对扩展开放,对修改关闭
首先创建Dept实体类,修改Emp实体类 给 EmpMapper.xml 做准备
@Data
public class Dept {
private Integer deptId;
private String dname;
private String location;
}
修改Emp实体类 添加
private Dept dept;
EmpMapper.xml
将的listEmpDept放入EmpMapper映射
ListlistEmpDept();
EmpService一份
ListlistEmpDept();
EmpServiceImpl实现类去实现
@Override
public List listEmpDept() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
List empList = empMapper.listEmpDept();
sqlSession.close();
return empList;
}
去修改一下EmpListServlet,
在步骤1、查询数据库中,使用新方法listEmpDept()
//1、查询数据库
List empList = empService.listEmpDept();
System.out.println(empList);
如果没有配置web.xml,去配置
emplist
则刷新页面http://localhost:8080/emplist
看输出台
Preparing: select emp_id,name,age,sex,salary,e.dept_id did,dname,location from t_emp e left join t_dept d on e.dept_id=d.dept_id
==> Parameters:
<== Columns: emp_id, name, age, sex, salary, did, dname, location
<== Row: 1, 张三, 19, 男, 20000.00, 1, 技术部, 保定市
此时去修改页面emplist.jsp
${emp.deptId}
换成
${emp.dept.dname}
===========================================================================
第二个点
添加人员,部门使用表单的下拉条
又是数据库那一套
创建DeptMapper.xml
创建接口DeptMapper
public interface DeptMapper {
List listDept();
创建接口DeptService
public interface DeptService {
List listDept();
}
创建实现类DeptServiceImpl实现接口DeptService
public class DeptServiceImpl implements DeptService {
@Override
public List listDept() {
SqlSession sqlSession = MybatisUtil.getSqlSession();
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
List deptList = deptMapper.listDept();
sqlSession.close();
return deptList;
}
}
然后编辑跳转页AddEmpViewServlet添加业务层
@WebServlet(urlPatterns = "/addempview")
public class AddEmpViewServlet extends HttpServlet {
private DeptService deptService = new DeptServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List deptList = deptService.listDept();
req.setAttribute("deptlist",deptList);
req.getRequestDispatcher("\\WEB-INF\\addemp.jsp").forward(req,resp);
}
}
编辑addemp.jsp 添加部门数据
将表单提交数据,加入数据库
AddEmpServlet
@WebServlet(urlPatterns = {"/addemp"})
public class AddEmpServlet extends HttpServlet {
private EmpService empService = new EmpServiceImpl();
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");/*防止提交表单中文乱码,告诉服务器utf-8解码*/
String name = req.getParameter("name");
Integer age = Integer.parseInt(req.getParameter("age"));
String sex = req.getParameter("sex");
BigDecimal salary = new BigDecimal(req.getParameter("salary"));
Integer deptId = Integer.parseInt(req.getParameter("deptId"));/*注意这里*/
// String deptId = req.getParameter("deptId");
Emp parm = new Emp();
parm.setName(name);
parm.setAge(age);
parm.setSex(sex);
parm.setSalary(salary);
parm.setDeptId(deptId);/*注意这里*/
/* System.out.println(name);
System.out.println(age);
System.out.println(sex);
System.out.println(salary);*/
empService.saveEmp(parm);
// 跳转页面到员工列表页 重定向
resp.sendRedirect("emplist");
}
}
最后修改EmpMapper.xml
insert into t_emp(name,age,sex,salary,dept_id) value (#{name},#{age},#{sex},#{salary},#{deptId})
源码:https://download.csdn.net/download/aasszzxxddff/12387118