_mapperxml res">
当前位置:   article > 正文

Mybatis中mapper配置文件的resultMap标签中的子元素id、result、association、collection、discriminator的用法_mapperxml result id 标签有什么用

mapperxml result id 标签有什么用

关于resultMap标签相信大家都是很常用到的,但是如果是对于有相应单独实体类的就没有必要再新建这个标签了,下面是对resultMap下面的标签的用法及解释。

1.id

id的用法对应的是表中的主键字段和实体类中相应属性的关系,property对应的是实体类的属性,column对应的是数据库表的字段;

<resultMap type="com.hansong.entity.User" id="UserResult">
		<id property="id" column="id"/> 
		<result property="username" column="username"/> 
 		<result property="email" column="email"/>
 		<result property="sex" column="sex"/> 		           
 </resultMap>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.result

result的用法是指表中其他键值和实体类中字段的对应关系;

<resultMap type="com.hansong.entity.User" id="UserResult">
		<id property="id" column="id"/> 
		<result property="username" column="username"/> 
		<!-- 下列中"email"是实体类的属性,"u_email"是数据库表中的字段-->
 		<result property="email" column="u_email"/>
 		<result property="sex" column="sex"/> 		           
 </resultMap>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

3.association

association 是将复杂类型的结果封装成其他实体类赋值给当前实体类的属性,当前实体的属性和select的返回值是同一类型的。

<resultMap type="com.hansong.entity.User" id="UserResult">
		<id property="id" column="id"/> 
		<result property="username" column="username"/> 		
 		<result property="email" column="u_email"/>
 		<result property="sex" column="sex"/> 		
 		<!-- 下列中"unifiedUser"是实体类的属性,而该属性对应的实体类为UnifiedUser,"select"是引用getUnifiedUserById
这个查询方法,获取其返回值对象,赋值类当前实体类的属性unifiedUser,不仅是可以当前命名空间的mapper,也可以引用其他命名空
间的mapper,例如:select="com.hansong.dao.getUserById"来查询User-->
 		<association property="unifiedUser" column="id" select="getUnifiedUserById"/>              
 </resultMap>

 <select id="getUnifiedUserById" parameterType="Long" resultType="com.hansong.entity.UnifiedUser">
        select * from unifieduser where id =#{id}
    </select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

4.collection

collection是将更复杂的类型包装之后复制给当前实体类的属性,其中ofType是指返回值中的集合的对象实体为Role,javaType是指当前实体类属性为ArrayList集合,select用法和association一样

<resultMap type="com.hansong.entity.User" id="UserResult">
		<id property="id" column="id"/> 
		<result property="username" column="username"/> 
 		<result property="email" column="u_email"/>
 		<result property="sex" column="sex"/> 		      
 		<collection property="roles"  ofType="com.hansong.entity.Role" javaType="ArrayList"  column="id"                       
            select="com..hansong.RoleDao.findByUserId">
        </collection>      
 </resultMap>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

对应实体User

public class User implements Serializable {
	
	private Long id;
	private String username;
	private String email;
	private String sex;
	private List<Role> roles;  
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

5.discriminator

discriminator其实优点类似switch语句,就是将discriminator 里面的column属性进行区分,当case相应值时执行case内部操作,javaType为column中键值对应实体属性的类型为String

<resultMap type="com.hansong.entity.User" id="UserResult">
		<id property="id" column="id"/> 
		<result property="username" column="username"/> 
 		<result property="email" column="u_email"/>
 		<result property="sex" column="sex"/> 	
 		<!--例如性别是以0--男 和 1--女 来区分-->	      
 	    <discriminator javaType="String" column="sex">		
 	    	<!--当性别为男时执行case 0里面的操作-->	      
			<case value="0" resultType="...">
					<!--执行操作-->	      
			</case>		
			<!--当性别为女时执行case 1里面的操作-->	      	
			<case value="1" resultType="...">
					<!--执行操作-->	 
			</case>
 </resultMap>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

以上就是在下的学习心得,如有不妥之处,帮忙指出下,谢谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/241553
推荐阅读
相关标签