mybatis的关联查询以及count

摘要:
这是上面的userName。但事实上,当我们有多个表时,通常每个表都有一个POJO类。您还可以将StringuserName更改为NfUsernfUser,然后在mapper中将其更改为˂resultproperties=“nfUser.userName”column=“user_name”

1.多表查询,1对1的时候,最简单的做法

<resultMap id="postInfo" type="postInfoEntity">
        <id property="postId" column="post_id"/>
        <result property="userName" column="user_name"/>
        <result property="postTitle" column="post_title"/>
        <result property="postContent" column="post_content"/>
        <result property="topPost" column="top_post"/>
        <result property="wonderfulPost" column="wonderful_post"/>
        <result property="createTime" column="create_time"/>
        <result property="count" column="num"/>
    </resultMap>

    <!--某个贴吧所有界面的帖子,通过置顶降序排序-->
    <select id="listAllPostInfos" resultMap="postInfo">
        SELECT A.post_id, COUNT(*) num, post_title, post_content, top_post, wonderful_post, user_name, A.create_time
        FROM post_info A, nf_user B, reply_post_info C
        WHERE post_status = 1 AND C.reply_status = 1
        AND post_bar_id = #{param1}
        AND A.user_id = B.user_id
        AND A.post_id = C.post_id
        GROUP BY post_id
        ORDER BY top_post DESC, create_time
    </select>

其中COUNT(*) 取了一个别名,目的是为了对应resultMap中的<result property="count" column="num"/>, 然而仅仅这样是不够的,因为虽然查得到,但是mybatis映射不出来,他底层的反射和动态代理还需要我们在实体类进行设置——我们加一个属性和property对应就可以了

@Data
@Document(indexName = "post_info")
public class PostInfoEntity {

    @Id
    private Long postId;
    @Field(type = FieldType.Long)
    private Long postBarId;
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String postTitle;
    @Field(type = FieldType.Text, analyzer = "ik_max_word")
    private String postContent;
    @Field(type = FieldType.Long)
    private Long userId;
    @Field(type = FieldType.Long)
    private Long topPost;
    @Field(type = FieldType.Long)
    private Long wonderfulPost;
    @Field(type = FieldType.Integer)
    private Integer audit;
    @Field(type = FieldType.Long)
    private Long visitCount;
    @Field(type = FieldType.Integer)
    private Integer postStatus;
    @Field(type = FieldType.Date)
    private Date createTime;
    private Integer count;
    private String userName;

}
// 除了@Data注解其他都无视,其他的是elasticsearch的

2.多表联合查询,可以像我们上面一样,和COUNT(*)同一种写法,直接写一个<result>标签然后实体类加一个属性对应。就是上面的userName.

但是实际上我们多表的时候一般每一张表都有POJO类的,也可以把String userName改成NfUser nfUser  然后mapper中对应修改成<result properties="nfUser.userName" colum="user_name"

免责声明:文章转载自《mybatis的关联查询以及count》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇C#返回多个参数 ref及outShape.Type属性名称及对应值列表下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

mybatis插入Oracle数据库中日期型数据

前端页面type类型是:datetime-local,因为如果是date类型,只显示年月日,不显示时间,这个可以显示时间 但插入时会以:2020-02-0202T10:10  这个格式出现,所以下面代码,将这个格式转换为:2020-02-02 10:10 ,通过修改传来的user,然后通过set方法修改。 1 /** 2 * 新增用...

Dapper2.0.78手册翻译

Dapper - .Net下的一个简单的ORM框架 特性 Dapper是一个NuGet库,你可以把它添加到你的项目中,扩展你的IDbConnection接口。 提供了3个方法 查询并映射一个实例集合 public static IEnumerable<T> Query<T>(this IDbConnection cnn, strin...

python接口自动化测试

(1)环境准备: 接口测试的方式有很多,比如可以用工具(jmeter,postman)之类,也可以自己写代码进行接口测试,工具的使用相对来说都比较简单,重点是要搞清楚项目接口的协议是什么,然后有针对性的进行选择,甚至当工具不太适合项目时需要自己进行开发。 在我们项目的初期,我们采用的是jmeter进行接口测试,当时觉得这个工具上手简单,团队成员学习成本低,...

关于mybatis使用foreach插入速度较慢的问题

使用mybatis批量插入,看了这篇博客 https://blog.csdn.net/m0_37981235/article/details/79131493 我这种懒货懒得想其中原因,直接上手第三种! 结果测试多次,发现我插入8000条数据,第一种方式只需要30秒不到,可是第三种方法却需要一分多钟。 不知道原作者是怎么实现的,可能和插入数据的多少有关,...

【Mybatis进阶】动态代理

如果你想更深刻的理解Mybatis动态代理的原理,那么你应该先知道 什么是代理模式? 在没有动态代理的时候Mybatis是如何实现dao层的? 什么是代理模式 具体可以阅读笔者的博客—— 代理模式 在没有动态代理的时候Mybatis是如何实现dao层的 本篇博客基于mybatis的环境已经搭建完成,如果不知道如何搭建,具体可以阅读笔者的博客——【从零开...

微服务架构 SpringBoot(二)

第二天内容:想来想去玩个ssm小demo吧1.创建表 2..引入相关mybatis 数据库jar: <!--mybatis --> <dependency>   <groupId>org.mybatis.spring.boot</groupId>   <artifactId>mybatis-s...