• mybatis通过注解实现一对多查询


    1:AddressVo.java

    package com.wzh.app.address;
    
    public class AddressVo{
        private long id;
        private long personId;
        private String address;
    
        public long getId() {
            return id;
        }
    
        public void setId(long id) {
            this.id = id;
        }
    
        public long getPersonId() {
            return personId;
        }
    
        public void setPersonId(long personId) {
            this.personId = personId;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }

    2:AddressMapper.java

    package com.wzh.app.address;
    
    import org.apache.ibatis.annotations.Mapper;
    import org.apache.ibatis.annotations.Select;
    
    import java.util.List;
    
    @Mapper
    public interface AddressMapper {
    
        @Select("<script>" +
                " select id,person_id as personId,address from t_address where person_id=#{personId}" +
                "</script>")
        List<AddressVo> selectAddressByPersonId(long personId);
    }

    3:PersonVo.java

    package com.wzh.app.users;
    
    import com.fasterxml.jackson.annotation.JsonFormat;
    import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
    import com.wzh.app.address.AddressVo;
    
    import java.time.LocalDate;
    import java.util.List;
    
    @JsonIgnoreProperties(value = { "handler"})
    public class PersonVo {
        private Long id;
    
        private String name;
    
        @JsonFormat(pattern = "yyyy-MM-dd")
        private LocalDate birthday;
    
        private char sex;
    
        private List<AddressVo> addressList;
    
        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 LocalDate getBirthday() {
            return birthday;
        }
    
        public void setBirthday(LocalDate birthday) {
            this.birthday = birthday;
        }
    
        public char getSex() {
            return sex;
        }
    
        public void setSex(char sex) {
            this.sex = sex;
        }
    
        public List<AddressVo> getAddressList() {
            return addressList;
        }
    
        public void setAddressList(List<AddressVo> addressList) {
            this.addressList = addressList;
        }
    }

    4:PersonMapper.java

    package com.wzh.app.users;
    
    import org.apache.ibatis.annotations.*;
    import org.apache.ibatis.mapping.FetchType;
    
    import java.util.List;
    import java.util.Map;
    
    @Mapper
    public interface PersonMapper {
    
        @Select("<script>" +
                " select id,name,birthday,sex from t_person"+
                "</script>")
        @Results({
                @Result(id=true, property = "id", column="id"),
                @Result(property = "name", column = "name"),
                @Result(property = "birthday", column = "birthday"),
                @Result(property = "addressList", column = "id",//此处id表示将此字段作为selectAddressByPersonId查询的条件
                        many = @Many(select = "com.wzh.app.address.AddressMapper.selectAddressByPersonId",
                        fetchType = FetchType.LAZY)
                )
        })
        List<PersonVo> selectListPerson();
    
    }

    5:测试效果

    {
        "msg": "获取用户列表信息",
        "code": 0,
        "data": [
            {
                "id": 5,
                "name": "赵花",
                "birthday": "2002-12-20",
                "sex": "1",
                "addressList": [
                    {
                        "id": 1,
                        "personId": 5,
                        "address": "地址1"
                    },
                    {
                        "id": 2,
                        "personId": 5,
                        "address": "地址2"
                    }
                ]
            },
            {
                "id": 6,
                "name": "李四",
                "birthday": "2001-12-20",
                "sex": "1",
                "addressList": []
            },
            {
                "id": 7,
                "name": "王五",
                "birthday": "2009-01-20",
                "sex": "1",
                "addressList": []
            }
        ]
    }
  • 相关阅读:
    vim 多窗口编辑
    opengl笔记——旋转,一段代码的理解
    用条件变量实现事件等待器的正确与错误做法
    opengl笔记—— glMultMatrixf() 区别 glLoadMatrixf()
    Mule与其它web应用服务器的区别
    海量数据相似度计算之simhash短文本查找
    关于协方差矩阵的理解
    C++ STL中的常用容器浅谈
    唐-诗:《枫桥夜泊》
    唐-诗:《肚桑干》
  • 原文地址:https://www.cnblogs.com/yshyee/p/14201060.html
Copyright © 2020-2023  润新知