Two ---- Mybatis association mapping entry

   A mapping between a default database table and the data entity is achieved by the same column and attribute names.

         If the attribute name column names in the database table and the entity class is inconsistent,

               (1) can be achieved by mapping setting alias to the column name:

              <select id="empall1" resultType="Emp">
                          select deptno deptno1,dname dname1 from emp
              </select>

               (2) can be set by setting the mapping file resultMap

                 <resultMap id="dept1"  type="dept">

                       <-! Id role in order to make other calls, type is the result of type ->

                       <ID = column "DEPTNO" Property = "deptno1" />     <-! This label primary key column, column name of a column, property attribute name ->

                       <Result column = "DNAME" Property = "dname1" />      <-! This column label ordinary ->

                 </resultMap>

                  sql statement:

                    <select   id="empall1"   resultMap="dept1">

                             select dname from dept

                    </select>

     Second, the association mapping (multi-table queries) many-to-many query settings

         Employee and department entities:

         Employees table (emp):

public class Emp {
private Integer empno;
private String ename;
private String job;
private Date hiredate;
private Double sal;
private Integer deptno;

public Emp() {
}

public Emp(Integer empno, String job, Double sal) {
this.empno = empno;
this.job = job;
this.sal = sal;
}

public Emp(String ename, String job, Date hiredate, Double sal, Integer deptno) {
this.ename = ename;
this.job = job;
this.hiredate = hiredate;
this.sal = sal;
this.deptno = deptno;
}

public Emp(Integer empno, String ename, String job, Date hiredate, Double sal) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.hiredate = hiredate;
this.sal = sal;
}

public Emp(Integer empno, String ename, String job, Date hiredate, Double sal, Integer deptno, Dept dept) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.hiredate = hiredate;
this.sal = sal;
this.deptno = deptno;
this.dept = dept;
}

public Integer getEmpno() {
return empno;
}

public void setEmpno(Integer empno) {
this.empno = empno;
}

public String getEname() {
return ename;
}

public void setEname(String ename) {
this.ename = ename;
}

public String getJob() {
return job;
}

public void setJob(String job) {
this.job = job;
}

public Date getHiredate() {
return hiredate;
}

public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}

public Double getSal() {
return sal;
}

public void setSal(Double sal) {
this.sal = sal;
}

public Integer getDeptno() {
return deptno;
}

public void setDeptno(Integer deptno) {
this.deptno = deptno;
}

@Override
public String toString() {
return "Emp{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", hirbate=" + hiredate +
", sal=" + sal +
", deptno=" + deptno +
'}';
}
}
View Code

 

        Department table (dept):

public class Dept {
    private Integer deptno;
    private String dname;

    public Dept() {
    }

    public Dept(Integer deptno, String dname) {
        this.deptno = deptno;
        this.dname = dname;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                '}';
    }
}
View Code

 

          In this case to the relationship between employees and departments, for example

          1, many to one (mainly employee-table table)

               (1) introducing a first one of the plurality of one entity, one of a construct of get and set methods, method toString

package com.aaa.entity;

import java.util.Date;

public class Emp {
    private Integer empno;
    private String ename;
    private String job;
    private Date hiredate;
    private Double sal;
    private Integer deptno;
    private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public Emp() {
    }

    public Emp(Integer empno, String job, Double sal) {
        this.empno = empno;
        this.job = job;
        this.sal = sal;
    }

    public Emp(String ename, String job, Date hiredate, Double sal, Integer deptno) {
        this.ename = ename;
        this.job = job;
        this.hiredate = hiredate;
        this.sal = sal;
        this.deptno = deptno;
    }

    public Emp(Integer empno, String ename, String job, Date hiredate, Double sal) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.hiredate = hiredate;
        this.sal = sal;
    }

    public Emp(Integer empno, String ename, String job, Date hiredate, Double sal, Integer deptno, Dept dept) {
        this.empno = empno;
        this.ename = ename;
        this.job = job;
        this.hiredate = hiredate;
        this.sal = sal;
        this.deptno = deptno;
        this.dept = dept;
    }

    public Integer getEmpno() {
        return empno;
    }

    public void setEmpno(Integer empno) {
        this.empno = empno;
    }

    public String getEname() {
        return ename;
    }

    public void setEname(String ename) {
        this.ename = ename;
    }

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }

    public Date getHiredate() {
        return hiredate;
    }

    public void setHiredate(Date hiredate) {
        this.hiredate = hiredate;
    }

    public Double getSal() {
        return sal;
    }

    public void setSal(Double sal) {
        this.sal = sal;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    @Override
    public String toString() {
        return "Emp{" +
                "empno=" + empno +
                ", ename='" + ename + '\'' +
                ", job='" + job + '\'' +
                ", hirbate=" + hiredate +
                ", sal=" + sal +
                ", deptno=" + deptno +
                ", dept=" + dept +
                '}';
    }
}
View Code

              (2) disposed in the mapping file resultMap to contact map table and the table dept

                  <resultMap id="map1" type="Emp" autoMapping="true">

                          <-! AutoMapping: automatic mapping, the mapping entity name attribute names and data listed in the table, if the attribute name and column names are automatically mapped to use the same, if not the same as the upper side is provided herein should use ->

                          <association property="dept" column="deptno" javaType="Dept" autpMapping="true">

                               <- column:! Association between the two column table ->

                          </assoction>

                 </resultMap>

                sql statement:

                 <select id="empall" resultMap="test1">
                      select e.*,d.dname from emp e join dept d on e.deptno = d.deptno
                </select>

          2, many (sectoral table the primary table)

               (1) First, the introduction of more than one party in a building set, get, toString method,

package com.aaa.entity;

import java.util.List;

public class Dept {
    private Integer deptno;
    private String dname;
      //多的一方数据多,用list
    private List<Emp> emps;

    public List<Emp> getEmps() {
        return emps;
    }

    public void setEmps(List<Emp> emps) {
        this.emps = emps;
    }

    public Dept() {
    }

    public Dept(Integer deptno, String dname) {
        this.deptno = deptno;
        this.dname = dname;
    }

    public Integer getDeptno() {
        return deptno;
    }

    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }

    public String getDname() {
        return dname;
    }

    public void setDname(String dname) {
        this.dname = dname;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "deptno=" + deptno +
                ", dname='" + dname + '\'' +
                ", emps=" + emps +
                '}';
    }
}
View Code

               (2) Set resultMap in the mapping file, contact the dept and emp

                   <resultMap id="dept1"  type="dept"  autpMapping="true">

                            <-! Be sure to write id tags resultMap under many circumstances ->

                            <id property="deptno"  column="deptno"></id>

                             <! - name of the entity variable number of property values ​​in one of the one introduced entity one of

                                   column: association between the two column table

                                   The class name of the entity class multi-party: ofType

                               -->

                            <collection property="emps" column="deptno" ofType="Emp" autoMapping="true">

                             </collection>

                     </resultMap>

                      sql statement:

                      <select id="quaryAll"  resultMap="dept1">

                          select e.*,d.dname from dept d join e emp on d.deptno=e.deptno

                      </select>

 

 

 

 

 

             

 

Guess you like

Origin www.cnblogs.com/fbbg/p/11719781.html