MyBatista的自定义映射

  1. 自定义resultMap,实现高级结果集映射
  2. id :用于完成主键值的映射
  3. result :用于完成普通列的映射
  4. association :一个复杂的类型关联;许多结果将包成这种类型
  5. collection : 复杂类型的集

 

开发环境下不用 *

association  联合查询时使用

在这种情况下,虽然两个表之间有主外键的关系,但是在实际的bean中,其中一个是以类对象的形式存在。

<resultMap type="User" id="selectUserByIdMap">
  <id column="id" property="id"></id>
  <result column="username" property="username"/>
  <result column="password" property="password"/>
  <result column="email" property="email"/>
       <association property="dept" javaType="Dept" >
           <id column="did" property="did"/>
           <result column="deptname" property="deptname"/>
       </association>

</resultMap>


<select id="selectUserById" resultMap="selectUserByIdMap">

     select * from user u,dept d where u.did=d.did and u.id=#{id} 
   
</select>
package com.thekingqj.bean;

public class User {
     private String username;
     private String password;
     private String email;
     private Integer id;
     private Dept dept;
	public User() {
		super();
		// TODO Auto-generated constructor stub
	}
	public User(Integer id,String username, String password, String email) {
		super();
		this.username = username;
		this.password = password;
		this.email = email;
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	
	public Dept getDept() {
		return dept;
	}
	public void setDept(Dept dept) {
		this.dept = dept;
	}
	@Override
	public String toString() {
		return "User [username=" + username + ", password=" + password + ", email=" + email + ", id=" + id + ", dept="
				+ dept + "]";
	}
	
     
}

association 分步查询(需要配置默认是没有的)

 先通过员工的id查询员工信息     再通过查询出来的员工信息中的外键(部门id)查询对应的部门信息.

 用户表:

<resultMap type="User" id="selectUserByIdMap1">
	   <id column="id" property="id"></id>
	   <result column="username" property="username"/>
	   <result column="password" property="password"/>
	   <result column="email" property="email"/>
	   <!-- select标签 -->
	    <association property="dept" select="com.thekingqj.dao.DeptMapper.getDeptById" column="did"></association> 
	
	</resultMap>
	
	<select id="selectUserById1" resultMap="selectUserByIdMap1">
	    select * from user where id = #{id}
	 
	</select>

 部门表:

  
	    <select id="getDeptById" resultType="Dept">
	        select * from dept where did = #{did}  
	    </select>
	    
	    

MyBatis-config.xml文件:

<!-- 开启延迟加载 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 设置加载的数据是按需还是全部 -->
		<setting name="aggressiveLazyLoading" value="false" />

自关联查询:

<resultMap id="nestedSubject" type="com.atguigu.guli.service.edu.entity.vo.SubjectVo">
        <id property="id" column="id" />
        <result property="title" column="title" />
        <result property="sort" column="sort" />
        <collection property="children"
                    column="id"
                    ofType="com.atguigu.guli.service.edu.entity.vo.SubjectVo"
                    select="selectNestedListByParentId" />
    </resultMap>

    <select id="selectNestedListByParentId" resultMap="nestedSubject">
        SELECT * FROM edu_subject WHERE parent_id = #{parentId}
    </select>

扩展: 分步查询多列值的传递

如果分步查询时,需要传递给调用的查询中多个参数,则需要将多个参数封装成Map来进行传递,语法如下: {k1=v1, k2=v2....}

在所调用的查询方,取值时就要参考Map的取值方式,需要严格的按照封装map时所用的key来取值.

扩展: association 或 collection的 fetchType属性

在<association> 和<collection>标签中都可以设置fetchType,指定本次查询是否要使用延迟加载。默认为 fetchType=”lazy” ,如果本次的查询不想使用延迟加载,则可设置为     fetchType=”eager”.

fetchType可以灵活的设置查询是否需要使用延迟加载,而不需要因为某个查询不想使用延迟加载将全局的延迟加载设置关闭.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值