首页 > 其他分享 >mybatis返回集合对象包含List<String>

mybatis返回集合对象包含List<String>

时间:2023-03-20 11:56:57浏览次数:42  
标签:COMMENT String List team mybatis 团队 id name

mybatis返回集合对象包含List<String>

时间:2021-07-13 <iframe data-google-container-id="a!1" data-google-query-id="CIuQ2eXL6f0CFVC_lgodxTADhg" data-load-complete="true" frameborder="0" height="90" id="aswift_0" marginheight="0" marginwidth="0" name="aswift_0" scrolling="no" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9401041403549801&output=html&h=90&slotname=4032144190&adk=909515367&adf=577686739&pi=t.ma~as.4032144190&w=728&fwrn=4&fwrnh=100&lmt=1679283839&rafmt=12&format=728x90&url=http%3A%2F%2Fwww.manongjc.com%2Fdetail%2F25-awwiwhmiqmxtlqj.html&fwr=0&fwrattr=true&rh=90&rw=728&sfro=1&wgl=1&dt=1679283839355&bpp=4&bdt=396&idt=406&shv=r20230315&mjsv=m202303150101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D26d945f494f4fd8b-2284c74321d300af%3AT%3D1652222944%3ART%3D1652222944%3AS%3DALNI_MYjlxY0KPbYhOmtg7nNY5GStS1jng&gpic=UID%3D00000bd1dfbccf51%3AT%3D1677929753%3ART%3D1679277855%3AS%3DALNI_MZH4w812WAEFvnNgyGc_MJvJdGqeQ&correlator=6308245294621&frm=20&pv=2&ga_vid=1196238526.1679283840&ga_sid=1679283840&ga_hid=1044623963&ga_fc=0&u_tz=480&u_his=1&u_h=800&u_w=1280&u_ah=752&u_aw=1280&u_cd=24&u_sd=2&adx=107&ady=307&biw=1263&bih=644&scr_x=0&scr_y=0&eid=44777876%2C31072662%2C31072664%2C44759876%2C44759927%2C44759842%2C31072951%2C31073057%2C31073103%2C31073142%2C21065724&oid=2&pvsid=2344929420730827&tmod=347710784&uas=0&nvt=1&ref=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DbHKZ9lp2C9l5eVYL85NB0CqHtDK4c4VqGd5jiSrAC1BjtaX9WChDP3u2S6_u65tDtEVjp1L59xMONhyXDAOl0q%26wd%3D%26eqid%3Dd9db626900025180000000066417d64a&fc=896&brdim=0%2C0%2C0%2C0%2C1280%2C0%2C1280%2C752%2C1280%2C644&vis=1&rsz=%7C%7CpoeE%7C&abl=CS&pfx=0&fu=128&bc=23&ifi=1&uci=a!1&fsb=1&xpc=vC4aPPTUEu&p=http%3A//www.manongjc.com&dtd=432" width="728"></iframe> 本文章向大家介绍mybatis返回集合对象包含List<String>,主要包括mybatis返回集合对象包含List<String>使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。 <iframe data-google-container-id="a!2" data-google-query-id="COHi2eXL6f0CFZCnlgod1iYEgA" data-load-complete="true" frameborder="0" height="90" id="aswift_1" marginheight="0" marginwidth="0" name="aswift_1" scrolling="no" src="https://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-9401041403549801&output=html&h=90&slotname=1516503570&adk=61531810&adf=3996677496&pi=t.ma~as.1516503570&w=728&lmt=1679283839&rafmt=12&format=728x90&url=http%3A%2F%2Fwww.manongjc.com%2Fdetail%2F25-awwiwhmiqmxtlqj.html&wgl=1&dt=1679283839359&bpp=1&bdt=400&idt=437&shv=r20230315&mjsv=m202303150101&ptt=9&saldr=aa&abxe=1&cookie=ID%3D26d945f494f4fd8b-2284c74321d300af%3AT%3D1652222944%3ART%3D1652222944%3AS%3DALNI_MYjlxY0KPbYhOmtg7nNY5GStS1jng&gpic=UID%3D00000bd1dfbccf51%3AT%3D1677929753%3ART%3D1679277855%3AS%3DALNI_MZH4w812WAEFvnNgyGc_MJvJdGqeQ&prev_fmts=728x90&correlator=6308245294621&frm=20&pv=1&ga_vid=1196238526.1679283840&ga_sid=1679283840&ga_hid=1044623963&ga_fc=0&u_tz=480&u_his=1&u_h=800&u_w=1280&u_ah=752&u_aw=1280&u_cd=24&u_sd=2&adx=107&ady=517&biw=1263&bih=644&scr_x=0&scr_y=0&eid=44777876%2C31072662%2C31072664%2C44759876%2C44759927%2C44759842%2C31072951%2C31073057%2C31073103%2C31073142%2C21065724&oid=2&pvsid=2344929420730827&tmod=347710784&uas=0&nvt=1&ref=https%3A%2F%2Fwww.baidu.com%2Flink%3Furl%3DbHKZ9lp2C9l5eVYL85NB0CqHtDK4c4VqGd5jiSrAC1BjtaX9WChDP3u2S6_u65tDtEVjp1L59xMONhyXDAOl0q%26wd%3D%26eqid%3Dd9db626900025180000000066417d64a&fc=896&brdim=0%2C0%2C0%2C0%2C1280%2C0%2C1280%2C752%2C1280%2C644&vis=1&rsz=%7C%7CpoeE%7C&abl=CS&pfx=0&fu=256&bc=23&ifi=2&uci=a!2&fsb=1&xpc=OHFy2Wy7Ow&p=http%3A//www.manongjc.com&dtd=443" width="728"></iframe>

需求:
最近遇到一个需求:

一个团队对应多个人,一个人只能有一个团队
根据团队的成绩的降序,查询出每个团队的信息,和其中每一个团队中每个人的名字。

分析:

首先:需要查询出每个人团队的信息
其次:查询出每个团队中对应的用户的名字
所以返回结果应该是
返回一个List,List中每一个对象都是一个团队,然后每一个团队的人员名单显示到List<String> userNames中。
数据库表:(数据库表只显示了部分必要字段)

团队表

CREATE TABLE `team` (
  `id` varchar(255) NOT NULL COMMENT '雪花算法,id',
  `team_id` varchar(255) DEFAULT NULL COMMENT '团队id',
  `team_name` varchar(255) DEFAULT NULL COMMENT '团队名字',
  `group_results` float(8,2) DEFAULT NULL COMMENT '团队成绩'
  PRIMARY KEY (`id`) USING BTREE
) COMMENT='团队表';

人员表

CREATE TABLE `person` (
  `id` varchar(255) NOT NULL DEFAULT '' COMMENT '主键id(用户id)',
  `team_id` varchar(22) DEFAULT NULL COMMENT '团队id',
  `user_code` varchar(22) DEFAULT NULL COMMENT '学号',
  `user_name` varchar(22) DEFAULT NULL COMMENT '用户姓名',
  PRIMARY KEY (`id`) USING BTREE
) COMMENT='人员表';

解决方案:

SELECT
    t.team_id,
    t.team_name,
    t.group_results,
    p.`user_name` 
FROM
    `person` p
    RIGHT JOIN ( SELECT t.team_id, t.team_name, t.group_results FROM `team` t ) t ON p.team_id = t.team_id 
ORDER BY
    group_results DESC

实体
构造的实体,很简单,但是重要的一点就是返回的用户名的集合

@Data
public class TeamRanking {
    private String teamName;//小组名字
    private Double teamGrade;//小组成绩
    private List<String> userNames;
    private Integer teamId;
})

dao层
dao层返回的是上面那个对象的集合

List<TeamRanking> selectTeamRanking();


mybatis的mapper
在Mapper中,使用了ResultMap的collection标签,并且:
collection的properties=对应名字的集合
collection标签中result标签的propertis内容也是对应集合的名字。

<select id="selectTeamRanking" resultMap="selectTeamRankingMap">
        SELECT
            t.team_id,
            t.team_name,
            t.group_results,
            p.`user_name`
        FROM
            `person` p
            RIGHT JOIN ( SELECT t.team_id, t.team_name, t.group_results FROM `team` t  ) t ON p.team_id = t.team_id
        ORDER BY
            group_results DESC
    </select>


    <resultMap id="selectTeamRankingMap" type="com.tfjybj.typing.model.TeamRankingModel">
        <result property="teamId" column="team_id"></result>
        <result property="teamName" column="team_name"></result>
        <result property="teamGrade" column="group_results"></result>
        <collection property="userNames" ofType="string">
            <result property="userNames" column="user_name"></result>
        </collection>
    </resultMap>

踩过的坑:
笔者也使用过,但是最后都没有成功,最后在大家的讨论中,尝试出来上面的方式。

如果有大佬研究过mybaits的映射源码的,笔者非常迫切的请求赐教。

<resultMap id="selectTeamRankingMap" type="com.tfjybj.typing.model.TeamRankingModel">
        <result property="teamId" column="team_id"></result>
        <result property="teamName" column="team_name"></result>
        <result property="teamGrade" column="group_results"></result>
        <collection property="userNames" ofType="string"  column="user_name"></collection>
    </resultMap>
<resultMap id="selectTeamRankingMap" type="com.tfjybj.typing.model.TeamRankingModel">
        <result property="teamId" column="team_id"></result>
        <result property="teamName" column="team_name"></result>
        <result property="teamGrade" column="group_results"></result>
        <collection ofType="string" column="user_name">
            <result property="userNames"></result>
        </collection>
    </resultMap>

标签:COMMENT,String,List,team,mybatis,团队,id,name
From: https://www.cnblogs.com/qianxingji/p/17235810.html

相关文章