MyBatis里ResultMap丶ResultType的区别与ParameterType详解

在使用Mybatis与数据库连接时sql语句返回通常有两种返回类型,一种是ResultType还有一种是ResultMap,下面我来说说我对两者的区别与理解:

两者都是查询才需要用到


ResultType

适用于单表查询后返回的结果,而里面填的就是项目中的实体类(pojo)的相对路径,例如:

	<select id="userInfoLogin" parameterType="com.jl.vo.UserInfoVo" resultType="com.jl.pojo.UserInfo">
        select *from userinfo where username=#{username} and password=#{password}
    </select>

com.jl.pojo.UserInfo是我userInfo实体类的地方,它返回的就是一个UserInfo对象

ResultMap

适用于多表查询后返回的结果,需要在mapper.xml中定义resultMap进行pojo和相应表字段的对应。而里面填的就是项目中的实体类(pojo)的相对路径,例如现在一个用户对应多个角色

用户表

package com.jl.pojo;

import java.io.Serializable;
import java.util.List;

public class UserInfo implements Serializable {
    private int userid;
    private String username;
    private String name;
    private String password;
    private String regEmail;
    private String mobile;
    private String icon;
    private List<RoleInfo>roleInfoList;

    public UserInfo() {
    }

    public UserInfo(int userid, String username, String name, String password, String regEmail, String mobile, String icon, List<RoleInfo> roleInfoList) {
        this.userid = userid;
        this.username = username;
        this.name = name;
        this.password = password;
        this.regEmail = regEmail;
        this.mobile = mobile;
        this.icon = icon;
        this.roleInfoList = roleInfoList;
    }

    public List<RoleInfo> getRoleInfoList() {
        return roleInfoList;
    }

    public void setRoleInfoList(List<RoleInfo> roleInfoList) {
        this.roleInfoList = roleInfoList;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRegEmail() {
        return regEmail;
    }

    public void setRegEmail(String regEmail) {
        this.regEmail = regEmail;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getIcon() {
        return icon;
    }

    public void setIcon(String icon) {
        this.icon = icon;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "userid=" + userid +
                ", username='" + username + '\'' +
                ", name='" + name + '\'' +
                ", password='" + password + '\'' +
                ", regEmail='" + regEmail + '\'' +
                ", mobile='" + mobile + '\'' +
                ", icon='" + icon + '\'' +
                ", roleInfoList=" + roleInfoList +
                '}';
    }
}

角色表

package com.jl.pojo;

import java.io.Serializable;
import java.util.List;


public class RoleInfo implements Serializable {
    private int roleId;
    private String roleName;
    private String shortName;
    public RoleInfo() {
    }

    public RoleInfo(int roleId, String roleName, String shortName) {
        this.roleId = roleId;
        this.roleName = roleName;
        this.shortName = shortName;
    }

    public int getRoleId() {
        return roleId;
    }

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

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public String getShortName() {
        return shortName;
    }

    public void setShortName(String shortName) {
        this.shortName = shortName;
    }

    @Override
    public String toString() {
        return "RoleInfo{" +
                "roleId=" + roleId +
                ", roleName='" + roleName + '\'' +
                ", shortName='" + shortName + '\'' +
                '}';
    }
}

userinfomapper.xml也要配置ResultMap才能用

<!-- 		id:实体类中的id,主键  
            column:数据库的字段或者说列
            property:实体类中的属性
            数据库的column与 实体类property要一一对应
            collection:用户实体类里的roleInfoList属性,集合,用于一对多  
          -->  
<resultMap id="userInfoMap" type="com.jl.pojo.UserInfo">
        <id column="USERID" property="userid"></id>
        <result column="USERNAME" property="username"></result>
        <result column="NAME" property="name"></result>
        <result column="PASSWORD" property="password"></result>
        <result column="REGEMAIL" property="regEmail"></result>
        <result column="MOBILE" property="mobile"></result>
        <result column="ICON" property="icon"></result>
        <collection property="roleInfoList" ofType="com.jl.pojo.RoleInfo">
            <id column="ROLEID" property="roleId"></id>
            <result column="ROLENAME" property="roleName"></result>
            <result column="SHORTNAME" property="shortName"></result>
        </collection>
    </resultMap>
//查询节点resultMap对应的填上面resultMap节点的id
<select id="UserInfoAndRoleInfo" resultMap="userInfoMap" parameterType="com.jl.vo.UserInfoVo">
        SELECT u.*,r.* FROM userinfo u,user_role ur,roleinfo r
        <where>
         u.USERID = ur.USERID AND r.ROLEID = ur.ROLEID
         <if test="userid!=null">
            and u.userid=#{userid}
         </if>
         <if test="username!=null and username!=''">
            and u.username=#{username}
         </if>
         <if test="name!=null and name!=''">
            and u.name=#{name}
         </if>
         <if test="regEmail!=null and regEmail!=''">
            and u.regEmail=#{regEmail}
         </if>
        </where>
    </select>

ParameterType

传入的参数类型,也是写实体类的相对路径,假设只需要传入一个是int型的id查询用户,只需要写int即可,传入的单个值什么类型就写什么类型

 <select id="findById" parameterType="int" resultMap="com.jl.pojo.UserInfo">
        select *from userinfo where id=#{id}
    </select>

如果要传入多个条件就要写对象实体类了

 <select id="findById" parameterType="com.jl.vo.UserInfoVo" resultMap="com.jl.pojo.UserInfo">
        select *from userinfo where id=#{id} and username=#{username}
    </select>

你可能感兴趣的:(Maven)