MyBatis 多表联合查询及优化

正文


下面就来说一下 mybatis 是通过什么来实现多表联合查询的。首先看一下表关系,如图:



这里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了简单期间,你可以不用搭前端的框架,只使用 Spring + MyBatis 就可以,外加 junit 测试即可。环境我就不带大家搭了,这里只说涉及到联合查询的操作。

设计好表之后,我用到了 mybatis 的自动生成工具 mybatis generator 生成的实体类、mapper 接口、以及 mapper xml 文件。由于是测试多表联合查询,因此需要自己稍加改动。

下面是 User 和 Role 的实体类代码:

User
  1. <span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.domain;  
  2.   
  3. import java.io.Serializable;  
  4. import java.util.List;  
  5.   
  6. public class User implements Serializable {  
  7.     private String id;  
  8.   
  9.     private String username;  
  10.   
  11.     private String password;  
  12.   
  13.     private List<Role> roles;  
  14.   
  15.     private static final long serialVersionUID = 1L;  
  16.   
  17.     public String getId() {  
  18.         return id;  
  19.     }  
  20.   
  21.     public void setId(String id) {  
  22.         this.id = id == null ? null : id.trim();  
  23.     }  
  24.   
  25.     public String getUsername() {  
  26.         return username;  
  27.     }  
  28.   
  29.     public void setUsername(String username) {  
  30.         this.username = username == null ? null : username.trim();  
  31.     }  
  32.   
  33.     public String getPassword() {  
  34.         return password;  
  35.     }  
  36.   
  37.     public void setPassword(String password) {  
  38.         this.password = password == null ? null : password.trim();  
  39.     }  
  40.   
  41.     public List<Role> getRoles() {  
  42.         return roles;  
  43.     }  
  44.   
  45.     public void setRoles(List<Role> roles) {  
  46.         this.roles = roles;  
  47.     }  
  48.   
  49.     @Override  
  50.     public boolean equals(Object that) {  
  51.         if (this == that) {  
  52.             return true;  
  53.         }  
  54.         if (that == null) {  
  55.             return false;  
  56.         }  
  57.         if (getClass() != that.getClass()) {  
  58.             return false;  
  59.         }  
  60.         User other = (User) that;  
  61.         return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))  
  62.             && (this.getUsername() == null ? other.getUsername() == null : this.getUsername().equals(other.getUsername()))  
  63.             && (this.getPassword() == null ? other.getPassword() == null : this.getPassword().equals(other.getPassword()));  
  64.     }  
  65.   
  66.     @Override  
  67.     public int hashCode() {  
  68.         final int prime = 31;  
  69.         int result = 1;  
  70.         result = prime * result + ((getId() == null) ? 0 : getId().hashCode());  
  71.         result = prime * result + ((getUsername() == null) ? 0 : getUsername().hashCode());  
  72.         result = prime * result + ((getPassword() == null) ? 0 : getPassword().hashCode());  
  73.         return result;  
  74.     }  
  75. }</span>  

Role
  1. <span style="font-family:Comic Sans MS;font-size:12px;">package com.sica.domain;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. public class Role implements Serializable {  
  6.     private String id;  
  7.   
  8.     private String name;  
  9.   
  10.     private String jsms;  
  11.   
  12.     private String bz;  
  13.   
  14.     private Integer jlzt;  
  15.   
  16.     private String glbm;  
  17.   
  18.     private String userid;  
  19.   
  20.     private static final long serialVersionUID = 1L;  
  21.   
  22.     public String getId() {  
  23.         return id;  
  24.     }  
  25.   
  26.     public void setId(String id) {  
  27.         this.id = id == null ? null : id.trim();  
  28.     }  
  29.   
  30.     public String getName() {  
  31.         return name;  
  32.     }  
  33.   
  34.     public void setName(String name) {  
  35.         this.name = name == null ? null : name.trim();  
  36.     }  
  37.   
  38.     public String getJsms() {  
  39.         return jsms;  
  40.     }  
  41.   
  42.     public void setJsms(String jsms) {  
  43.         this.jsms = jsms == null ? null : jsms.trim();  
  44.     }  
  45.   
  46.     public String getBz() {  
  47.         return bz;  
  48.     }  
  49.   
  50.     public void setBz(String bz) {  
  51.         this.bz = bz == null ? null : bz.trim();  
  52.     }  
  53.   
  54.     public Integer getJlzt() {  
  55.         return jlzt;  
  56.     }  
  57.   
  58.     public void setJlzt(Integer jlzt) {  
  59.         this.jlzt = jlzt;  
  60.     }  
  61.   
  62.     public String getGlbm() {  
  63.         return glbm;  
  64.     }  
  65.   
  66.     public 
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值