mybatis resultType和resultMap的区别和使用场景

1.resultType

从这条语句中返回的期望类型的类的完全限定名或别名。 注意如果返回的是集合,那应该设置为集合包含的类型,而不是集合本身。可以使用 resultType 或 resultMap,但不能同时使用。

例如:

1.student实体类

package com.generic.feng.modules.student.entity;

import java.util.List;

import org.hibernate.validator.constraints.Length;

import com.generic.feng.common.entity.DataEntity;
import com.generic.feng.modules.sys.entity.Office;


/**
 * 学生管理Entity
 * @author gsl
 * @version 2018-10-09
 */
public class Student extends DataEntity {
	
	private static final long serialVersionUID = 1L;
	private String studentName;		// 学生姓名
	private String studentNo;		// 学生编号
	private String phoneNumber;		// 手机号
	private Office office;		// 部门id
	private String officeId;		// 部门id
	private List list;
	private String roleId;
	
	
	
	public String getRoleId() {
		return roleId;
	}

	public void setRoleId(String roleId) {
		this.roleId = roleId;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getOfficeId() {
		return officeId;
	}

	public void setOfficeId(String officeId) {
		this.officeId = officeId;
	}

	public Student() {
		super();
	}

	public Student(String id){
		super(id);
	}

	@Length(min=0, max=255, message="学生姓名长度必须介于 0 和 255 之间")
	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}
	
	@Length(min=0, max=255, message="学生编号长度必须介于 0 和 255 之间")
	public String getStudentNo() {
		return studentNo;
	}

	public void setStudentNo(String studentNo) {
		this.studentNo = studentNo;
	}
	
	@Length(min=0, max=255, message="手机号长度必须介于 0 和 255 之间")
	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	
	public Office getOffice() {
		return office;
	}

	public void setOffice(Office office) {
		this.office = office;
	}
	
}

2.mapper.xml


    a.id AS "id",
	a.student_name AS "studentName",
	a.student_no AS "studentNo",
	a.phone_number AS "phoneNumber",
	a.office_id AS "office.id",
	a.create_by AS "createBy.id",
	a.create_date AS "createDate",
	a.update_by AS "updateBy.id",
	a.update_date AS "updateDate",
	a.remarks AS "remarks",
	a.del_flag AS "delFlag",
	o5.name AS "office.name"



	LEFT JOIN sys_office o5 ON o5.id = a.office_id

如果数据库的字段和POJO里面的属性不匹配,则可以使用别名来进行映射。如果两者一样就不需要设置别名。

2.resultMap

resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets 数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。实际上,在为一些比如连接的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的长达数千行的代码。ResultMap 的设计思想是,对于简单的语句根本不需要配置显式的结果映射,而对于复杂一点的语句只需要描述它们的关系就行了。

例子

1.teacher实体类

package com.ruoyi.system.domain;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.ruoyi.common.core.domain.BaseEntity;

/**
 * 老师基础表 sys_teacher
 * 
 * @author ruoyi
 * @date 2019-07-02
 */
public class Teacher extends BaseEntity
{
	private static final long serialVersionUID = 1L;
	
	/** 老师主键id */
	private String teacherId;
	/** 老师姓名 */
	private String teacherName;
	/** 老师年龄 */
	private Integer teacherAge;
	/** 删除标志(0代表存在 2代表删除) */
	private String delFlag;

	public void setTeacherId(String teacherId) 
	{
		this.teacherId = teacherId;
	}

	public String getTeacherId() 
	{
		return teacherId;
	}
	public void setTeacherName(String teacherName) 
	{
		this.teacherName = teacherName;
	}

	public String getTeacherName() 
	{
		return teacherName;
	}
	public void setTeacherAge(Integer teacherAge) 
	{
		this.teacherAge = teacherAge;
	}

	public Integer getTeacherAge() 
	{
		return teacherAge;
	}
	public void setDelFlag(String delFlag) 
	{
		this.delFlag = delFlag;
	}

	public String getDelFlag() 
	{
		return delFlag;
	}

    public String toString() {
        return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
            .append("teacherId", getTeacherId())
            .append("teacherName", getTeacherName())
            .append("teacherAge", getTeacherAge())
            .append("delFlag", getDelFlag())
            .append("createBy", getCreateBy())
            .append("createTime", getCreateTime())
            .append("updateBy", getUpdateBy())
            .append("updateTime", getUpdateTime())
            .append("remark", getRemark())
            .toString();
    }
}

2.mapper.xml


   
   
   
   
   
   
   
   
   

 

 

你可能感兴趣的:(Mybatis)