• 18 MyBatis——多表查询


    引入

    多表查询我们就不能使用实体类来接收结果,而使用VO(Value Object)类来接收。他们在结构形式上是一致的,但是定位不同,VO类专用于多表查询结果的封装。

    案例:t_user表与t_group表的查询

    t_user表结构

     

    t_group表结构

    VO类的设计

    多表查询结果通常使用一个VO类来存储数据。

    我们现在要实现查询结果附带所在的组名称,于是要在原来的bean基础上添加groupName属性。

    public class UserVO {
    	private Integer id;
    	private String username;
    	private String password;
    	private Integer age;
    	private String phone;
    	private String email;
    	private Integer groupId;
    	private String groupName;
            //setter/getter
            //toString() hashCode() equals()      
    }
    

      

    mapper查询示例

    <select id="findVOById" resultType="cn.tedu.mybatis.UserVO">
    	    select t_user.id, username, password, age, phone, email, group_id AS groupId, name AS groupName
    		from t_user 
    		join t_group 
    		on t_user.group_id=t_group.id 
    		where t_user.id = #{id}; 
    </select>
    

      

    UserMapper.xml中添加查询方法

    UserVO findVOById(Integer id);
    

      

  • 相关阅读:
    枚举
    泛型
    装箱和拆箱
    使用TryParse()来执行数值转换
    参数数组
    checked和unchecked转换
    字符串不可变
    TCC : Tiny C Compiler (2018-2-6)
    win10 下 protobuf 与 qt
    QWebView 与Js 交互
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/12484974.html
Copyright © 2020-2023  润新知