能不能把另一个表(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>
文件结构
在 resultMap 中直接映射 dname 字段,而不是通过 association 标签。
我看你那个UsersMapper.xml里用的
association 标签
UsersMapper.xml
Users.java