在使用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