mybatis 返回 Set

来源:互联网 发布:ubuntu修改镜像源 编辑:程序博客网 时间:2024/06/06 01:19

mybatis 返回 Set
相信大家平时用到返回List的情况比较多
一.那为什么要返回set集合
1.因为Set集合的特性就是 不能有重复数据,所以通常我们返回List之后还需要去掉重复数据 是不是很麻烦?可以直接返回Set直接帮我我们去掉重复数据(记得要重写实体类的equals和hashCode 这两个方法)


下面是测试代码:
1:实体类 记得要重写实体类的equals和hashCode 这两个方法(这里只展示这两个方法)

@Overridepublic int hashCode() {        final int prime = 31;        int result = 1;        result = prime * result + ((b == null) ? 0 : b.hashCode());        result = prime * result + ((frequency == null) ? 0 : frequency.hashCode());        result = prime * result + ((ids == null) ? 0 : ids.hashCode());        result = prime * result + ((magnetism == null) ? 0 : magnetism.hashCode());        result = prime * result + ((reelNumber == null) ? 0 : reelNumber.hashCode());        result = prime * result + ((trademark == null) ? 0 : trademark.hashCode());        return result;    }    @Override    public boolean equals(Object obj) {        if (this == obj)            return true;        if (obj == null)            return false;        if (getClass() != obj.getClass())            return false;        MagnetizingCurveBH other = (MagnetizingCurveBH) obj;        if (b == null) {            if (other.b != null)                return false;        } else if (!b.equals(other.b))            return false;        if (frequency == null) {            if (other.frequency != null)                return false;        } else if (!frequency.equals(other.frequency))            return false;        if (ids == null) {            if (other.ids != null)                return false;        } else if (!ids.equals(other.ids))            return false;        if (magnetism == null) {            if (other.magnetism != null)                return false;        } else if (!magnetism.equals(other.magnetism))            return false;        if (reelNumber == null) {            if (other.reelNumber != null)                return false;        } else if (!reelNumber.equals(other.reelNumber))            return false;        if (trademark == null) {            if (other.trademark != null)                return false;        } else if (!trademark.equals(other.trademark))            return false;        return true;    }

2: .xml 配置文件

<select id="testFindSet" resultMap="BaseResultMap" >      select      <include refid="Base_Column_List" />      from ANALYSIS_MAGNETIZING_CURVE_BH       where TRADEMARK = 'AA0182'</select>
<resultMap id="BaseResultMap" type="com.sgai.qgpam.analysis.entity.MagnetizingCurveBH" >    <id column="SID" property="sid" jdbcType="DECIMAL" />    <result column="TRADEMARK" property="trademark" jdbcType="VARCHAR" />    <result column="REEL_NUMBER" property="reelNumber" jdbcType="VARCHAR" />    <result column="FREQUENCY" property="frequency" jdbcType="DECIMAL" />    <result column="MAGNETISM" property="magnetism" jdbcType="DECIMAL" />    <result column="B" property="b" jdbcType="DECIMAL" />    <result column="CREATED_DT" property="createdDt" jdbcType="TIMESTAMP" />    <result column="CREATED_BY" property="createdBy" jdbcType="VARCHAR" />  </resultMap>

3.mybatis接口

public Set<MagnetizingCurveBH> testFindSet();

4 junit测试类

@Testpublic void testFindSet(){    Set<MagnetizingCurveBH> list = magnetizingCurveBHMapper.testFindSet();    for (MagnetizingCurveBH m : list)    {        System.out.println(m);    }}

5 console 结果展示
1 还没有重写equals和hashCode
这里写图片描述
2 重写之后
这里写图片描述
大家要注意 不重复equals和hashCode 还是会有重复数据呦

原创粉丝点击