完成查询所有订单并在页面上显示
1.首先,准备一个订单数据库
2.创建一个实体类Orders 该类要与数据库对应
package com.itheima.pojo; public class Orders { private int order_id; private String customer_id; private String product_code; private String product; private int quantity; String order_date; String delivery_date; public int getOrder_id() { return order_id; } public void setOrder_id(int order_id) { this.order_id = order_id; } public String getCustomer_id() { return customer_id; } public void setCustomer_id(String customer_id) { this.customer_id = customer_id; } public String getProduct_code() { return product_code; } public void setProduct_code(String product_code) { this.product_code = product_code; } public String getProduct() { return product; } public void setProduct(String product) { this.product = product; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public String getOrder_date() { return order_date; } public void setOrder_date(String order_date) { this.order_date = order_date; } public String getDelivery_date() { return delivery_date; } public void setDelivery_date(String delivery_date) { this.delivery_date = delivery_date; } public Orders(int order_id, String customer_id, String product_code, String product, int quantity, String order_date, String delivery_date) { this.order_id = order_id; this.customer_id = customer_id; this.product_code = product_code; this.product = product; this.quantity = quantity; this.order_date = order_date; this.delivery_date = delivery_date; } public Orders() { } @Override public String toString() { return "Orders{" + "order_id=" + order_id + ", customer_id='" + customer_id + '\'' + ", product_code='" + product_code + '\'' + ", product='" + product + '\'' + ", quantity=" + quantity + ", order_date='" + order_date + '\'' + ", delivery_date='" + delivery_date + '\'' + '}'; } }
3.创建一个ordersMapper.xml 文件用来完成sql语句映射,简单的sql也可以用注释来操作
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.itheima.mapper.ordersMapper"> <select id="selectOrders" resultType="com.itheima.pojo.Orders"> select * from orders </select> </mapper>
4.创建一个接口类ordersMapper 用来声明一些数据库操作的函数
package com.itheima.mapper; import com.itheima.pojo.Orders; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import java.util.List; public interface ordersMapper { List<Orders> selectOrders(); }
5. 接着完成创建DataService 该类用来实现ordersMapper里声明的函数具体功能
package com.itheima.Service; import com.itheima.Util.MyBatisUtils; import com.itheima.mapper.userMapper; import com.itheima.mapper.ordersMapper; import com.itheima.mapper.partsMapper; import com.itheima.mapper.processesMapper; import com.itheima.pojo.Orders; import com.itheima.pojo.Parts; import com.itheima.pojo.Processes; import com.itheima.pojo.employees; import org.apache.ibatis.session.SqlSession; import java.util.List; public class DataService { SqlSession sqlSession = MyBatisUtils.getSqlSession(); /* * 获得订单信息 * */ public List<Orders> getOrders(){ //第一步 获取sqlsession对象 //第二步 执行sql ordersMapper ordersMapper =sqlSession.getMapper(ordersMapper.class); List<Orders>orders = ordersMapper.selectOrders(); System.out.println(orders); sqlSession.close(); return orders; } }
6.接着创建一个OrderServlet 用来给前端传送数据
package com.itheima.Servlet; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.itheima.Service.DataService; import com.itheima.pojo.Orders; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; import java.io.PrintWriter; import java.util.List; @WebServlet("/OrderServlet") public class OrderServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { DataService dataService = new DataService(); List<Orders> orders = dataService.getOrders(); /* * 使用FastJson将list转化成JSON * */ String jsonDate = JSON.toJSONString(orders, SerializerFeature.WriteMapNullValue); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter();//获取PrintWriter对象 out.print(jsonDate);//将jsonDate这个json数据写入到PrintWriter中 实际上数据发送到与客户端相关联的输出流。 out.flush();//方法用于强制将缓冲区中的内容发送到客户端 } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
到此,查询订单的后端代码就写完了。
剩下在前端界面写ajax接受数据并显示。
标签:product,erp,String,public,查询,订单,date,import,id From: https://www.cnblogs.com/sxwgzx23/p/17840759.html