mybatis查询基本返回数据基本用到的都是resulttype与resultmap,resulttype必须po类与sql字段一致,而resultmap可以不一致通过配置映射。
本篇文章一是要讲解resulttype与resultmap如何使用;
二是重点讲解一对一与一对多查询resultmap的使用方便之处,举例:在查询每个班级有多少学生,一般会查出很多条,
班级1,学生1;
班级1,学生2;
班级2,学生3;
班级2,学生4;
而我们想要返回的数据格式是
班级1:学生1,学生2;
班级2:学生3,学生4,
这个直接可以在resultmap中配置实现而不用在service里面再加工。
base表:主查询表
onetoone表:一对一查询的表,code与base表fcode关联
onetomany表:一对多查询的表,code与base表fcode关联
Base类:对应基表的po类
public class Base {
private String fcode;
private String fname;
private String fzrzt;
private String fssfqy;
private String fssfqybh;
private String fssqy;
private String fssqybh;
private String fsshy;
private String fhydh;
private String flssq;
private String fjtsq;
private String fjzms;
private String flyqk;
private String flyqkbz1;
private String fbz;
private String fbz2;
public String getFcode() {
return fcode;
}
public void setFcode(String fcode) {
this.fcode = fcode == null ? null : fcode.trim();
}
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname == null ? null : fname.trim();
}
public String getFzrzt() {
return fzrzt;
}
public void setFzrzt(String fzrzt) {
this.fzrzt = fzrzt == null ? null : fzrzt.trim();
}
public String getFssfqy() {
return fssfqy;
}
public void setFssfqy(String fssfqy) {
this.fssfqy = fssfqy == null ? null : fssfqy.trim();
}
public String getFssfqybh() {
return fssfqybh;
}
public void setFssfqybh(String fssfqybh) {
this.fssfqybh = fssfqybh == null ? null : fssfqybh.trim();
}
public String getFssqy() {
return fssqy;
}
public void setFssqy(String fssqy) {
this.fssqy = fssqy == null ? null : fssqy.trim();
}
public String getFssqybh() {
return fssqybh;
}
public void setFssqybh(String fssqybh) {
this.fssqybh = fssqybh == null ? null : fssqybh.trim();
}
public String getFsshy() {
return fsshy;
}
public void setFsshy(String fsshy) {
this.fsshy = fsshy == null ? null : fsshy.trim();
}
public String getFhydh() {
return fhydh;
}
public void setFhydh(String fhydh) {
this.fhydh = fhydh == null ? null : fhydh.trim();
}
public String getFlssq() {
return flssq;
}
public void setFlssq(String flssq) {
this.flssq = flssq == null ? null : flssq.trim();
}
public String getFjtsq() {
return fjtsq;
}
public void setFjtsq(String fjtsq) {
this.fjtsq = fjtsq == null ? null : fjtsq.trim();
}
public String getFjzms() {
return fjzms;
}
public void setFjzms(String fjzms) {
this.fjzms = fjzms == null ? null : fjzms.trim();
}
public String getFlyqk() {
return flyqk;
}
public void setFlyqk(String flyqk) {
this.flyqk = flyqk == null ? null : flyqk.trim();
}
public String getFlyqkbz1() {
return flyqkbz1;
}
public void setFlyqkbz1(String flyqkbz1) {
this.flyqkbz1 = flyqkbz1 == null ? null : flyqkbz1.trim();
}
public String getFbz() {
return fbz;
}
public void setFbz(String fbz) {
this.fbz = fbz == null ? null : fbz.trim();
}
public String getFbz2() {
return fbz2;
}
public void setFbz2(String fbz2) {
this.fbz2 = fbz2 == null ? null : fbz2.trim();
}
}
public class BaseCustom1 extends Base {
private String code;
private String name;
private String info;
private Onetoone onetoone;
private List listOnetomany;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
public Onetoone getOnetoone() {
return onetoone;
}
public void setOnetoone(Onetoone onetoone) {
this.onetoone = onetoone;
}
public List getListOnetomany() {
return listOnetomany;
}
public void setListOnetomany(List listOnetomany) {
this.listOnetomany = listOnetomany;
}
}
public class Onetoone {
private String code;
private String info;
private String name;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code == null ? null : code.trim();
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info == null ? null : info.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
}
public class Onetomany {
private String code;
private String name;
private String info;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code == null ? null : code.trim();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info == null ? null : info.trim();
}
}
TestMapper接口:resulttype方法使用resulttype接收一对一,resultmap方法使用resultmap接收一对一,resultmap2方法使用resultmap接收一对多
public interface TestMapper {
List resulttype ();
List resultmap ();
List resultmap2 ();
}
TestMapper配置xml:resultmap一对一对应关系要使用association,resultmap一对多对应关系使用collection,具体对应关系自寻琢磨或者百度
这个比较简单,直接调用mapper接口就行
public Object resulttype() {
testMapper.resulttype();
return testMapper.resulttype();
}
public Object resultmap() {
return testMapper.resultmap();
}
public Object resultmap2() {
return testMapper.resultmap2();
}
resulttype:
resultmap:
resultmap2: