springboot 之mybatis 多表关联查询

数据库表

一对多查询

自定义实体类:

package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * (Dept)实体类
 *
 * @author makejava
 * @since 2020-09-02 08:40:43
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept implements Serializable {
    private static final long serialVersionUID = 735011413198873745L;

    private Integer deptno;

    private String dname;

    private String loc;

    private List<Emp> emps;

}

dao层:

package com.example.dao;

import com.example.entity.Dept;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * (Dept)表数据库访问层
 *
 * @author makejava
 * @since 2020-09-02 08:40:46
 */
public interface DeptDao {

    /**
     * 通过ID查询单条数据
     *
     * @param deptno 主键
     * @return 实例对象
     */
    Dept queryById(Integer deptno);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    List<Dept> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 无条件查询
     *
     * @return 对象列表
     */

    List<Dept> getAll();

    /**
     * 通过实体作为筛选条件查询
     *
     * @param dept 实例对象
     * @return 对象列表
     */
    List<Dept> queryAll(Dept dept);

    /**
     * 新增数据
     *
     * @param dept 实例对象
     * @return 影响行数
     */
    int insert(Dept dept);

    /**
     * 修改数据
     *
     * @param dept 实例对象
     * @return 影响行数
     */
    int update(Dept dept);

    /**
     * 通过主键删除数据
     *
     * @param deptno 主键
     * @return 影响行数
     */
    int deleteById(Integer deptno);


}

直接三层调用即可 :dao层—> Service 层---->Controller 层:

mapper文件



<mapper namespace="com.example.dao.DeptDao">

    <resultMap type="com.example.entity.Dept" id="DeptMap">
        <result property="deptno" column="deptno" jdbcType="INTEGER"/>
        <result property="dname" column="dname" jdbcType="VARCHAR"/>
        <result property="loc" column="loc" jdbcType="VARCHAR"/>
        
        <collection property="emps" javaType="list" ofType="com.example.entity.Emp">
            <id property="empno" column="id">id>
            <result property="ename" column="ename">result>
            <result property="hiredate" column="hiredate">result>
            <result property="job" column="job">result>
        collection>
    resultMap>

    
    <select id="queryById" resultMap="DeptMap">
       select
        d.deptno, d.dname, d.loc,e.empno id, e.ename,e.hiredate,e.job
	   from db_rabc.dept d, db_rabc.emp e
        where d.deptno=e.deptno and d.deptno = #{deptno}
    select>

    
    <select id="queryAllByLimit" resultMap="DeptMap">
        select
          deptno, dname, loc
        from db_rabc.dept
        limit #{offset}, #{limit}
    select>


    
    <select id="queryAll" resultMap="DeptMap">
        select
        deptno, dname, loc
        from db_rabc.dept
        <where>
            <if test="deptno != null">
                and deptno = #{deptno}
            if>
            <if test="dname != null and dname != ''">
                and dname = #{dname}
            if>
            <if test="loc != null and loc != ''">
                and loc = #{loc}
            if>
        where>
    select>

    
    <insert id="insert" keyProperty="deptno" useGeneratedKeys="true">
        insert into db_rabc.dept(dname, loc)
        values (#{dname}, #{loc})
    insert>

    
    <update id="update">
        update db_rabc.dept
        <set>
            <if test="dname != null and dname != ''">
                dname = #{dname},
            if>
            <if test="loc != null and loc != ''">
                loc = #{loc},
            if>
        set>
        where deptno = #{deptno}
    update>

    
    <delete id="deleteById">
        delete from db_rabc.dept where deptno = #{deptno}
    delete>

    
    <select id="getAll" resultMap="DeptMap">
		select 
        d.deptno, d.dname, d.loc,e.empno id, e.ename,e.hiredate,e.job
	   from db_rabc.dept d, db_rabc.emp e
	   where  d.deptno=e.deptno
    select>
mapper>

效果图:

springboot 之mybatis 多表关联查询_第1张图片

springboot 之mybatis 多表关联查询_第2张图片

无条件查询:

springboot 之mybatis 多表关联查询_第3张图片

多对一查询

自定义实体类:

package com.example.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;

/**
 * (Emp)实体类
 *
 * @author makejava
 * @since 2020-09-02 08:40:50
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp implements Serializable {
    private static final long serialVersionUID = -10111395061902311L;

    private Integer empno;

    private String ename;

    private String job;

    private Integer mgr;

    private Date hiredate;

    private Integer sal;

    private Integer comm;

    private Integer deptno;

    private Dept dept;

}

mapper文件:




<mapper namespace="com.example.dao.EmpDao">

    <resultMap type="com.example.entity.Emp" id="EmpMap">
        <result property="empno" column="empno" jdbcType="INTEGER"/>
        <result property="ename" column="ename" jdbcType="VARCHAR"/>
        <result property="job" column="job" jdbcType="VARCHAR"/>
        <result property="mgr" column="mgr" jdbcType="INTEGER"/>
        <result property="hiredate" column="hiredate" jdbcType="TIMESTAMP"/>
        <result property="sal" column="sal" jdbcType="INTEGER"/>
        <result property="comm" column="comm" jdbcType="INTEGER"/>
        <result property="deptno" column="deptno" jdbcType="INTEGER"/>

        
        <association property="dept" column="dname" javaType="com.example.entity.Dept">
            <result property="dname" column="dname">result>
        association>
    resultMap>

    
    <select id="queryById" resultMap="EmpMap">
        select
          e.empno, e.ename, e.job, e.mgr, e.hiredate, e.sal, e.comm, d.dname
        from db_rabc.emp e,db_rabc.dept d
        where empno = #{empno}
    select>

    
    <select id="queryAllByLimit" resultMap="EmpMap">
        select
          empno, ename, job, mgr, hiredate, sal, comm, deptno
        from db_rabc.emp
        limit #{offset}, #{limit}
    select>


    
    <select id="queryAll" resultMap="EmpMap">
        select
        empno, ename, job, mgr, hiredate, sal, comm, deptno
        from db_rabc.emp
        <where>
            <if test="empno != null">
                and empno = #{empno}
            if>
            <if test="ename != null and ename != ''">
                and ename = #{ename}
            if>
            <if test="job != null and job != ''">
                and job = #{job}
            if>
            <if test="mgr != null">
                and mgr = #{mgr}
            if>
            <if test="hiredate != null">
                and hiredate = #{hiredate}
            if>
            <if test="sal != null">
                and sal = #{sal}
            if>
            <if test="comm != null">
                and comm = #{comm}
            if>
            <if test="deptno != null">
                and deptno = #{deptno}
            if>
        where>
    select>

    
    <insert id="insert" keyProperty="empno" useGeneratedKeys="true">
        insert into db_rabc.emp(ename, job, mgr, hiredate, sal, comm, deptno)
        values (#{ename}, #{job}, #{mgr}, #{hiredate}, #{sal}, #{comm}, #{deptno})
    insert>

    
    <update id="update">
        update db_rabc.emp
        <set>
            <if test="ename != null and ename != ''">
                ename = #{ename},
            if>
            <if test="job != null and job != ''">
                job = #{job},
            if>
            <if test="mgr != null">
                mgr = #{mgr},
            if>
            <if test="hiredate != null">
                hiredate = #{hiredate},
            if>
            <if test="sal != null">
                sal = #{sal},
            if>
            <if test="comm != null">
                comm = #{comm},
            if>
            <if test="deptno != null">
                deptno = #{deptno},
            if>
        set>
        where empno = #{empno}
    update>

    
    <delete id="deleteById">
        delete from db_rabc.emp where empno = #{empno}
    delete>

    
    <select id="getAll" resultMap="EmpMap">
		  select
          e.empno, e.ename, e.job, e.mgr, e.hiredate, e.sal, e.comm, d.dname
        from db_rabc.emp e,db_rabc.dept d
    select>
mapper>

Dao 层

package com.example.dao;

import com.example.entity.Emp;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * (Emp)表数据库访问层
 *
 * @author makejava
 * @since 2020-09-02 08:40:50
 */
@Component
public interface EmpDao {

    /**
     * 通过ID查询单条数据
     *
     * @param empno 主键
     * @return 实例对象
     */
    Emp queryById(Integer empno);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    List<Emp> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 无条件查询
     *
     * @return 对象列表
     */

    List<Emp> getAll();

    /**
     * 通过实体作为筛选条件查询
     *
     * @param emp 实例对象
     * @return 对象列表
     */
    List<Emp> queryAll(Emp emp);

    /**
     * 新增数据
     *
     * @param emp 实例对象
     * @return 影响行数
     */
    int insert(Emp emp);

    /**
     * 修改数据
     *
     * @param emp 实例对象
     * @return 影响行数
     */
    int update(Emp emp);

    /**
     * 通过主键删除数据
     *
     * @param empno 主键
     * @return 影响行数
     */
    int deleteById(Integer empno);


    Emp getEmpAndDept();
}

直接三层调用即可 :dao层—> Service 层---->Controller 层

测试效果图:

springboot 之mybatis 多表关联查询_第4张图片
其他同理做法即可

你可能感兴趣的:(springboot,mybatis,springboot)