mybatis中联合查询

Posted noob-mengling

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis中联合查询相关的知识,希望对你有一定的参考价值。

创建两个类一个employee,一个department

技术分享图片
package com.cn.orm;

public class Department {
    private Integer deptId;

    private String deptName;

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName == null ? null : deptName.trim();
    }

    @Override
    public String toString() {
        return "Department{" +
                "deptId=" + deptId +
                ", deptName=‘" + deptName + ‘‘‘ +
                ‘}‘;
    }
}
View Code
技术分享图片
package com.cn.orm;

public class Employee {
    private Integer id;

    private String name;

    private Integer age;

    private String sex;

    private Department department;
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", name=‘" + name + ‘‘‘ +
                ", age=" + age +
                ", sex=‘" + sex + ‘‘‘ +
                ", department=" + department +
                ‘}‘;
    }
}
View Code

在employeeMapper.xml文件中配置

技术分享图片
<?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.cn.dao.EmployeeMapper">
  <resultMap id="BaseResultMap" type="com.cn.orm.Employee">
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="age" jdbcType="INTEGER" property="age" />
    <result column="sex" jdbcType="CHAR" property="sex" />
    <association property="department" javaType="com.cn.orm.Department">
      <result column="dept_id" javaType="INTEGER" property="deptId"></result>
      <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
    </association>
  </resultMap>

  <select id="fetchEmployeesList" resultMap="BaseResultMap">
      select e.id,e.name,e.age,e.sex,t.id as dept_id,t.name as dept_name from employee e left join department t on e.dept_id = t.id
  </select>

</mapper>
View Code

说明两个类中具有同名属性id,name。若不指定别名,则会将属性名相同的赋值成相同的内容,无法达到联合查询的效果。

column为sql查询之后列的名称,property为java中属性字段名称

以上是关于mybatis中联合查询的主要内容,如果未能解决你的问题,请参考以下文章

sql注入中联合查询group_concat位置

从 MS-Access 中联合选择查询中的 SQLite 语法错误

在 SQL Server 中联合所有的替代方案

创建 XML 文件时在 SQL 中联合

如何在mysql中联合后获取最小值?

Ariadne 中联合类型的解析器函数