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>

原文地址:https://www.cnblogs.com/fbbg/p/11719781.html

时间: 2024-07-31 14:31:36

Mybatis入门二----关联映射的相关文章

mybatis入门(二)

mybatis入门(二) 探究sql语句的映射过程 要探究sql执行过程,先看一个简单的小例子 <?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"> <map

mybatis入门二-----增删改查

一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapp

mybatis 入门二

1.新建一个java项目 2.加入mybatis.jar和mysql.jar 3.加mybatis的配置文件 mybatis.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-con

mybatis入门(二):增删改查

mybatis的原理: 1.mybatis是一个持久层框架,是apache下的顶级项目 mybatis托管到googlecode下,目前托管到了github下面 2.mybatis可以将向prepareStatement中输入的参数自动进行输入映射,将查询结果集灵活的映射成java对象.(输出映射) mybatis的一般使用到的maven包: <dependency> <groupId>mysql</groupId> <artifactId>mysql-co

MyBatis 入门(二)--用接口方式访问数据库

一.建立接口 UserMapper.java public interface UserMapper { public List<User> getAllUser(); public User getUserById(String userId); public int insert(User user); public int update(User user); public int delete(User user); } 二 修改 UserMapper.xml <?xml ver

MyBatis入门(二)---一对一,一对多

一.创建数据库表 1.1.创建数据表同时插入数据 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.6.27-log : Database - mybatis ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @[emai

MyBatis入门(五)---延时加载、缓存

一.创建数据库 1.1.建立数据库 /* SQLyog Enterprise v12.09 (64 bit) MySQL - 5.7.9-log : Database - mybatis ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=''*/; /*!40014 SET @[email protec

MyBatis 入门到精通(二) SQL语句映射XML文件

MyBatis 真正强大之处就在这些映射语句,也就是它的魔力所在.对于它的强大功能,SQL 映射文件的配置却非常简单. 如果您比较SQL 映射文件配置与JDBC 代码,您很快可以发现,使用SQL 映射文件配置可以节省95%的代码量.MyBatis 被创建来专注于SQL,但又给您自己的实现极大的空间. 需要配置的基本元素 1. cache – 配置给定模式的缓存 2. cache-ref – 从别的模式中引用一个缓存 3. resultMap – 这是最复杂而却强大的一个元素了,它描述如何从结果集

Hibernate的七种映射关系之七种关联映射(二)

继续上篇博客 七.Hibernate双向一对多关联映射:让多的一端来维护关系. 主要是解决一对多单向关联的缺陷,而不是需求驱动的. 1.在Student.java实体类里添加Classes引用.private Classes classes; 2.Student.hbm.xml里添加many-to-one标签:<many-to-one name="classes" column="classesid"/>.Classes.hbm.xml在例子(六)里的那