mybatis 如何查找表里的某一个字段,然后返回它们的结果集list ?

本文详细介绍了如何在MyBatis中进行单个字段和多个字段的List映射,包括从数据库中获取数据并映射到自定义对象。示例展示了如何使用ResultMap进行复杂查询,如通过group by获取问题主题,以及如何通过关联查询获取多个字段的数据。同时,还提供了一个简单的查询示例,用于查询特定用户的所有讨论。
摘要由CSDN通过智能技术生成

单个字段的list映射

取自: https://blog.csdn.net/weixin_30723433/article/details/95642002?biz_id=102&utm_term=mybatis%20%20%E6%

<!--    问题类型-->
    <select id="getQuestionSubjects" parameterType="list" resultType="string">
    	select subject from question GROUP BY subject
    </select>

多个字段的list映射

@Data
@ToString
public class SelectQuestion {
    private String putUser;
    private Long qid;
    private String question;
    private String answer;
    private String subject;
    private List<String> selections; //Selection
}
	<resultMap id="SQuestion" type="SelectQuestion">
        <result property="qid" column="qid"></result>
        <result property="putUser" column="name"></result>
        <collection property="selections" javaType="ArrayList" ofType="SelectionEntity" select="getSelectMsgById" column="qid"/>
    </resultMap>
    <select id="getSelectMsgById" resultType="String">
        select  selection from  toanswer.selection where qid=#{qid}
    </select>
   <select id="getSelectQuestionByKeywords"   resultMap="SQuestion">
        SELECT  * from view_select_question where question like concat('%',#{keywords},'%')
    </select>

简单查询

     <select id="getDiscussByUid"  resultType="DiscussSimpleEntity">
        select did,content from discuss where uid=#{uid} and is_deleted=0;
   </select>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值