Mybatis入门二----关联映射
Posted 橙汁one
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis入门二----关联映射相关的知识,希望对你有一定的参考价值。
一、 数据库表中的数据和实体之间的映射默认是通过列名和属性名一致实现的。
如果数据库表中的列名和实体类中的属性名不一致,
(1)可以通过来给列名设定别名来实现映射:
<select id="empall1" resultType="Emp">
select deptno deptno1,dname dname1 from emp
</select>
(2)可以通过在映射文件中设置resultMap来设置
<resultMap id="dept1" type="dept">
<!--id的作用为让别的命令调用,type为结果类型-->
<id column="deptno" property="deptno1"/> <!--此标签为主键列,column为列名,property为属性名-->
<result column="dname" property="dname1"/> <!--此标签为普通列-->
</resultMap>
sql语句:
<select id="empall1" resultMap="dept1">
select dname from dept
</select>
二、关联映射(多表查询)多对一,一对多查询设置
员工和部门的实体:
员工表(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 + ‘}‘; } }
部门表(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 + ‘‘‘ + ‘}‘; } }
本例中以员工和部门的关系为例
1、多对一(以员工表为主表)
(1)首先在多的一方的实体中引入一的一方,构建一的一方的get和set方法,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 + ‘}‘; } }
(2)在映射文件中配置resultMap,来进行map表和dept表的联系
<resultMap id="map1" type="Emp" autoMapping="true">
<!--autoMapping:自动映射,实体中的属性名和数据表中列名的映射,如果属性名和列名是一样的就用自动映射,如果不一样就要使用本文上边的设置-->
<association property="dept" column="deptno" javaType="Dept" autpMapping="true">
<!--column:两表之间的关联列-->
</assoction>
</resultMap>
sql语句:
<select id="empall" resultMap="test1">
select e.*,d.dname from emp e join dept d on e.deptno = d.deptno
</select>
2、一对多(以部门表为主表)
(1)首先在一的一方引入多的一方,构建set、get、toString方法,
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 + ‘}‘; } }
(2)在映射文件中设置resultMap,进行emp和dept的联系
<resultMap id="dept1" type="dept" autpMapping="true">
<!--在一对多的情况下一定要在resultMap中写id标签-->
<id property="deptno" column="deptno"></id>
<!--property的值为在的一的一方实体中引入的多的一方的实体变量名
column:两表之间的关联列
ofType:多的一方的实体类的类名
-->
<collection property="emps" column="deptno" ofType="Emp" autoMapping="true">
</collection>
</resultMap>
sql语句:
<select id="quaryAll" resultMap="dept1">
select e.*,d.dname from dept d join e emp on d.deptno=e.deptno
</select>
以上是关于Mybatis入门二----关联映射的主要内容,如果未能解决你的问题,请参考以下文章