OA项目之Mybatis多表链接查询
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多表链接查询的更多相关文章
- OA项目之mybatis动态查询
类似于三个条件,可以全部选择,也可以选择几个条件进行查询 Mapper.xml文件: <resultMap type="Employee" id="selAll&q ...
- Mybatis多表链接查询重复字段问题
A表和B表一对多的关系 A表 B表 A表和C表也是一对多关系 C表 我现在向查询出A表的所有字段和B表的name字段,C表的name字段 这是我错误的sql语句,可以看出我没有查B表和C表的id字段, ...
- oa项目面试准备
熟悉项目在ssm框架下的编程流程,了解mysql html spring springmvc mybatis技术.了解过springboot编程. 在上个寒假跟着培训机构用springboot框架编写 ...
- 【Java EE 学习 67 上】【OA项目练习】【JBPM工作流的使用】
OA项目中有极大可能性使用到JBPM框架解决流程控制问题,比如请假流程.报销流程等等. JBPM:JBoss Business Process Management,翻译过来就是业务流程管理.实际上就 ...
- Mybatis使用MySQL模糊查询时输入中文检索不到结果怎么办--转自http://www.jb51.net/article/88236.htm
这篇文章主要介绍了Mybatis使用MySQL模糊查询时输入中文检索不到结果的解决办法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下 项目开发中,在做Mybatis动态查询时,遇到了 ...
- mybatis深入之动态查询和连接池介绍
mybatis深入之动态查询和连接池介绍 一.mybatis条件查询 在mybatis前述案例中,我们的查询条件都是确定的.但在实际使用的时候,我们的查询条件有可能是动态变化的.例如,查询参数为一个u ...
- MyBatis工程搭建&MyBatis实现Mapper配置查询
一.MyMyBatis工程搭建 新建Maven项目:mybatis-demo 准备数据源 1 # 删除mybatis_demo数据库 2 drop database if exists mybatis ...
- MyBatis的多表查询笔记
MyBatis的多表查询 随着学习的进步,需求的提高,我们在实际开发中用的最多的还是多表查询,就让我们一起学习MyBatis中的多表查询. 数据库准备 Class表 Student表 项目结构 这次使 ...
- MyBatis实现关联表查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
随机推荐
- VLAN的 基本用法与配置
需求:在一家小型企业中,所有员工都使用一台交换机,老板为了避免员工私下通信,将他们分配了不同网段,但偶尔还是会发现,有些员工会自行修改网段和别人通信.如果你是这家企业的网络工程师,你该如何处理? 1. ...
- jsp+servlet分页查询
分页查询 减少服务器内存开销 提高用户体验 效果图 思绪图 分页显示Bean文件代码 package cn.ytmj.findlist.domain; import java.util.List; / ...
- java里的一些名词的意思
JDK java开发工具包(java development kit) JRE java运行环境 (java runtime environment)
- suseoj 1209: 独立任务最优调度问题(动态规划)
1209: 独立任务最优调度问题 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 用2台处理机A和B处理 ...
- 在 Windows 上 安装 Oracle 11g Xe
去oracle官网下载 https://www.oracle.com/database/technologies/xe-prior-releases.html 点击下载: Oracle Databas ...
- C语言入门教程: 一个简单的实例
对于学习要保持敬畏! 语言不只是一种工具,还是一种资源,因此,善待它,掌握它! 我们知道,对于未知通常都会充满好奇和畏惧,既想了解它,又害怕神秘面纱隐藏的不确定性.对于一门编程语言同样如此,我将以 ...
- Python 操作Gitlab-API 实现批量的合并分支
1.需求:每次大批量上线完成后,都会进行将hotfix合并到Master,合并到test/uat等等重复操作(上线发布后自动合并master已完成). 2.现实:在完成发布后自动合并master后,可 ...
- python 实现图片批量加入水印!pillow 入门实战!
写文章的时候可以设置是否添加水印.可是,有些图片可能想加水印,有些不想加水印,该怎么办呢? 配置环境 python3 + pillow pip3 install pillow 引入库 from PIL ...
- JSP内置对象详解及示例
目录 JSP 内置对象 out request response config application session pageContext page exception JSP 内置对象 jsp一 ...
- day 17 re模块 正则表达式
import re 引用re模块 查找 finall:匹配所有,每一项都是列表中的一个元素 search:只匹配从左到右的第一个,得到的不是直接的结果而是一个变量,通过group方法获取结果,没 ...