前端页面:
<template>
<div>
<h1 align="center">订单列表,ts={{ts}}</h1>
<!-- 搜索框 -->
<el-form :inline="true" :model="formInline" class="demo-form-inline">
<el-form-item label="订单描述">
<el-input v-model="formInline.order_desc" placeholder="订单描述"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="query">搜索</el-button>
<el-button type="primary" @click="add">新增</el-button>
</el-form-item>
</el-form>
<el-table :data="orderList" style="width: 100%">
<el-table-column prop="id" label="订单编号" width="180" />
<el-table-column prop="orderDesc" label="订单简介" width="180" />
<el-table-column prop="amount" label="商品价格" />
<el-table-column prop="custName" label="用户名称" />
<el-table-column prop="custPhone" label="联系方式" />
<el-table-column prop="custAddr" label="收货地址" />
<el-table-column label="操作">
<template #default="scope">
<el-button size="small" @click="handleEdit(scope.row)">修改</el-button>
<el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog :title='title' :visible.sync="open">
<el-form :ref="order" :rules="rule" :inline="true" :model="order" class="demo-form-inline">
<el-form-item hidden v-if="title=='修改订单'" label="订单编号">
<el-input v-model="order.id" placeholder="订单编号"></el-input>
</el-form-item>
<el-form-item label="订单描述">
<el-input porp='orderDesc' v-model="order.orderDesc" placeholder="订单描述"></el-input>
</el-form-item>
<el-form-item label="商品价格">
<el-input v-model="order.amount" placeholder="商品价格"></el-input>
</el-form-item>
<el-form-item label="用户名称">
<el-input v-model="order.custName" placeholder="用户名称"></el-input>
</el-form-item>
<el-form-item label="联系方式">
<el-input v-model="order.custPhone" placeholder="联系方式"></el-input>
</el-form-item>
<el-form-item label="收货地址">
<el-input v-model="order.custAddr" placeholder="收货地址"></el-input>
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="open = false">取消</el-button>
<el-button type="primary" @click="onSubmit">提交</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script>
export default {
data: function() {
return {
ts: new Date().getTime(),
formInline: {
order_desc: null
},
orderList: [],
open: false,
order: {
id: null,
orderDesc: null,
amount: null,
custName: null,
custPhone: null,
custAddr: null
},
title: null,
rule: {
orderDesc: [{
required: true,
message: "请输入用户名称",
trigger: "blur"
}]
}
};
},
created() {
this.getList();
},
methods: {
getList() {
let url = this.axios.urls.ORDERS;
this.axios.get(url, {
params: {
orderDesc: this.formInline.order_desc,
}
}).then(resp => {
this.orderList = resp.data.data;
});
},
query() {
this.getList();
},
add() {
this.title = '新增订单';
this.resetOrder();
this.open = true;
},
cancel() {
this.open = false;
},
onSubmit() {
this.$refs[this.order].validate(valid => {
if (valid) {
console.log("success submit!!");
return true;
}else{
console.log("error submit!!");
return false;
}
});
let url = null;
if (this.title == '新增订单') {
url = this.axios.urls.ADD_ORDER;
} else {
url = this.axios.urls.UPDATE_ORDER;
}
this.axios.post(url, this.order).then(resp => {
if (resp.data.code == '1') {
this.$message({
message: '操作成功',
type: 'success'
});
this.getList();
this.open = false;
this.resetOrder();
} else {
this.$message({
message: '输入信息有误',
type: 'warning'
});
}
});
},
resetOrder() {
this.order.orderDesc = null,
this.order.amount = null,
this.order.custName = null,
this.order.custPhone = null,
this.order.custAddr = null
},
//修改按钮点击事件
handleEdit(row) {
this.title = '修改订单';
this.order.id = row.id;
this.order.orderDesc = row.orderDesc;
this.order.amount = row.amount;
this.order.custName = row.custName;
this.order.custPhone = row.custPhone;
this.order.custAddr = row.custAddr;
// console.log(row);
this.open = true;
},
//删除按钮
handleDelete(row) {
let url = this.axios.urls.DELETE_ORDER;
if (confirm("你确定要删除id为" + row.id + "吗")) {
this.axios.post(url, {
id: row.id
}).then(resp => {
this.$message({
message: '操作成功',
type: 'success'
});
this.getList();
});
}
}
},
}
</script>
<style>
</style>
后端controller
package com.zking.ssm.controller;
import com.zking.ssm.model.Order;
import com.zking.ssm.service.IOrderService;
import jdk.nashorn.internal.objects.annotations.Constructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author aq
* @site www.xiaomage.com
* @company xxx公司
* @create 2022-01-10 15:06
*/
@RestController("order")
public class OrderController {
@Autowired
private IOrderService orderService;
@GetMapping("/orders")
public Object getOrders(String orderDesc) {
List<Order> orders = orderService.listOrders(orderDesc);
Map<String, Object> data = new HashMap<>();
data.put("code", 1);
data.put("msg", "操作成功");
data.put("data", orders);
return data;
}
@PostMapping("/insert")
public Object insert(Order order) {
Map<String, Object> data = new HashMap<>();
try {
orderService.insert(order);
data.put("code", 1);
data.put("msg", "操作成功");
return data;
} catch (Exception e) {
e.printStackTrace();
data.put("code", -1);
data.put("msg", "操作失败");
return data;
}
}
@PostMapping("delete")
public Object delete(Integer id) {
Map<String, Object> data = new HashMap<>();
try {
int i = orderService.deleteByPrimaryKey(id);
if (i == 1) {
data.put("code", 1);
data.put("msg", "操作成功");
return data;
}else{
data.put("code", 0);
data.put("msg", "操作失败");
return data;
}
} catch (Exception e) {
e.printStackTrace();
data.put("code", -1);
data.put("msg", "操作失败");
return data;
}
}
@PostMapping("update")
public Object update(Order order){
Map<String, Object> data = new HashMap<>();
try {
int i = orderService.updateByPrimaryKeySelective(order);
if (i == 1) {
data.put("code", 1);
data.put("msg", "操作成功");
return data;
}else{
data.put("code", 0);
data.put("msg", "操作失败");
return data;
}
} catch (Exception e) {
e.printStackTrace();
data.put("code", -1);
data.put("msg", "操作失败");
return data;
}
}
}
模糊查询:
<select id="listOrders" resultType="com.zking.ssm.model.Order">
select
<include refid="Base_Column_List" />
from t_order
<where>
<if test="orderDesc != null and orderDesc != ''">
and order_desc like concat(#{orderDesc,jdbcType=VARCHAR}, '%')
</if>
</where>
</select>