layui表格显示后台的多表的级联查询(多对多,多对一)带mybatis级联查询源码,已解决

更新时间:2023-06-29 15:01:25 阅读: 评论:0

layui表格显⽰后台的多表的级联查询(多对多,多对⼀)带mybatis级联查询源
码,已解决
在ssm和springboot项⽬中我们存在表与表之间⼀对多和多对多的情况,那么他们就要进⾏级联查询查询出相关联的数据,级联查询涉及到的问题就是在⼀个实体类中存在另⼀个实体类的对象(⼀对⼀)或者对象集合(⼀对多),在利⽤layui时我们会把查询的数据分页并返回给前端,layui⾃动分页,我们常常使⽤的都是返回⼀个实体类的数据,⽽遇到级联查询可能不知道应该怎样让数据传到前端表格⾃动渲染出来,话不多说上正题。
⾸先看下我的实体类。
Admin.java(Admin内有BuyTotal的集合,⼀对多)
package com.zlx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id;
import javax.persistence.GeneratedValue;
import java.util.List;
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
//管理员账号类
public class Admin {
@Id
@GeneratedValue
private Integer adminid;
private String urname;
private String password;
private String identify;
//⼀个收银台有多条记录
private List<BuyTotal> buyTotalList;
}
BuyTotal.java(内有Ur对象,⼀对⼀,还有BuyRecord集合,⼀对多)
import org.springframework.data.annotation.Id;
import javax.persistence.GeneratedValue; import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BuyTotal {
发展体育运动增强人民体质是谁说的@Id
@GeneratedValue
private int totalid;
private int uid;
private double salesMoney;
private double payMoney;
private double changeMoney;
private int adminid;
//⼀条总记录⾥有多条详细记录
跳舞的英文private List<BuyRecord> buyRecordList;
//⼀条总记录⾥对应着⼀个⽤户;
private Ur ur;
}
Ur.java
package com.zlx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.data.annotation.Id; import javax.persistence.GeneratedValue;
@Data
when can i e you again
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Ur {
@Id
@GeneratedValue
private Integer uid;
private String idCard;
private String integral;
}
BuyRecord.java
@AllArgsConstructor
@Data
@NoArgsConstructor
public class BuyRecord {
private int totalid;
private int gid;
private int buyAmount;
//⼀条详细记录记录着⼀个商品
private Goods goods;
}
Goods.java
package com.zlx.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Goods {
private Integer gid;
private String gNo;
private String gName;
private String price;
private String isBargainPrice;
朱莉和茱莉亚
private String bargainPrice;
初二英语日记private String unit;
private String stock;
}
下⾯是mybatis中的级联查询源码,通过级联查询把好⼏张表联合起来查询数据
<lect id="queryAllRecordByCondition"parameterType="map"resultMap="AdminMapping">
alid AS buy_tid,idcard,gname,price,isbargainprice,bargainPrice,unit,buyAmount,salesMoney,payMoney,changeMoney,integral,us ername
FROM ADMIN ,buyrecord,buytotal,goods,USER WHERE
admin.adminid=buytotal.adminid AND ur.uid=buytotal.uid
alid AND goods.gid = buyrecord.gid
<if test="urname!=null and urname!=''">
精度英文AND urname like concat('%',#{urname},'%')
</if>
<if test="idCard!=null and idCard!=''">
and idCard like concat('%',#{idCard},'%')
</if>
</lect>
<resultMap id="AdminMapping"type="com.zlx.pojo.Admin">
<id column="adminid"property="adminid"></id>
<result column="urname"property="urname"></result>
<collection property="buyTotalList"ofType="com.zlx.pojo.BuyTotal"resultMap="buyTotalMapping"></collection>
</resultMap>
<resultMap id="buyTotalMapping"type="com.zlx.pojo.BuyTotal">
<id column="buy_tid"property="totalid"></id>
<result column="salesMoney"property="salesMoney"></result>
andida
<result column="payMoney"property="payMoney"></result>
<result column="changeMoney"property="changeMoney"></result>
<association property="ur"javaType="com.zlx.pojo.Ur"resultMap="UrMapping"></association>
<collection property="buyRecordList"ofType="com.zlx.pojo.BuyRecord"resultMap="BuyRecordMapping"></collection>
</resultMap>
<resultMap id="UrMapping"type="com.zlx.pojo.Ur">
<result column="idCard"property="idCard"></result>
<result column="integral"property="integral"></result>
</resultMap>
<resultMap id="BuyRecordMapping"type="com.zlx.pojo.BuyRecord">
<result column="buyAmount"property="buyAmount"></result>
新福尔摩斯<association property="goods"javaType="com.zlx.pojo.Goods"resultMap="GoodsMapping"></association>
</resultMap>
<resultMap id="GoodsMapping"type="com.zlx.pojo.Goods">
<result column="gName"property="gName"></result>
<result column="price"property="price"></result>
<result column="isBargainPrice"property="isBargainPrice"></result>
<result column="bargainPrice"property="bargainPrice"></result>
<result column="unit"property="unit"></result>
</resultMap>
查出来的数据最终通过controller分页后传到前端,能否把数据渲染到layui表格的最重要的⼀步就是设置好layui的cols,只有⼀个实体类很简单,直接是实体类的属性写在那就可以,⽽级联则存在对象1.对象2.属性的情况,如下代码
cols:
[[
{
field: 'buyTotalList', templet: function (data) {
return data.buyTotalList[0].totalid;
}, title: '单号', width: 120, sort: true, align: "center"
},
{
field: 'ur', templet: function (data) {
return data.buyTotalList[0].ur.idCard;
}, title: '会员卡号', width: 100, align: "center"
},
{
field: 'goods', templet: function (data) {
return data.buyTotalList[0].buyRecordList[0].ame;
}, title: '商品名', width: 150, align: "center"
},
{
field: 'goods', templet: function (data) {
field: 'goods', templet: function (data) {
return data.buyTotalList[0].buyRecordList[0].goods.price;
}, title: '原价', width: 80, sort: true, align: "center"
},
{
field: 'goods', templet: function (data) {
return data.buyTotalList[0].buyRecordList[0].goods.isBargainPrice;
}, title: '是否特价', width: 120, sort: true, align: "center"
},
{
field: 'goods', templet: function (data) {
return data.buyTotalList[0].buyRecordList[0].goods.bargainPrice;
}, title: '特价价格', width: 120, sort: true, align: "center"
},
{
field: 'goods', templet: function (data) {
return data.buyTotalList[0].buyRecordList[0].goods.unit;
}, title: '单位', width: 80, align: "center"
},
{
field: 'buyRecordList', templet: function (data) {
return data.buyTotalList[0].buyRecordList[0].buyAmount;
}, title: '购买数量', width: 120, align: "center"
},
{
field: 'buyTotalList', templet: function (data) {
return data.buyTotalList[0].salesMoney;
}, title: '本单⾦额', width: 120, align: "center"
},
{
bombingfield: 'buyTotalList', templet: function (data) {
return data.buyTotalList[0].payMoney;
}, title: '实付⾦额', width: 120, align: "center"
},
{
field: 'buyTotalList', templet: function (data) {
return data.buyTotalList[0].changeMoney;
wind s}, title: '找零', width: 80, align: "center"
},
{
field: 'ur', templet: function (data) {
return data.buyTotalList[0].ur.integral;
}, title: '积分', width: 80, sort: true, align: "center"
},
{field: 'urname', title: '收银台号', width: 120, align: "center"},
,
{title: '操作',width:80, toolbar: '#currentTableBar', align: "center"}
]],
可以看出在cols的field写的不是字段名了,⽽是随后⼀个对象名,
例如对象1.对象2.对象3.属性,则field写的是对象3,映射的数据则是
对象3.属性所对应的属性数据。也可以看出是需要加⼀个函数来返回数据的。
在最后说⼀个layui可能会发⽣的⼀个坑,我⾃⼰遇到过,有时候在cols的field中写的是后端实体类的属性名,但是显⽰不出来数据,或者所只有部分列显⽰出来,那么你应该直接访问传数据过来的controller层,观察它返回给前端的数据的属性名,属性名可能被修改(我⾃⼰的就是属性名的⾸字母被从⼤写改成了⼩写~~)

本文发布于:2023-06-29 15:01:25,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/78/1067165.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   对象   查询   级联   属性
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图