在数据库里创建好车票表,并在idea上写好车票类
package com.example.loginpj.entity;
public class chepiao {
private Integer cpid;
private String time;
private String start;
private String end;
private Integer time_cost;
private Integer carr_number;
private Integer book_number;
public void setCpid(Integer cpid) {
this.cpid = cpid;
}
public void setTime(String time) {
this.time = time;
}
public void setStart(String start) {
this.start = start;
}
public void setEnd(String end) {
this.end = end;
}
public void setTime_cost(Integer time_cost) {
this.time_cost = time_cost;
}
public void setCarr_number(Integer carr_number) {
this.carr_number = carr_number;
}
public void setBook_number(Integer book_number) {
this.book_number = book_number;
}
public Integer getCpid() {
return cpid;
}
public String getTime() {
return time;
}
public String getStart() {
return start;
}
public String getEnd() {
return end;
}
public Integer getTime_cost() {
return time_cost;
}
public Integer getCarr_number() {
return carr_number;
}
public Integer getBook_number() {
return book_number;
}
}
然后在mapper层添加接口chepiaoMapper.java和chepiaoMapper.xml
package com.example.loginpj.mapper;
import com.example.loginpj.entity.Chepiao;
import java.util.List;
public interface chepiaoMapper {
// 查询车票所有信息
List<Chepiao> selectChepiao();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.loginpj.mapper.chepiaoMapper">
<select id="selectChepiao" resultType="com.example.loginpj.entity.Chepiao">
select * from chepiao;
</select>
</mapper>
然后建立chapiaoservice.java
package com.example.loginpj.service;
import com.example.loginpj.entity.Chepiao;
import com.example.loginpj.mapper.chepiaoMapper;
import com.example.loginpj.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;
public class chepiaoService {
SqlSessionFactory factory= SqlSessionFactoryUtils.getSqlSessionFactory();
// 查询车票所有信息
public List<Chepiao> selectChepiao(){
// 获取sqlsession
SqlSession session=factory.openSession();
// 获取chepiaomapper
chepiaoMapper chepiaomapper =session.getMapper(chepiaoMapper.class);
// 调用方法,查询所有
List<Chepiao> chepiaos=chepiaomapper.selectChepiao();
session.close();
return chepiaos;
}
}
在chepiaoservlet中,
在这里插入代码片package com.example.loginpj.controller;
import com.example.loginpj.entity.Chepiao;
import com.example.loginpj.service.chepiaoService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
public class chepiaoServlet extends HttpServlet {
private chepiaoService chepiaoservice=new chepiaoService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 调用chepiaoservice完成查询
List<Chepiao> chepiaos= chepiaoService.selectChepiao();
// 存入reqest域中
req.setAttribute("chepiaos",chepiaos);
//转发到jsp
req.getRequestDispatcher("selchepiao.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
tse测试通过后我们开始写前端页面,(和用户显示啊增加啊是一样的,不多说了)
<%--
Created by IntelliJ IDEA.
User: 向阳
Date: 2022/11/16
Time: 11:20
To change this template use File | Settings | File Templates.
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>车票查询</title>
</head>
<body>
<div id="selChepiao1">
<table style="border: solid red" id="selchepiaoTable">
<tr>
<td>班次</td>
<td>发车时间</td>
<td>起点站</td>
<td>终点站</td>
<td>行车时间</td>
<td>额定载量</td>
<td>已预订人数</td>
</tr>
<%-- <tr>--%>
<%-- <td>${Chepiao.cpid}</td>--%>
<%-- <td>${Chepiao.time}</td>--%>
<%-- <td>${Chepiao.start}</td>--%>
<%-- <td>${Chepiao.end}</td>--%>
<%-- <td>${Chepiao.time_cost}</td>--%>
<%-- <td>${Chepiao.carr_number}</td>--%>
<%-- <td>${Chepiao.book_number}</td><td><a href="#">点击购票</a></td>--%>
<%-- </tr>--%>
<c:forEach items="${chepiaos}" var="Chepiao" >
<tr>
<td>${Chepiao.cpid}</td>
<td>${Chepiao.time}</td>
<td>${Chepiao.start}</td>
<td>${Chepiao.end}</td>
<td>${Chepiao.time_cost}</td>
<td>${Chepiao.carr_number}</td>
<td>${Chepiao.book_number}</td><td><a href="#">点击购票</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
结果: