• mybatis的一些注意事项


    放假2个月,脑子蒙圈了,写个mybatis总是云里雾里,不过好歹还是复习了下。

    1.mybatis理论上应该是能够一直嵌套循坏的(我没有看源代码,这个是猜的,别信),因为太久没真正写过项目了,放假都是随便打了几行代码,真正用起来发现了许多问题(大部分都是以前解决过的,但是无奈忘了,

    2.对于1

    @Data
    public class PostBarInfoEntity {
    
        private Long postBarId;
        private Long catalogueId;
        private String postBarName;
        private String postBarExplain;
        private String postBarLogoUrl;
        private Long userId;
        private Long postCount;
        private Long userCount;
        private Date createTime;
        private Integer postBarStatus;
        private PostInfoEntity postInfoEntity;
        private PartitionCatalogueEntity partitionCatalogueEntity;
    }
    @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 NfUser nfUser;
    }
    <resultMap id="postBarInfo" type="postBarInfoEntity">
            <id property="postBarId" column="post_bar_id"/>
            <result property="partitionCatalogueEntity.catalogueName" column="catalogue_name"/>
            <result property="postBarName" column="post_bar_name"/>
            <result property="postBarExplain" column="post_bar_explain"/>
            <result property="postBarLogoUrl" column="post_bar_logo_url"/>
            <result property="postCount" column="post_count"/>
            <result property="userCount" column="user_count"/>
            <collection property="postInfoEntity" resultMap="postInfo"/>
        </resultMap>
    
        <resultMap id="postInfo" type="postInfoEntity">
            <id property="postId" column="post_id"/>
            <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"/>
            <association property="nfUser">
                <result property="userName" column="user_name"/>
            </association>
        </resultMap>
    
        <!--某个贴吧所有界面的帖子,通过置顶降序排序-->
        <!--此查询用了4个表,这是不应该的,最多3个-->
        <select id="listAllPostInfos" resultMap="postBarInfo">
            SELECT catalogue_name,post_bar_name,post_bar_explain,post_bar_logo_url,post_count, user_count, post_id, post_title, post_content, top_post, wonderful_post, user_name, A.create_time
            FROM post_info A, nf_user B, partition_catalogue C, post_bar_info D
            WHERE post_status = 1
            AND B.status = 1
            AND A.post_bar_id = D.post_bar_id
            AND D.post_bar_id = #{param1}
            AND D.catalogue_id = C.catalogue_id
            AND A.user_id = B.user_id
            ORDER BY top_post DESC, create_time
        </select>

    说下发现的问题:

    1.平时只有两个表的连接查询,即实体类1里有另一个实体类2的属性字段,也是直接在mapper文件中写实体类2的xx属性就行了,但是上面原来这么写的时候报错了,要我明确指出实体类2.xx.  意思就是:class A { private B b}           mapper双表查询直接写<result properties="xx" column=" xx">   但是上面这样写报错了,叫我<result properties="B.xx" column=" xx">.   properties对应的实体类属性,column对应的数据库的列名

    2.如果在resultMap中不写<id properties column/>属性的话,如果后面的数据都一样,是会被覆盖的,让我想起了elasticsearch也要明确指定id,不然一样的话也会覆盖。也就是说id最好一定要写。

    3.为什么阿里巴巴的规范说不能超过三个表的连接会影响性能,性能的能影响是肯定,但是麻烦,看起来乱七八糟也是一个,主要是我这边想试试并且(en……vue里面我想少些几个请求才这样搞的,)……事实证明,像我上面那样写的话,本来只需要查一次的数据也会跟着子数据查一样多的次数,这样肯定影响性能的。

    4.再就是mysql的=,mysql不能三个=的,这个我记得我似乎刚开始学mysql的时候也试过。 意思是

    A.post_bar_id = D.post_bar_id =  2
    是没有效果的,至少也要写成
    AND A.post_bar_id = D.post_bar_id
    AND D.post_bar_id = 2


    过了10来分钟我又回来了,我的天,发现实际上要完成那个页面,如果想一个sql语句写完,要写5个表,额。我傻了,不作死了,我还是赶紧拆sql语句改成多次请求了。

  • 相关阅读:
    centos7下redis6的安装
    Centos MySQL 5.7安装、升级教程
    Python之SSH-paramiko模块的使用
    Linux进程管理工具之Supervisor的使用
    Python-RPC框架之-ZeroRPC和SimpleXMLRPCServer
    Django入门--简单的form验证
    redis常用指令
    MySQL自增主键ID重新排序
    批量微博删除脚本
    解决springboot2.x集成redis节点故障redisTemplate报错redis Command timed out
  • 原文地址:https://www.cnblogs.com/woyujiezhen/p/12493670.html
Copyright © 2020-2023  润新知