1.添加购票按钮
对应的html代码
因为列表是js函数动态填充的,故添加按钮应该在js函数中,完整代码如下:
/**
* 注意在调用该函数时必须输入参数
* 查询+ 分页
* */
function query(_pageSize,_currentPage){
// alert("------query------");
//1.获取参数
var startStation = $("#startStation").val();
var stopStation = $("#stopStation").val();
//2.发送请求
var params = {
startStation:startStation,
stopStation:stopStation,
_pageSize:_pageSize,//分页需要的数据
_currentPage:_currentPage//分页需要的数据
};
var url = 'http://localhost:8080/ticket2/data3';
jQuery.ajax({
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
url: url,
data: params,
dataType: 'json',
success: function (data) {
//取出列表
var ticketList = data.list;
//取出分页数据
var currentPage= data.currentPage;
var count= data.count;
var pageSize= data.pageSize;
var totalPage= data.totalPage;
var html='<tr>'+
'<td>编号</td>'+
'<td>开始车站</td>'+
'<td>到达车站</td>'+
'<td>余票数</td>'+
'<td>操作</td>'+ //添加购票按钮
'</tr>';
//解析数据到table表中
for (var i=0;i<ticketList.length;i++){
//取出一个对象 java里面的内省机制
var ticket = ticketList[i];
var id = ticket.id;
var startStation= ticket.startStation;
var stopStation= ticket.stopStation;
var standby = ticket.standby;
html+='<tr>'+
'<td>'+id+'</td>'+
'<td>'+startStation+'</td>'+
'<td>'+stopStation+'</td>'+
'<td>'+standby+'</td>'+
'<td><button onclick="buyTicket('+id+')">购买</button></td>'+ //添加购票按钮
'</tr>';
}
//3.填充数据
//填充列表
$("#ticketList").html(html);
//填充分页数据
$("#totalPage").html(totalPage);
$("#pageSize").html(pageSize);
$("#count").html(count);
$("#currentPage").html(currentPage);
},
error: function (data) {
alert("失败啦");
}
});
}
购买js函数
function buyTicket(id){
alert("buyTicket----"+id);
//2.发送请求
var params = {
id:id
};
var url = 'http://localhost:8080/ticket2/buyTicket';
jQuery.ajax({
type: 'POST',
contentType: 'application/x-www-form-urlencoded',
url: url,
data: params,
dataType: 'json',
success: function (data) {
alert("成功");
//刷新数据
query(5,1);
},
error: function (data) {
alert("失败啦");
}
});
}
2.控制层代码
/**
* 购票功能
* @param id
* @param req
* @return
*/
@RequestMapping("/buyTicket")
@ResponseBody //请求数据必须写这个
public Result getData3(Integer id,HttpServletRequest req){
//1.接收参数
//2.调用业务方法
HttpSession session = req.getSession();
LoginUser loginUser = (LoginUser) session.getAttribute("LOGIN_IN_SESSION");
Result<Object> objectResult = ticketService.buyTicket(id,loginUser);
//3.控制跳转
return objectResult;
}
3.业务层代码
@Override
public Result buyTicket(Integer id,LoginUser loginUser) {
Result result = new Result();
//1.修改原来的余票数
//查询出车票
Ticket ticket = ticketDao.queryById(id);
Integer standby = ticket.getStandby();
if (standby<=0){//购票失败 余票不足
result.setSuccess(false);
result.setCode("0001");//余票不足
result.setMsg("余票不足");
return result;
}
// 修改车票
ticket.setStandby(standby-1);//余票减1
ticketDao.update(ticket);
//2.创建订单 车票id 用户id 购票数 订单编号(业务编号 + 时间 + 序列号 + 用户id)
TicketOrder ticketOrder = new TicketOrder();
//车票id
ticketOrder.setTicketId(id);
//用户id session
ticketOrder.setUserId(loginUser.getId());
//订单编号
String orderNUm = UUID.randomUUID().toString();
ticketOrder.setOrderNumber(orderNUm);
//3.保存订单数据
tiketOrderDao.saveTicketOrder(ticketOrder);
return result;
}
4.dao层接口
4.1对车票的查询与修改接口
/**
* 根据id查询车票对象
* @param id
* @return
*/
Ticket queryById(Integer id); /**
* 根据id修改余票数修改余票数
* @param ticket
*/
void update(Ticket ticket);
4.2对车票的查询与修改 mapper 文件
<!-- 根据id查询-->
<select id="queryById" parameterType="int" resultType="com.day02.sation.model.Ticket">
SELECT id,start_station startStation,stop_station stopStation,standby from ticket WHERE id=#{id}
</select>
<!-- 根据id修改-->
<update id="update" parameterType="com.day02.sation.model.Ticket" >
UPDATE ticket SET standby=#{standby} WHERE id=#{id}
</update>
4.3对订单保存接口
/**
* 保存订单
* @param ticketOrder
*/
void saveTicketOrder(TicketOrder ticketOrder);
4.4对订单保存 mapper 文件
<!--保存一个订单-->
<insert id="saveTicketOrder" parameterType="com.day02.sation.model.TicketOrder">
INSERT INTO ticket_order (order_number,user_id,ticket_id) VALUES (#{orderNumber},#{userId},#{ticketId})
</insert>
5.测试各dao
/**
* 测试 根据id查询车票
*/
@Test
public void testQueryById(){
Ticket ticket = ticketDao.queryById(1);
System.out.println("ticket="+ticket);
} /**
* 测试 更新
*/
@Test
public void testUpdate(){
Ticket ticket1 = new Ticket();
ticket1.setId(1);
ticket1.setStandby(32);
ticketDao.update(ticket1);
} /**
* 测试保存订单
*/
@Test
public void testSave(){
TicketOrder ticketOrder = new TicketOrder();
ticketOrder.setOrderNumber("123456");
ticketOrder.setUserId(5);
ticketOrder.setTicketId(2);
this.ticketOrder.saveTicketOrder(ticketOrder);
}
6.新增的ticketOrder.java模型
package com.day02.sation.model; /**
* Created by Administrator on 1/2.
*/
public class TicketOrder {
private Integer id;
private String orderNumber;
private Integer userId;
private Integer ticketId; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getOrderNumber() {
return orderNumber;
} public void setOrderNumber(String orderNumber) {
this.orderNumber = orderNumber;
} public Integer getUserId() {
return userId;
} public void setUserId(Integer userId) {
this.userId = userId;
} public Integer getTicketId() {
return ticketId;
} public void setTicketId(Integer ticketId) {
this.ticketId = ticketId;
}
}
TicketOrder.java
到此购票功能完成,重启项目购票吧!