前端:
import { get,post,qspost,put,deletefn } from '../../utils/axios/axios' export const getBrandList = data => post("/item/brand/list",data) export const addBrand = data =>post("/item/brand",data); export const deleteBrandById = data =>deletefn(`/item/brand/${data.id}`,data); export const updateBrandById = data =>put(`/item/brand/${data.id}`,data);
<template>
<el-card class="box-card">
<!--面包屑-->
<el-breadcrumb separator-class="el-icon-arrow-right">
<el-breadcrumb-item>商品管理</el-breadcrumb-item>
<el-breadcrumb-item>品牌管理</el-breadcrumb-item>
</el-breadcrumb>
<el-row style="margin-top: 20px">
<el-col :span="12">
<el-button type="primary" @click="addDialogForm()">新增</el-button>
</el-col>
<el-col :span="12" >
<el-row>
<el-col :span="12"><el-input class="input" v-model="search" placeholder="请输入名称" clearable @clear="clearInput()"></el-input></el-col>
<el-col :span="12"> <el-button type="primary" plain @click="searchName()" >搜索</el-button></el-col>
</el-row>
</el-col>
</el-row>
<el-table
:data="tableData"
style="width: 100%"
height="500px">
<el-table-column
type="index"
label="序号"
width="180">
<template slot-scope="scope">
<span>{{(pageNum - 1) * pageSize + scope.$index + 1}}</span>
</template>
</el-table-column>
<el-table-column
prop="id"
label="id"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="名称"
width="180">
</el-table-column>
<el-table-column
prop="image"
label="Logo"
width="180">
</el-table-column>
<el-table-column
prop="letter"
label="首字母"
width="180">
</el-table-column>
<el-table-column
prop="letter"
label="首字母"
width="180">
<template slot-scope="scope">
<el-button
size="mini"
@click="handleEdit(scope.$index, scope.row)">编辑
</el-button>
<el-button
size="mini"
type="danger"
@click="handleDelete(scope.$index, scope.row)">删除
</el-button>
</template>
</el-table-column>
</el-table>
<!--分页条-->
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="pageNum"
:page-sizes="[50, 100, 150, 200]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="total">
</el-pagination>
<!--新增对话框-->
<el-dialog title="新增" :visible.sync="AddDialogFormVisible">
<el-form :model="addForm">
<el-form-item label="名称" label-width="120px">
<el-input v-model="addForm.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="Logo" label-width="120px">
<!--<el-input v-model="addForm.image" autocomplete="off"></el-input>-->
<el-upload
action="https://jsonplaceholder.typicode.com/posts/"
list-type="picture-card"
:on-preview="handleImageCardPreview"
:on-remove="handleImageRemove">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="ImageDialogVisible">
<img width="100%" :src="ImageDialogImageUrl" alt="">
</el-dialog>
</el-form-item>
<el-form-item label="首字母" label-width="120px">
<el-input v-model="addForm.letter" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="AddDialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="submitAddForm">确 定</el-button>
</div>
</el-dialog>
<!--编辑对话框-->
<el-dialog title="编辑" :visible.sync="EditDialogFormVisible">
<el-form :model="editForm">
<el-form-item label="名称" label-width="120px">
<el-input v-model="editForm.name" autocomplete="off"></el-input>
</el-form-item>
<el-form-item label="Logo" label-width="120px">
<!--<el-input v-model="addForm.image" autocomplete="off"></el-input>-->
<el-upload
action="https://jsonplaceholder.typicode.com/posts/"
list-type="picture-card"
:on-preview="handleImageCardPreview"
:on-remove="handleImageRemove">
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="ImageDialogVisible">
<img width="100%" :src="ImageDialogImageUrl" alt="">
</el-dialog>
</el-form-item>
<el-form-item label="首字母" label-width="120px">
<el-input v-model="editForm.letter" autocomplete="off"></el-input>
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="EditDialogFormVisible = false">取 消</el-button>
<el-button type="primary" @click="submitEditForm">确 定</el-button>
</div>
</el-dialog>
</el-card>
</template>
<script>
import {getBrandList,addBrand,deleteBrandById,updateBrandById} from "../../api/item/brand";
export default {
name: "Brand",
data() {
return {
tableData: [],
total:0,
search:"",
pageNum:1,
pageSize:50,
addForm:{
name:'',
image:'',
letter:''
},
AddDialogFormVisible:false,
ImageDialogImageUrl: '',
ImageDialogVisible: false,
editForm:{
id:"",
name:'',
image:'',
letter:''
},
EditDialogFormVisible:false,
}
},
methods: {
handleSizeChange(val) {
this.pageSize=val;
this.getTableData();
},
handleCurrentChange(val) {
this.pageNum=val;
this.getTableData();
},
getTableData(){
let requestData={
search:this.search,
pageNum:this.pageNum,
pageSize:this.pageSize,
}
getBrandList(requestData).then(res=>{
console.log(res)
this.tableData=res.data.list;
this.total=res.data.total;
}).catch(res=>{
})
},
/*搜搜*/
searchName(){
this.getTableData();
},
/*清空搜索框*/
clearInput(){
this.getTableData();
},
/*打开新增对话框*/
addDialogForm(){
this.AddDialogFormVisible=true;
},
/*提交*/
submitAddForm(){
addBrand(this.addForm).then(res=>{
console.log(res)
if (res.code===200){
this.AddDialogFormVisible=false;
this.$message({
message: '保存成功',
type: 'success'
});
this.addForm={};
this.getTableData();
}else {
this.AddDialogFormVisible=false;
this.$message.error('保存失败');
}
}).catch(res=>{
})
},
/*放大图片*/
handleImageCardPreview(file){
console.log(111)
this.ImageDialogImageUrl = file.url;
this.ImageDialogVisible = true;
},
/*删除上传*/
handleImageRemove(file, fileList){
console.log(file, fileList);
},
//编辑
handleEdit(index,row){
this.EditDialogFormVisible=true;
this.editForm.id=row.id
this.editForm.name=row.name;
this.editForm.image=row.image;
this.editForm.letter=row.letter;
},
//删除
handleDelete(index,row){
console.log(index,row)
deleteBrandById({id:row.id}).then(res=>{
if (res.code===200){
this.$message({
message: '删除成功',
type: 'success'
});
this.getTableData();
}else {
this.$message.error('删除失败');
}
}).catch(res=>{
})
},
/*保存编辑*/
submitEditForm(){
updateBrandById(this.editForm).then(res=>{
if (res.code===200){
this.$message({
message: '更新成功',
type: 'success'
});
this.EditDialogFormVisible=false;
this.getTableData();
}else {
this.$message.error('跟新失败');
this.EditDialogFormVisible=false;
this.getTableData();
}
}).catch(res=>{
})
}
},
created() {
this.getTableData();
}
}
</script>
<style scoped>
/*/deep/.el-input>.el-input__inner{
width: 300px;
}*/
.input{
width: 300px;
}
</style>
后端:
package com.leyou.controller;
import com.leyou.common.PageResult;
import com.leyou.common.Result;
import com.leyou.common.ResultEnum;
import com.leyou.common.ResultUtil;
import com.leyou.entity.Brand;
import com.leyou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/brand")
public class BrandController {
@Autowired
private BrandService brandService;
/*@RequestMapping(value = "/list",method = RequestMethod.GET)
public Result getBrandList(@RequestParam(value = "search",required = false) String search, @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum ,
@RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize ){
PageResult<Brand> list=brandService.getBrandList(search,pageNum,pageSize);
return ResultUtil.success(list);
}*/
@RequestMapping(value = "/list",method = RequestMethod.POST)
public Result getBrandList(@RequestBody Map<String,String> map ){
String search = map.get("search");
String pageNum = map.get("pageNum");
String pageSize = map.get("pageSize");
if (StringUtils.isEmpty(pageNum)){
pageNum="1";
}
if (StringUtils.isEmpty(pageSize)){
pageSize="5";
}
PageResult<Brand> list=brandService.getBrandList(search,Integer.valueOf(pageNum),Integer.valueOf(pageSize));
return ResultUtil.success(list);
}
@RequestMapping(value = "",method = RequestMethod.POST)
public Result saveBrand( @RequestBody Brand brand){
int row= brandService.saveBrand(brand);
if (row!=1){
return ResultUtil.error(501,"新增失败");
}
return ResultUtil.success(ResultEnum.SUCCESS);
}
@RequestMapping(value = "{id}",method = RequestMethod.DELETE)
public Result deleteBrandById(@PathVariable Long id){
int row= brandService.deleteBrandById(id);
if (row!=1){
return ResultUtil.error(502,"删除失败");
}
return ResultUtil.success(ResultEnum.SUCCESS);
}
@RequestMapping(value = "{id}",method = RequestMethod.PUT)
public Result updateBrandById(@PathVariable Long id,@RequestBody Brand brand){
//设置修改的id
brand.setId(id);
int row= brandService.updateBrand(brand);
if (row!=1){
return ResultUtil.error(503,"更新失败");
}
return ResultUtil.success(ResultEnum.SUCCESS);
}
}
package com.leyou.service; import com.leyou.common.PageResult; import com.leyou.entity.Brand; public interface BrandService { PageResult<Brand> getBrandList(String search, Integer pageNum, Integer pageSize); int saveBrand(Brand brand); int deleteBrandById(Long id); int updateBrand(Brand brand); //List<Brand> getBrandList(Map map); }
package com.leyou.service.ServiceImpl;
import com.leyou.common.PageResult;
import com.leyou.dao.BrandDao;
import com.leyou.entity.Brand;
import com.leyou.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
public class BrandServiceImpl implements BrandService {
@Autowired
private BrandDao brandDao;
@Override
public PageResult<Brand> getBrandList(String search, Integer pageNum, Integer pageSize) {
Specification<Brand> specification=new Specification<Brand>() {
//select * from tb_brand where name like? limit 0,10;
@Override
public Predicate toPredicate(Root<Brand> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
Path<Object> name = root.get("name");
List<Predicate> predicateList = new ArrayList<>();
if (search!=null){
Predicate p1 = criteriaBuilder.like(name.as(String.class),"%"+search+"%");
predicateList.add(p1);
return criteriaQuery.where(p1).getRestriction();
}
Predicate[] pre = new Predicate[predicateList.size()];
return criteriaQuery.where(predicateList.toArray(pre)).getRestriction();
}
};
Pageable pageable=new PageRequest(pageNum-1,pageSize);
Page<Brand> page=brandDao.findAll(specification,pageable);
List<Brand> list = page.getContent();//数据
int totalPages = page.getTotalPages();//总页数
long total = page.getTotalElements();//总条数
return new PageResult<>(total,totalPages,list);
}
@Override
@Transactional
public int saveBrand( Brand brand) {
Brand save = brandDao.save(brand);
if (save==null){
return 0;
}
return 1;
}
@Override
public int deleteBrandById(Long id) {
brandDao.deleteById(id);
return 1;
}
@Override
public int updateBrand(Brand brand) {
//查询
Brand temp = brandDao.findById(brand.getId()).get();
//设置属性
if (temp!=null){
temp.setName(brand.getName());
temp.setImage(brand.getImage());
temp.setLetter(brand.getLetter());
}
//更新
Brand save = brandDao.save(temp);
if (save==null){
return 0;
}
return 1;
}
}
package com.leyou.dao;
import com.leyou.entity.Brand;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import java.util.List;
public interface BrandDao extends JpaRepository<Brand,Long>, JpaSpecificationExecutor<Brand> {
}
浙公网安备 33010602011771号