mybatis 一对一 查询所有员工,及其档案详情

package com.entity;


import java.util.Date;


public class Emp {



private long id;
private String  name;
private String job;
private  long  mgr;
private String  hiredate;
private  double sal;
private double comm;
private int deptno;
private Doc doc;


public Doc getDoc() {
return doc;
}
public void setDoc(Doc doc) {
this.doc = doc;
}
public Emp(long id, String name, String job, long mgr, String hiredate,
double sal, double comm, int deptno) {
super();
this.id = id;
this.name = name;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public Emp() {
super();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public long getMgr() {
return mgr;
}
public void setMgr(long mgr) {
this.mgr = mgr;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public double getComm() {
return comm;
}
public void setComm(double comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", job=" + job + ", mgr="
+ mgr + ", hiredate=" + hiredate + ", sal=" + sal + ", comm="
+ comm + ", deptno=" + deptno ;
}








}

------------------------------------------

package com.entity;


public class Doc {
/* docid  NUMBER(10) not null,
  sn     VARCHAR2(20),
  jiguan VARCHAR2(20),
  card   VARCHAR2(20),
  phone  VARCHAR2(20),
  email  VARCHAR2(20),
  eid    NUMBER(4)*/
private long  docid;
private String sn;
private String jiguan;
private String card;
private String phone;
private String email;
private String eid;
private   Emp  emp;
public Doc(long docid, String sn, String jiguan, String card, String phone,
String email, String eid, Emp emp) {
super();
this.docid = docid;
this.sn = sn;
this.jiguan = jiguan;
this.card = card;
this.phone = phone;
this.email = email;
this.eid = eid;
this.emp = emp;
}
public Doc() {
super();
}
public long getDocid() {
return docid;
}
public void setDocid(long docid) {
this.docid = docid;
}
public String getSn() {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getJiguan() {
return jiguan;
}
public void setJiguan(String jiguan) {
this.jiguan = jiguan;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getEid() {
return eid;
}
public void setEid(String eid) {
this.eid = eid;
}
public Emp getEmp() {
return emp;
}
public void setEmp(Emp emp) {
this.emp = emp;
}
@Override
public String toString() {
return "Doc [docid=" + docid + ", sn=" + sn + ", jiguan=" + jiguan
+ ", card=" + card + ", phone=" + phone + ", email=" + email
+ ", eid=" + eid + ", emp=" + emp + "]";
}








}

--------------------------------------------------------------

package com.dao;


import java.util.List;


import com.entity.Emp;


//用mybatis做一个员工的查询一个员工,查询所有员工,
public interface IEmpDao {
public List<Emp> getAll();
}

----------------------------------------

<?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.dao.IEmpDao">


    <resultMap type="Emp" id="empmap">
        <id property="id" column="empno" />
        <result property="name" column="ename" />  
        <result property="job" column="job" />  
        <result property="mgr" column="mgr"/>  
        <result property="hiredate" column="hiredate" />  
        <result property="sal" column="sal" />  
        <result property="comm" column="comm" />  
        <result property="deptno" column="deptno" />  
      <association property="doc" resultMap="docmap"/>
   </resultMap>


<resultMap type="Doc" id="docmap">
        <id property="docid" column="docid" />
        <result property="sn" column="sn" />  
        <result property="jiguan" column="jiguan" />  
        <result property="card" column="card"/>  
        <result property="phone" column="phone" />  
        <result property="email" column="email" /> 
        <result property="eid" column="eid" /> 
        <association property="emp" resultMap="empmap"/>
</resultMap> 
<select id="getAll" resultMap="empmap">
select e.*,d.*
from emp e,doc d
where e.empno=d.eid
</seleect>

</mapper>

-----------------------------

public class EmpDaoImpl implements IEmpDao {
@Override
public List<Emp> getAll(){
IEmpDao  dao=MybatisUtil.getSqlSessetion1().getMapper(IEmpDao.class);
List<Emp>  emps=dao.getAll();//
MybatisUtil.getSqlSessetion1().close();
return emps;

}

}

8888888888888888888888888888888

测试

public class Test {
public static void main(String[] args) {
EmpDaoImpl dao = new EmpDaoImpl();
List<Emp> emps=dao.getAll();
for (Emp emp : emps) 
{
System.out.println(emp+"--"+emp.getDoc().getCard());

}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值