mybaties list 返回重复数据 | 您所在的位置:网站首页 › mybatisplus去重查询集合 › mybaties list 返回重复数据 |
记录下mybatis的集合查询中碰到的问题 描述下场景,比如一个人有多个qq号(假设一个人可以有重复的qq号)
数据库结构,有两张表: people表 idname1jackpeople_qq表 idpeople_idqq11123456212345673145678941123456
实体类: Java代码 import java.io.Serializable; import java.util.List; public class People implements Serializable{ private static final long serialVersionUID = -5935066186174346694L; private Long id; private String name; private List qqs; 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 List getQqs() { return qqs; } public void setQqs(List qqs) { this.qqs = qqs; } @Override public String toString() { return "People [id=" + id + ", name=" + name + ", qqs=" + qqs + "]"; } }
mapper接口: Java代码 import com.hnpicheng.mybatisissue.domain.People; public interface PeopleMapper { People selectPeopleById( Long id); }
测试代码: Java代码 import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hnpicheng.mybatisissue.domain.People; import com.hnpicheng.mybatisissue.mapper.PeopleMapper; public class App { public static void main( String[] args ) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-mybatis.xml"); PeopleMapper peopleMapper = context.getBean(PeopleMapper.class); People p = peopleMapper.selectPeopleById(1L); System.out.println(p); } }
PeopleMapper.xml
根据业务需要,如果不需要将重复数据查出来: 那么可以使用 Xml代码 [xml] view plain copy select p.*,pq.qq from people p left join people_qq pq on p.id = pq.people_id where p.id = #{id}
测试结果,优点只要查一次,对于需要排重的查询业务,可以用这个方法: Java代码 DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - Preparing: select * from people where id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - Preparing: select qq from people_qq where people_id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - Preparing: select * from people where id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - Preparing: select qq from people_qq where people_id = ? DEBUG [main] - ==> Parameters: 1(Long) DEBUG [main] - |
CopyRight 2018-2019 实验室设备网 版权所有 |