mybatis使用心得总结

首先下面的总结是基于mysql数据库的其他数据库是不是一样不知道

1.模糊查询

使用concat()函数。CONCAT(str1,str2,...)

SELECT * from t_cbo_location_apply
        
            and identity_type = #{identityType,jdbcType=INTEGER}
            and address_structure like
                concat("%",#{addressStructure,jdbcType=VARCHAR},"%")
            
        

2.大于号小于号

使用 标记将大于号和小于号圈起来

SELECT t1.*,t2.name,t2.id as orgId,1 as cond FROM t_pb_conference t1
                INNER JOIN t_pb_organization t2
                on t1.pb_id = t2.id
                
                    
                        and   #{nowDate,jdbcType=TIMESTAMP} and t1.status < 4 and t1.status >= 1]]>
                        and = #{nowDate,jdbcType=TIMESTAMP} and t1.status < 4 and t1.status >= 1]]>
                        and = 1]]>
                        and t1.status = 4
                    
                    and t1.conference_type = #{conferenceType,jdbcType=INTEGER} 
                    and t1.theme like concat("%",#{theme,jdbcType=VARCHAR},"%") 
                    
                        and t1.start_time between #{beginTime,jdbcType=VARCHAR} and #{endTime,jdbcType=VARCHAR}
                    
                    and t1.pb_id in
                        
                            #{id,jdbcType=BIGINT}
                        
                    
                    and t1.status > 0
                
                order by t1.release_time desc

3.批量更新数组(不是按照id批量设置某一个属性的值,而是更新多行数据)


        UPDATE t_pb_conference
        
            
                
                    
                        WHEN id = #{item.id,jdbcType=BIGINT} then #{item.status,jdbcType=INTEGER}
                    
                
            
            
                
                    
                        WHEN id = #{item.id,jdbcType=BIGINT} then #{item.version,jdbcType=INTEGER} + 1
                    
                
            
        
        
            
                id = #{item.id,jdbcType=BIGINT} and version = #{item.version,jdbcType=INTEGER}
            

        
    

这个sql需要传进来一个list ,updateList中是要更新的数据库队列的值。这里更新的条件是按照id和锁version同时满足。至于要更新的行的属性,只需要将item.xxx进行 替换就行

4.mybatis批量查询 结果集按照传入参数的顺序输出

这里我自己的例子找不到了 找了个网上的例子

5.mybatis 的insert动态语句

不要将字段和值写死,要改成动态的。虽然这样会多写很多代码。ε=(´ο`*)))唉。

个人不太喜欢用代码生成器,生成了方法名字也得改。


        INSERT INTO t_cbo_location_apply
        
            
                resident_id,
            
            
                community_id,
            
            
                community_name,
            
            
                org_id,
            
            
                org_name,
            
            
                address_id,
            
            
                address_structure,
            
            
                create_time,
            
            
                update_time,
            
            
                update_user_id,
            
            
                identity_type,
            
            
                apply_status,
            
            
                enable,
            
            
                version,
            
            
                comment,
            
            
                card_type,
            
            
                card_num,
            
            
                credentials_photo_ids,
            
            
                resident_name,
            
        
        
            
                #{residentId,jdbcType=BIGINT},
            
            
                #{communityId,jdbcType=BIGINT},
            
            
                #{communityName,jdbcType=VARCHAR},
            
            
                #{orgId,jdbcType=BIGINT},
            
            
                #{orgName,jdbcType=VARCHAR},
            
            
                #{addressId,jdbcType=BIGINT},
            
            
                #{addressStructure,jdbcType=VARCHAR},
            
            
                #{createTime,jdbcType=TIMESTAMP},
            
            
                #{updateTime,jdbcType=TIMESTAMP},
            
            
                #{updateUserId,jdbcType=BIGINT},
            
            
                #{identityType,jdbcType=INTEGER},
            
            
                #{applyStatus,jdbcType=INTEGER},
            
            
                #{enable,jdbcType=INTEGER},
            
            
                #{version,jdbcType=INTEGER},
            
            
                #{comment,jdbcType=INTEGER},
            
            
                #{cardType,jdbcType=INTEGER},
            
            
                #{cardNum,jdbcType=VARCHAR},
            
            
                #{credentialsPhotoIds,jdbcType=VARCHAR},
            
            
                #{residentName,jdbcType=VARCHAR},
            
        
    

6.GROUP_CONCAT()函数

写项目的时候碰上个需求,要将一个关联关系表的值展示成一个字段。网上找了一下用这个函数就行

       SELECT a.*,b.extend_type FROM
        (
        SELECT DISTINCT t1.id,
        t1.real_name,
        t1.sex,
        t1.birthday,
        t1.card_num,
        t1.mobile,
        t1.`status`,
        t2.identity_type,
        t2.address_structure
        from t_cbo_resident t1
        INNER JOIN
        (
        SELECT * from t_cbo_location_apply
        
            and apply_status = 1
            and identity_type = #{identityType,jdbcType=INTEGER}
            and address_structure like concat("%",#{addressStructure,jdbcType=VARCHAR},"%") 
        
        LIMIT 1

        )t2 on t1.id = t2.resident_id) a
        Left JOIN (

            SELECT x.id,GROUP_CONCAT(y.extend_type) as extend_type  from t_cbo_resident x
            INNER JOIN t_cbo_resident_extend y on x.id = y.resident_id
            
                
                    and y.org_id = #{orgId,jdbcType=BIGINT}
                
            
            GROUP BY x.id
        ) b
        ON a.id = b.id
        
            and a.real_name like concat("%",#{realName,jdbcType=VARCHAR},"%")
            and a.card_num like concat("%",#{cardNum,jdbcType=VARCHAR},"%") 
            and a.mobile like concat("%",#{mobile,jdbcType=VARCHAR},"%") 
            and b.type like concat("%",#{extendType,jdbcType=VARCHAR},"%") 
        

    

注意这里使用了这个函数将关联关系表的值查询出来 按照居民的id分类,最后的结果当做一个表去和别的表join链接

SELECT x.id,GROUP_CONCAT(y.extend_type) as extend_type  from t_cbo_resident x
            INNER JOIN t_cbo_resident_extend y on x.id = y.resident_id
            
                
                    and y.org_id = #{orgId,jdbcType=BIGINT}
                
            
            GROUP BY x.id

 

你可能感兴趣的:(mybatis使用心得总结)