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" />

 

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

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

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

扩展: association 或 collection的 fetchType属性

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

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

发布了126 篇原创文章 · 获赞 6 · 访问量 3749

猜你喜欢

转载自blog.csdn.net/qq_40244391/article/details/102902919