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);
    

      

  • 相关阅读:
    使用git bash提交代码到github托管
    eclipse中将java项目变成web项目
    程序员的兼职网站
    python 文件内容修改替换操作
    简单介绍下python中函数的基础语法
    python lambda简单介绍
    微信小程序 --01
    微信小程序开发 --02
    弹性盒模型flex
    HTML基础
  • 原文地址:https://www.cnblogs.com/Scorpicat/p/12484974.html
Copyright © 2011-2022 走看看