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

来源:互联网 发布:飞鹰直销软件 编辑:程序博客网 时间:2024/06/05 05:07
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());

}