zoukankan      html  css  js  c++  java
  • 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);
    

      

  • 相关阅读:
    php——验证身份证是否合法的函数
    php——离线执行任务
    代码整洁之道
    js自适应屏幕高度
    SSH Junit4测试
    Java Persistence with Hibernate
    SSH搭建
    js整理
    Hibernate 应用
    对学习的一点感想
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/12484974.html
Copyright © 2011-2022 走看看