DTO使用sql语句进行多表查询

DTO使用sql语句进行多表查询

在映射文件mapper中添加

 <select id="selectOrdersByOidDTO" resultType="com.qfedu.dto.OrderDTO">
        select u.name as username, u.phone, p.name as pname, p.price as pprice, d.count, t.name as tname, o.price as tprice
            from users u
            inner join orders o on o.uid = u.uid
            inner join details d on o.oid = d.oid
            inner join products p on d.pid = p.pid
            inner join types t on p.tid = t.tid
            where o.oid = '1'
    </select>

OrderDto

package com.qfedu.dto;

public class OrderDTO {

    private String username;
    private String phone;
    private String pname;
    private double pprice;
    private int count;
    private String tname;
    private double tprice;

    @Override
    public String toString() {
        return "OrderDTO{" +
                "username='" + username + '\'' +
                ", phone='" + phone + '\'' +
                ", pname='" + pname + '\'' +
                ", pprice=" + pprice +
                ", count=" + count +
                ", tname='" + tname + '\'' +
                ", tprice=" + tprice +
                '}';
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getPname() {
        return pname;
    }

    public void setPname(String pname) {
        this.pname = pname;
    }

    public double getPprice() {
        return pprice;
    }

    public void setPprice(double pprice) {
        this.pprice = pprice;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    public double getTprice() {
        return tprice;
    }

    public void setTprice(double tprice) {
        this.tprice = tprice;
    }
}

Test.java

 @Test
    public void testGetAllOrderByDTO(){
        List<OrderDTO> list = session.selectList("com.qfedu.pojo.OrderMapper.selectOrdersByOidDTO");

        for (OrderDTO o : list) {
            System.out.println(o);
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

友情链接更多精彩内容