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

Mybatis动态SQL及关联查询实例

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

Mybatis动态SQL及关联查询实例

Mybatis动态SQL及关联查询实例
    • 动态SQL
    • 关联查询
        • 使用业务扩展类实现一对一:
        • 使用 resultMap 实现一对一:
        • 使用 resultMap 实现一对多:

动态SQL

实现:select stuno,stuname from student where stuname = #{stuName}and stuage = #{stuAge}

第一种写法:

	
		select stuno,stuname,stuage from student where 1=1
		
		 
			and stuname = #{stuName}
		
		 
			 and  stuage = #{stuAge}
		
	

第二种写法:会自动处理第一个标签中的 and,但不会处理之后中的and

	
	  	select * from student 
	  	
	  		 
	  		 	   
	  		 		#{stuNo}
	  		 	
	  		 
	  	
	
	
	
	  	select * from student 
	  	
	  		 
	  		 	   
	  		 		#{student.stuNo}
	  		 	
	  		 
	  	
	

提取相同相似SQL片段:


		
	  		 
	  		 	   
	  		 		#{student.stuNo}
	  		 	
	  		 
	  	
	
	
	
		select s.*,c.* from student s  inner join studentcard c
		on s.cardid=c.cardid
		where s.stuno = #{stuNo}
	
@Data
public class StudentBusiness  extends Student{//学生业务扩展类
	private int cardId;
	private String cardInfo ;
}
使用 resultMap 实现一对一:
	
		select  c.*,s.* from student s
		inner join studentclass c 
		on c.classid = s.classid
		where c.classid = #{classId}
	
	  
	
			
			
			
			
			
				
				
				
			
	
public class StudentClass {
	private int classId;//外键
	private String className;
	//增加学生属性 (通过该字段,让Student类和StudentClass类建立起关联)
	List students ;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/585339.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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