联表查询(一对多 多对一)

一对多情况处理流程:

在user类中引入dept类
   @Data
public class User {
    private Long id;
    private String username;
    private String password;
    private String phone;
    private String info;
    private Integer status;
    private Integer balance;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    private  String  deptName;
    //将部门类引入近来
    private  Dept dept;
}

2:usermapper中,通过员工名字查找信息以及部门信息
 User getAllByID(@Param("username") String username);
3:xml中设置一下resultmap
<resultMap id="resultOne" type="com.hefu.demos.web.domain.po.User">
    <id property="id" column="id"></id>
    <result property="username" column="username"></result>
    <result property="password" column="password"></result>
    <result property="password" column="password"></result>
    <result property="phone" column="phone"></result>
    <result property="info" column="info"></result>
    <result property="status" column="status"></result>
    <result property="balance" column="balance"></result>
    <result property="deptName" column="dept_name"></result>
    <association property="dept" javaType="com.hefu.demos.web.domain.po.Dept">
        <id property="name" column="name"></id>
        <result property="nums" column="nums"></result>
    </association>
</resultMap>

   <select id="getAllByID" resultMap="resultOne">
        select  * from user left join dept on user.dept_name = dept.id where user.username = #{username}
    </select>
4:测试类调用
    @Test
    void TestgetAllByID(){
        String username = "Jack";
       User user =  userMapper.getAllByID(username);
        System.out.println("user =>" + user);

    }

多对一:

1:部门类中传入员工类
@Data
public class Dept {
    private Long id;
    private String name;
    private  String  nums;
    //将user类引入进来
    private List<User> user;
}


2:mapper中定义接口
@Mapper
public interface DeptMapper extends BaseMapper<User> {
 List<Dept> getUsers(@Param("name") String name);
}

3:xml中
 <resultMap id="resultDeptUsers" type="com.hefu.demos.web.domain.po.Dept">
        <id property="id" column="id"></id>
        <result property="name" column="username"></result>
        <result property="nums" column="password"></result>
        <collection property="user" ofType="com.hefu.demos.web.domain.po.User">
            <id property="id" column="id"></id>
            <result property="username" column="username"></result>
            <result property="password" column="password"></result>
            <result property="password" column="password"></result>
            <result property="phone" column="phone"></result>
            <result property="info" column="info"></result>
            <result property="status" column="status"></result>
            <result property="balance" column="balance"></result>
            <result property="deptName" column="dept_name"></result>
        </collection>
    </resultMap>

    <select id="getUsers" resultMap="resultDeptUsers">
    select  * from dept left join user on dept.id = user.dept_name where dept.name = #{name}
    </select>

4测试类中:
@SpringBootTest
public class DeptMapperTest {
    @Autowired
    private DeptMapper deptMapper;
    @Test
    void  getDeptUsers(){
        String name = "事业部";
        List<Dept> user = deptMapper.getUsers(name);
        System.out.println("返回的结果是: " + user);
    }

}



返回的结果是: [Dept(id=1, name=, nums=123, user=[User(id=1, username=, password=123, phone=18688990011, info={"age": 24, "intro": "英文老师", "gender": "female"}, status=1, balance=200, createTime=null, updateTime=null, deptName=1, dept=null)])]

你可能感兴趣的:(后端crud,java,windows,开发语言)