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