JPA的三级联动的实现(包含了一个添加)

主表的实体类,有多对多的关系,,以及三级联动


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

@Entity
@Data
@Table(name = "t_shop")
public class Shop {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    private String name;

    private String pic;

    private double price;

    private int province;
    private int city;
    private int county;

    @Temporal(TemporalType.DATE)
    private Date created;

    @OneToOne
    @JoinColumn(name = "province",insertable = false,updatable = false,foreignKey = @ForeignKey(name = "none"))
    private Area provinceName;
    @OneToOne
    @JoinColumn(name = "city",insertable = false,updatable = false,foreignKey = @ForeignKey(name = "none"))
    private Area cityName;
    @OneToOne
    @JoinColumn(name = "county",insertable = false,updatable = false,foreignKey = @ForeignKey(name = "none"))
    private Area countyName;


    @ManyToMany
    @JoinTable(name = "t_shop_chuan",joinColumns = @JoinColumn(name = "shopid",foreignKey = @ForeignKey(name = "none")),
            inverseJoinColumns = @JoinColumn(name = "chuanid",foreignKey = @ForeignKey(name = "none")))
    private List<Chuan> chuanList;

    @Transient
    private Integer start;

    @Transient
    private Integer end;





}
**

## 地区表的实体类的实现

**

import lombok.Data;

import javax.persistence.*;
import java.io.Serializable;
import java.util.List;

@Data
@Entity
@Table(name = "t_area")
public class Area implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
    @Column(length = 50)
    private String name;
    private int parent;

    @OneToMany(cascade = {CascadeType.REFRESH,CascadeType.MERGE})
    @JoinColumn(name = "parent",updatable = false,insertable = false,foreignKey = @ForeignKey(name = "none"))
    private List<Area> list;
}

**

## 后台获取的三级联动的数据传到前台

**

    @RequestMapping("list1")
    public List<Area> findDistrict(){
        Specification specification= new Specification() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder cb) {
                ArrayList<Predicate> list = new ArrayList<>();
                Predicate parent = cb.equal(root.get("parent"), 0);
                list.add(parent);
                return cb.and(list.toArray(new Predicate[list.size()]));
            }
        };
        List<Area> all = areaDao.findAll(specification);
        for(Area list:all){
            for(Area li:list.getList()){
                for(Area l:li.getList()){
                    if(l.getList().size()==0){
                        l.setList(null);
                    }
                }
            }
        }

        return all;
    }
		
		**

## 前台获取后台的数据

**

			initCascader() {
				this.axios.get('http://localhost:8100/api/shop/list1').then((res) => {
					console.log(res.data);
					this.nations = res.data;
				})
			},
		**

## 列表中的显示

**
			<!--省市县-->
			<el-table-column label="产地">
				<template slot-scope="scope">
					{{scope.row.provinceName.name}}-{{scope.row.cityName.name}}-{{scope.row.countyName.name}}
				</template>
			</el-table-column>

**

## 添加的时候

**

				<el-form-item label="产地">
					<el-cascader v-model="value" :options="nations" :props="props" @change="flushpc">
					</el-cascader>
				</el-form-item><br />

**

## 添加的时候触发的点击方法

**

flushpc(value) {
				//判断是否是null ,是否是数组
				if (value != null && Array.isArray(value)) {
					if (this.roleDialog) {
						this.insertFormModel.province = this.value[0];
						this.insertFormModel.city = this.value[1];
						this.insertFormModel.county = this.value[2];
					}
					 
					}
				},

**

## return里面所包含的数据

**

				returen:{
						props: {
									value: 'id',
									label: 'name',
									children: 'list',
						},
						insertFormModel: {
									name: '',
									pic: '',
									price: '',
									province: '',
									city: '',
									county: '',
					},
				}
**

## 这个添加的方法前面的这个cid是获取被选中的值,,后面的方法是往后台传送数据的方法

**


			addsaveRole(){
				let cid = [];
				for (let i in this.checkedShop) {
					cid.push(this.checkedShop[i])
				}
				console.log("11111111111")
				console.log(this.insertFormModel)
				console.log(cid.join())
				this.axios.post("http://localhost:8100/api/shop/add", this.insertFormModel, {
					params: {
						
						cid: cid.join(),
					}
				}).then((res) => {
					alert("添加成功");
					this.roleDialog = false;
					this.initData();
				})
				
			},














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值