Mybatis+查询的结果集难道还要经过二次处理?不会吧?怎么优化?




能不能把另一个表(deparment)的字段dname,放到和用户表(Users)的字段id、name、did同级的地方????

代码如下:*

Deparment.java
package com.xujl.myspring.entity;
//import javax.persistence.*;
import jakarta.persistence.*;

@Entity
@Table(name = "deparment")
public class Deparment {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @Column(name = "dname")
  private String dname;

  @Column(name = "sort")
  private long sort;

  public long getSort() {
    return sort;
  }

  public void setSort(long sort) {
    this.sort = sort;
  }


  public String getDname() {
    return dname;
  }

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


  public long getId() {
    return id;
  }

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

}
Users.java

package com.xujl.myspring.entity;
//import javax.persistence.*;
import jakarta.persistence.*;

@Entity
@Table(name = "users")
public class Users {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @Column(name = "name")
  private String name;

  @Column(name = "did")
  private long did;

  private Object deparObj;

  public Object getDeparObj() {
    return deparObj;
  }

  public void setDeparObj(Object deparObj) {
    this.deparObj = deparObj;
  }

  public long getId() {
    return id;
  }

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


  public String getName() {
    return name;
  }

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


  public long getDid() {
    return did;
  }

  public void setDid(long did) {
    this.did = did;
  }

}
DeparmentMapper.java
package com.xujl.myspring.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xujl.myspring.entity.Deparment;

public interface DeparmentMapper extends BaseMapper<Deparment> {
    Deparment selectById(long id);
}
UsersMapper.java

package com.xujl.myspring.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xujl.myspring.entity.Users;

import java.util.List;

public interface UsersMapper extends BaseMapper<Users> {
     List findById(Long id);
}
DeparmentMapper.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">
<!-- namespace为映射的地址,也就是只有在这个接口中才能使用下边的内容-->
<mapper namespace="com.xujl.myspring.mapper.DeparmentMapper">
    <select id="selectById" resultType="com.xujl.myspring.entity.Deparment">
        select * from deparment where id = #{id}
    </select>

</mapper>
UsersMapper.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">
<!-- namespace为映射的地址,也就是只有在这个接口中才能使用下边的内容-->
<mapper namespace="com.xujl.myspring.mapper.UsersMapper">
    <!-- id 外部为调用的名字 resultType 为和数据库表中映射的类名,parameterType就是传递进来的参数id的类型-->
    <select id="findById" resultMap="UserList" parameterType="java.lang.Long">
        select u.*,d.dname from users u left join deparment d on u.did=d.id where u.did = #{id}
    </select>

    <resultMap id="UserList" type="com.xujl.myspring.entity.Users">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="did" column="did"/>
        <association property="deparObj" column="did"
                     javaType="com.xujl.myspring.entity.Deparment"
                     select="com.xujl.myspring.mapper.DeparmentMapper.selectById" />
    </resultMap>

</mapper>

文件结构

阅读 6.9k
3 个回答

在 resultMap 中直接映射 dname 字段,而不是通过 association 标签。

我看你那个UsersMapper.xml里用的
association 标签

UsersMapper.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.xujl.myspring.mapper.UsersMapper">
    <select id="findById" resultMap="UserList" parameterType="java.lang.Long">
        select u.id, u.name, u.did, d.dname
        from users u
        left join department d on u.did = d.id
        where u.id = #{id}
    </select>

    <resultMap id="UserList" type="com.xujl.myspring.entity.Users">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="did" column="did"/>
        <result property="dname" column="dname"/>
    </resultMap>
</mapper>

Users.java

package com.xujl.myspring.entity;

import jakarta.persistence.*;

@Entity
@Table(name = "users")
public class Users {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;

  @Column(name = "name")
  private String name;

  @Column(name = "did")
  private long did;

  @Transient
  private String dname;

  public String getDname() {
    return dname;
  }

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

  public long getId() {
    return id;
  }

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

  public String getName() {
    return name;
  }

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

  public long getDid() {
    return did;
  }

  public void setDid(long did) {
    this.did = did;
  }
}

改一下 UsersMapper.xml:

<mapper namespace="com.xujl.myspring.mapper.UsersMapper">
    <select id="findById" resultMap="UserList" parameterType="java.lang.Long">
        select u.*, d.dname from users u 
        left join deparment d on u.did=d.id 
        where u.did = #{id}
    </select>

    <resultMap id="UserList" type="com.xujl.myspring.entity.Users">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="did" column="did"/>
        <result property="dname" column="dname"/>
    </resultMap>
</mapper>

Users类:

public class Users {
    private Long id;
    private String name;
    private Long did;
    private String dname;  // 新增字段
    
    // getter和setter方法
}

这就是实体类只有一个的缺陷,Po、Vo、Dto都是用一个,导致其无法自定义。

请按照entity分包规范进行实现。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏