xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.EmployeeMapper">
<select id="selbyemptype" parameterType="int" resultType="Employee">
select * from employee where emptype=#{empType}
</select> <resultMap type="Employee" id="selAll">
<id property="empId" column="empid"/>
<result property="realName" column="realname"/>
<result property="sex" column="sex"/>
<result property="hireDate" column="hiredate"/>
<result property="phone" column="phone"/> <collection property="mgr" ofType="Employee">
<id property="empId" column="mgrId"/>
<result property="realName" column="realname"/>
</collection> <collection property="position" ofType="position">
<id property="posid" column="posid"/>
<result property="pname" column="pname"/>
</collection> <collection property="dept" ofType="Department">
<id property="deptno" column="deptno"/>
<result property="deptname" column="deptname"/>
</collection> </resultMap>
<select id="selAll" resultMap="selAll">
select e.empid,e.realname,e.sex,e.hiredate,e.phone,d.deptname,p.pname,e2.realname
from employee e left join dept d on e.deptno=d.deptno
left join position p on e.posid=p.posid
left join employee e2 on e.mgrid=e2.empid
order by e.empid
</select>
</mapper>

实体类:

package com.bjsxt.entity;

import java.util.ArrayList;
import java.util.Date;
import java.util.List; /**
* 员工类
*
* 如何表示员工所属一个部门、一个岗位、一个上级、甚至多个下级的信息呢?
* 在数据库中通过外键来实现:deptno、posid,mgrid
* 在Java类中通过属性关联来实现
*
* private Department dept; //员工所属部门 不仅包含部门的编号,还包含其他信息
private Position position;
private Employee mgr;//上级领导的信息
private List<Employee> empList = new ArrayList<Employee>();//下级的信息,可能多个
* @author Administrator
*
*/
public class Employee {
private String empId;//员工编号
private String password;//密码
private String realName;//真实姓名
private String sex;//性别
private Date birthDate;//出生日期
private Date hireDate;//入职日期
private Date leaveDate;//离职日期
private int onDuty;//是否在职 0-离职 1-在职
private int empType;//员工类型 1.普通员工 2.管理人员 含经理、总监、总裁等 3.管理员
private String phone;//联系方式
private String qq;
private String emerContactPerson;//紧急联系人信息
private String idCard;//身份证号码
/*
private int deptno;
private int posId;
private String mgrId;
*/
private Department dept; //员工所属部门 不仅包含部门的编号,还包含其他信息
private Position position;
private Employee mgr;//上级领导的信息
private List<Employee> empList = new ArrayList<Employee>();//下级的信息,可能多个 public Employee() {
super();
} public Employee(String empId, String password, String realName, String sex,
Date birthDate, Date hireDate, Date leaveDate, int onDuty,
int empType, String phone, String qq, String emerContactPerson,
String idCard, Department dept, Position position, Employee mgr) {
super();
this.empId = empId;
this.password = password;
this.realName = realName;
this.sex = sex;
this.birthDate = birthDate;
this.hireDate = hireDate;
this.leaveDate = leaveDate;
this.onDuty = onDuty;
this.empType = empType;
this.phone = phone;
this.qq = qq;
this.emerContactPerson = emerContactPerson;
this.idCard = idCard;
this.dept = dept;
this.position = position;
this.mgr = mgr;
} public Employee(String empId, String password, String realName, String sex,
Date birthDate, Date hireDate, Date leaveDate, int onDuty,
int empType, String phone, String qq, String emerContactPerson,
String idCard, Department dept, Position position, Employee mgr,
List<Employee> empList) {
super();
this.empId = empId;
this.password = password;
this.realName = realName;
this.sex = sex;
this.birthDate = birthDate;
this.hireDate = hireDate;
this.leaveDate = leaveDate;
this.onDuty = onDuty;
this.empType = empType;
this.phone = phone;
this.qq = qq;
this.emerContactPerson = emerContactPerson;
this.idCard = idCard;
this.dept = dept;
this.position = position;
this.mgr = mgr;
this.empList = empList;
}
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthDate() {
return birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public Date getHireDate() {
return hireDate;
}
public void setHireDate(Date hireDate) {
this.hireDate = hireDate;
}
public Date getLeaveDate() {
return leaveDate;
}
public void setLeaveDate(Date leaveDate) {
this.leaveDate = leaveDate;
}
public int getOnDuty() {
return onDuty;
}
public void setOnDuty(int onDuty) {
this.onDuty = onDuty;
}
public int getEmpType() {
return empType;
}
public void setEmpType(int empType) {
this.empType = empType;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmerContactPerson() {
return emerContactPerson;
}
public void setEmerContactPerson(String emerContactPerson) {
this.emerContactPerson = emerContactPerson;
}
public String getIdCard() {
return idCard;
}
public void setIdCard(String idCard) {
this.idCard = idCard;
}
public Department getDept() {
return dept;
}
public void setDept(Department dept) {
this.dept = dept;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
public Employee getMgr() {
return mgr;
}
public void setMgr(Employee mgr) {
this.mgr = mgr;
}
public List<Employee> getEmpList() {
return empList;
}
public void setEmpList(List<Employee> empList) {
this.empList = empList;
} @Override
public String toString() {
return "Employee [empId=" + empId + ", password=" + password
+ ", realName=" + realName + ", sex=" + sex + ", birthDate="
+ birthDate + ", hireDate=" + hireDate + ", leaveDate="
+ leaveDate + ", onDuty=" + onDuty + ", empType=" + empType
+ ", phone=" + phone + ", qq=" + qq + ", emerContactPerson="
+ emerContactPerson + ", idCard=" + idCard + ", dept=" + dept
+ ", position=" + position + ", mgr=" + mgr + ", empList="
+ empList + "]";
} }

Mapper接口:

package com.bjsxt.mapper;

import java.util.List;

import com.bjsxt.entity.Employee;

public interface EmployeeMapper {

	List<Employee> selbyemptype(int empType);

	List<Employee> selAll();
}

service方法:

package com.bjsxt.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.bjsxt.dao.EmployeeDao;
import com.bjsxt.dao.impl.EmployeeDaoImpl;
import com.bjsxt.entity.Employee;
import com.bjsxt.mapper.EmployeeMapper;
import com.bjsxt.service.EmployeeService;
import com.bjsxt.util.MyBatisUtil; public class EmployeeServiceImpl implements EmployeeService{ private EmployeeDao empDao = new EmployeeDaoImpl();
SqlSession session = MyBatisUtil.getSession();
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
@Override
public int add(Employee emp) {
return this.empDao.save(emp);
} @Override
public List<Employee> findEmpByType(int i) {
List<Employee> selbyemptype = mapper.selbyemptype(i);
session.close();
return selbyemptype;
} @Override
public List<Employee> selAll() {
List<Employee> selAll = mapper.selAll();
session.close();
return selAll;
} }

service接口:

package com.bjsxt.service;

import java.util.List;

import com.bjsxt.entity.Employee;

public interface EmployeeService {
/**
* 添加员工
* @param emp
* @return
*/
public int add(Employee emp);
/**
* 查询指定类型的员工
* @param i
* @return
*/
public List<Employee> findEmpByType(int i); List<Employee> selAll(); }

Servlet实现类:

package com.bjsxt.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import com.bjsxt.entity.Department;
import com.bjsxt.entity.Employee;
import com.bjsxt.entity.Position;
import com.bjsxt.service.DepartmentService;
import com.bjsxt.service.EmployeeService;
import com.bjsxt.service.PositionService;
import com.bjsxt.service.impl.DepartmentServiceImpl;
import com.bjsxt.service.impl.EmployeeServiceImpl;
import com.bjsxt.service.impl.PositionServiceImol; public class EmployeeServlet extends BaseServlet { public void selinfo(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String empid = request.getParameter("empId");
String deptno = request.getParameter("deptno");
System.out.println("=============="+deptno);
String shireDate = request.getParameter("hireDate");
String onduty = request.getParameter("onDuty");
DateFormat df=new SimpleDateFormat("yyyy-MM-dd");
Date hiredate =null;
try {
hiredate = df.parse(shireDate);
} catch (ParseException e) {
e.printStackTrace();
}
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> deptList = ds.seAll();
request.setAttribute("deptList", deptList);
EmployeeService es = new EmployeeServiceImpl();
List<Employee> selAll = es.selAll();
request.setAttribute("empid", empid);
request.setAttribute("deptno", deptno);
request.setAttribute("onduty", onduty);
request.setAttribute("hireDate", shireDate);
request.setAttribute("selAll", selAll);
request.getRequestDispatcher("/system/empList.jsp").forward(request, response); } public void selAll(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> deptList = ds.seAll();
request.setAttribute("deptList", deptList); EmployeeService es = new EmployeeServiceImpl();
List<Employee> selAll = es.selAll();
request.setAttribute("selAll", selAll);
request.getRequestDispatcher("/system/empList.jsp").forward(request, response);
} public void findsth(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取所有的部门信息
DepartmentService ds=new DepartmentServiceImpl();
List<Department> seAll = ds.seAll();
request.setAttribute("seAll", seAll);
//获取所有的岗位信息
PositionService ps=new PositionServiceImol();
List<Position> selpos = ps.selpos();
request.setAttribute("selpos", selpos);
//获取上级员工
EmployeeService empService = new EmployeeServiceImpl();
List<Employee> mgrList = empService.findEmpByType(2);//1 基层员工 2 各级管理人员
request.setAttribute("mgrList",mgrList);
//跳转到system/empAdd.jsp
request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
} public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取员工的信息
String empId = request.getParameter("empId");
String password ="123456";
String realName = request.getParameter("realName");
String sex = request.getParameter("sex");
//日期类型的处理
String sbirthDate = request.getParameter("birthDate");
String shireDate = request.getParameter("hireDate");
String sleaveDate = request.getParameter("leaveDate"); Date birthDate= null,hireDate = null,leaveDate = null;
DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
birthDate = sdf.parse(sbirthDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
hireDate = sdf.parse(shireDate);
} catch (ParseException e) {
e.printStackTrace();
}
try {
leaveDate = sdf.parse(sleaveDate);
} catch (ParseException e) {
System.out.println("暂时没有离职日期~");
}
//整数的处理
int onDuty = Integer.parseInt(request.getParameter("onDuty"));
int empType = Integer.parseInt(request.getParameter("empType")); String phone = request.getParameter("phone");
String qq = request.getParameter("qq");
String emerContactPerson = request.getParameter("emerContactPerson");
String idCard = request.getParameter("idCard"); //对象的处理
int deptno = Integer.parseInt(request.getParameter("deptno"));
Department dept = new Department();
dept.setDeptno(deptno); int posid = Integer.parseInt(request.getParameter("posid"));
Position position=new Position();
position.setPosid(posid); String mgrId = request.getParameter("mgrId");
Employee mgr = new Employee();
mgr.setEmpId(mgrId);//!!! //调用业务层完成添加操作
Employee emp = new Employee(empId, password, realName, sex, birthDate, hireDate, leaveDate, onDuty, empType, phone, qq, emerContactPerson, idCard, dept, position, mgr);
EmployeeService empService = new EmployeeServiceImpl();
int n = empService.add(emp);
//根据结果进行页面跳转
if(n>0){
response.sendRedirect(request.getContextPath()+"/system/empList.jsp");
}
else{
request.setAttribute("error", "添加员工失败");
request.getRequestDispatcher("/system/empAdd.jsp").forward(request, response);
} } }

实现效果:

OA项目之Mybatis多表链接查询的更多相关文章

  1. OA项目之mybatis动态查询

    类似于三个条件,可以全部选择,也可以选择几个条件进行查询 Mapper.xml文件: <resultMap type="Employee" id="selAll&q ...

  2. Mybatis多表链接查询重复字段问题

    A表和B表一对多的关系 A表 B表 A表和C表也是一对多关系 C表 我现在向查询出A表的所有字段和B表的name字段,C表的name字段 这是我错误的sql语句,可以看出我没有查B表和C表的id字段, ...

  3. oa项目面试准备

    熟悉项目在ssm框架下的编程流程,了解mysql html spring springmvc mybatis技术.了解过springboot编程. 在上个寒假跟着培训机构用springboot框架编写 ...

  4. 【Java EE 学习 67 上】【OA项目练习】【JBPM工作流的使用】

    OA项目中有极大可能性使用到JBPM框架解决流程控制问题,比如请假流程.报销流程等等. JBPM:JBoss Business Process Management,翻译过来就是业务流程管理.实际上就 ...

  5. Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm

    这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下   项目开发中,在做Mybatis动态查询时,遇到了 ...

  6. mybatis深入之动态查询和连接池介绍

    mybatis深入之动态查询和连接池介绍 一.mybatis条件查询 在mybatis前述案例中,我们的查询条件都是确定的.但在实际使用的时候,我们的查询条件有可能是动态变化的.例如,查询参数为一个u ...

  7. MyBatis工程搭建&MyBatis实现Mapper配置查询

    一.MyMyBatis工程搭建 新建Maven项目:mybatis-demo 准备数据源 1 # 删除mybatis_demo数据库 2 drop database if exists mybatis ...

  8. MyBatis的多表查询笔记

    MyBatis的多表查询 随着学习的进步,需求的提高,我们在实际开发中用的最多的还是多表查询,就让我们一起学习MyBatis中的多表查询. 数据库准备 Class表 Student表 项目结构 这次使 ...

  9. MyBatis实现关联表查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

随机推荐

  1. php imagick设置图片圆角的方法

    php imagick设置图片圆角的方法 <pre>header('Content-Type: image/png'); $image = new Imagick('http://stat ...

  2. JSON数据与Java对象的相互转换

    JSON数据与Java对象的相互转换 JSON解析器 常见的解析器:Jsonlib .Gson. fastjson. jackson JSON转化为Java对象 使用步骤: 1.导入jackson的相 ...

  3. [RAM] FPGA的学习笔记——RAM

    1.RAM——随机存取存储器, 分为SRAM和DRAM. SRAM:存和取得速度快,操作简单.然而,成本高,很难做到很大.FPGA的片内存储器,就是一种SRAM,用来存放程序,以及程序执行过程中,产生 ...

  4. 第一篇:jdk8下载和idea导入,java源码结构

    一.下载和导入 下载和导入到idea,完全参考文章:https://blog.csdn.net/zhanglong_4444/article/details/88967300 照做即可,详解简单到位. ...

  5. 使用POI导出EXCEL工具类并解决导出数据量大的问题

    POI导出工具类 工作中常常会遇到一些图表需要导出的功能,在这里自己写了一个工具类方便以后使用(使用POI实现). 项目依赖 <dependency> <groupId>org ...

  6. cocos creator 3D | 拇指射箭

    拇指射箭!你能射中靶心么? 效果预览 配置环境: cocos creator 3d v1.0.0 玩法介绍: 长按屏幕,拖动瞄准,放手发射.风向.重力和距离影响最终结果!越靠近中心得分越高!最高分10 ...

  7. 领扣(LeetCode)对称二叉树 个人题解

    给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null,3,nul ...

  8. Apache服务安装及一些基本操作

    注意:安装apache服务之前记得搭建yum仓库 1.安装apache服务,输入命令“yum install httpd” 安装成功后,会这样显示 2.需要对Apache服务进行启动,输入命令“sys ...

  9. python读取 ini 配置文件

    在详解python读取ini文件之前,我们先说明一个ini文件的组成: 一个ini文件是由多个section组成,每个section中以key=vlaue形式存储数据: 然后我们来使用python读取 ...

  10. Python3 之 with语句(高效、便捷)

    在实际的编码过程中,有时有一些任务,需要事先做一些设置,事后做一些清理,这时就需要python3 with出场了,with能够对这样的需求进行一个比较优雅的处理,最常用的例子就是对访问文件的处理. 文 ...