栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

MyBatis的各种动态sql写法以及各种动态sql所需使用的标签

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

MyBatis的各种动态sql写法以及各种动态sql所需使用的标签

MyBatis的各种动态sql写法

文章目录
    • MyBatis的各种动态sql写法
    • 1、各种动态sql所需使用的标签
      • 1.foreach 标签
      • 2.where标签
      • 3. sql标签
      • 4.trim标签
    • 2、批量添加、更新、删除
    • 3、给一个类起别名

1、各种动态sql所需使用的标签 1.foreach 标签

首先在mapper中接收到的方法参数应该是list、map或者array类型的。

使用方法如下


	#{item}





2.where标签



	select * from user
	
		
			username = #{username}
		
		
			and sex = #{sex}
		
	

3. sql标签

Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的

使用方法如下

   
        
            userName,
            age,
            sex,
            salary,
            completed,
            remark
        
    
    
        
            #{item.userName},
            #{item.age},
            #{item.sex},
            #{item.salary},
            #{item.completed},
            #{item.remark}
        
    

    
    
	
	        insert into UserInfo(userName,age,sex,salary,completed,remark) VALUES
		
		      ()
		
	

    
	
	        
	            insert into UserInfo()
	            VALUES()
	        
	

4.trim标签





        insert into user
        
            
                id,
            
            
                userId,
            
            
                username,
            
        
        
            
                #{id},
            
            
                #{userId},
            
            
                #{username},
            
        
    
2、批量添加、更新、删除

	详细语法 请看上文sql标签的使用!




        update UserInfo
        set age=
        
            when userInfoId=#{item.userInfoId} then #{item.age}
        
        where userInfoId in
        
            #{item.userInfoId}
        
  



 
        delete from UserInfo where userInfoId
        in
        
          #{item}
        
 



    update UserInfo
    
        
            
                 when userInfoId=#{item.userInfoId} then #{item.userName}
            
        

        
            
                when userInfoId=#{item.userInfoId} then #{item.age}
            
        

        
            
                when userInfoId=#{item.userInfoId} then #{item.sex}
            
        

        
            
                when userInfoId=#{item.userInfoId} then #{item.salary}
            
        

        
            
                when userInfoId=#{item.userInfoId} then #{item.completed}
            
        

        
            
                when userInfoId=#{item.userInfoId} then #{item.remark}
            
        
    
    where userInfoId in
    
        #{item.userInfoId}
    


3、给一个类起别名

		
		

注: 目前能想到的关于动态sql的标签和相关知识点只有上面那么多,欢迎大家如果有知道新的标签,可以在下方留言,我会进行采纳整理的,大家一起来整理相关知识,我相信每一个人都会学到很多,感谢啦!

   欢迎大家点赞收藏,一起学技术
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/891473.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号