mybatis实现一个数据库一对多的查询结果映射到java实体类

2022-08-21 12:39:27

一、测试表格:test1、test2

二、对应java实体类:test1中包含test2的list

test1:

public class Test1 {

    private Integer id;
    private String name1;
    private List<Test2> test2s;

    public Integer getId() {
        return id;
    }

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

    public String getName1() {
        return name1;
    }

    public void setName1(String name1) {
        this.name1 = name1;
    }

    public List<Test2> getTest2s() {
        return test2s;
    }

    public void setTest2s(List<Test2> test2s) {
        this.test2s = test2s;
    }
}

test2:

public class Test2 {

    private Integer id;
    private String operat;
    private Integer tid;

    public Integer getId() {
        return id;
    }

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

    public String getOperat() {
        return operat;
    }

    public void setOperat(String operat) {
        this.operat = operat;
    }

    public Integer getTid() {
        return tid;
    }

    public void setTid(Integer tid) {
        this.tid = tid;
    }
}

三、重点:mybatis的xml配置

第一种写法:

    <resultMap id="test1" type="com.example.demo2.entity.Test1">
        <id property="id" column="id"></id>
        <result property="name1" column="name1" />
        <collection property="test2s" ofType="com.example.demo2.entity.Test2" >
            <id property="id" column="id" ></id>
            <result property="operat" column="operat" />
            <result property="tid" column="tid" />
        </collection>
    </resultMap>


    <select id="getTest1" resultMap="test1" >
        select * from test1 left OUTER join test2 on test2.tid=test1.id;
    </select>

第二种写法

    <resultMap id="test2" type="com.example.demo2.entity.Test2">
        <id property="id" column="id" ></id>
        <result property="operat" column="operat" />
        <result property="tid" column="tid" />
    </resultMap>

    <resultMap id="test1" type="com.example.demo2.entity.Test1">
        <id property="id" column="id"></id>
        <result property="name1" column="name1" />
        <collection property="test2s" resultMap="test2" >
        </collection>
    </resultMap>

    <select id="getTest1" resultMap="test1" >
        select * from test1 left OUTER join test2 on test2.tid=test1.id;
    </select>

四、测试

    @Test
    void test2() {
        List<Test1> all = test1Mapper.getTest1();
        System.out.println(all);
    }



结果:
[Test1{id=1, name1='123hhh', test2s=[com.example.demo2.entity.Test2@4ee25d80]},
 Test1{id=2, name1='李【四', test2s=[com.example.demo2.entity.Test2@16a35bd]},
 Test1{id=3, name1='外网', test2s=[com.example.demo2.entity.Test2@ba17be6]},
 Test1{id=4, name1='任务', test2s=[com.example.demo2.entity.Test2@6f798482]}]

我们发现:虽然结果确实是把test2结果集合到了test1的实体类里面,但是却只存储了第一条数据,

原因:就是查询的几个表里面的字段名不能有重复的

解决方法:①如果不是必须使用的字段,可以在resultmap映射中将个重复名字的字段去掉

②也可以直接修改数据库列名,把几个表重复的列名改成不一样的

③可以在查询时候,查询语句里面给重复字段取别名

补充:

注意,在test1 只有关联test2一个一对多关系时,可以很好的把test2中多余重复去重;

但是,如果test1关联test2,并且test1又关联test3 .......等多个一对多关系时,查出来的数据就会出现重复,此时可能就需要在java层面对数据进行去重

  • 作者:goxingman
  • 原文链接:https://blog.csdn.net/goxingman/article/details/111034337
    更新时间:2022-08-21 12:39:27