第一步:启动软件选择工作空间
第二步:创建项目
第三步:选择配置
第一次配Tomcat会这样
然后接着,next
这里其实也可以不用改成这样,直接next也行
最后是
然后就创建乐成了
简单写一个三层架构:
entity代码:
`
package com.zuxia.entity;
public class CakeEntity {- //甜品编号
- private int DId;
- //甜品名称
- private String DName;
- //原价
- private double OriginalPrice;
- //特点1
- private String Feature1;
- //特点2
- private String Feature2;
- //状态,1表示上架,2表示下架,3表示售罄,4表示即将售罄
- private int State;
- //图片
- private String DImg;
- //是否为新品,1为新品,0为非新品
- private int IsNew;
- public int getDId() {
- return DId;
- }
- public void setDId(int dId) {
- DId = dId;
- }
- public String getDName() {
- return DName;
- }
- public void setDName(String dName) {
- DName = dName;
- }
- public double getOriginalPrice() {
- return OriginalPrice;
- }
- public void setOriginalPrice(double originalPrice) {
- OriginalPrice = originalPrice;
- }
- public String getFeature1() {
- return Feature1;
- }
- public void setFeature1(String feature1) {
- Feature1 = feature1;
- }
- public String getFeature2() {
- return Feature2;
- }
- public void setFeature2(String feature2) {
- Feature2 = feature2;
- }
- public int getState() {
- return State;
- }
- public void setState(int state) {
- State = state;
- }
- public String getDImg() {
- return DImg;
- }
- public void setDImg(String dImg) {
- DImg = dImg;
- }
- public int getIsNew() {
- return IsNew;
- }
- public void setIsNew(int isNew) {
- IsNew = isNew;
- }
复制代码 }
service代码:
package com.zuxia.service;
import com.zuxia.entity.;
import java.util.;
public interface CakeService {- //查询所有蛋糕
- List<CakeEntity> getCakeAll(int num);
复制代码 }
serviceImpImp代码:
package com.zuxia.service.imp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zuxia.entity.CakeEntity;
import com.zuxia.service.*;
import com.zuxia.utils.DBUtils;
public class CakeServiceImp implements CakeService {- private Connection conn;
- private PreparedStatement ps;
- private ResultSet rs;
- //查询所有蛋糕
- @Override
- public List<CakeEntity> getCakeAll(int num){
-
- List<CakeEntity> list = new ArrayList<CakeEntity>();
-
- try {
-
- //获取数据库连接
- conn = DBUtils.getConn();
-
- //编写sql语句
- String sql = "SELECT * FROM DessertInfo ";
-
- //判断请求类型
- if(num == 1){
- sql = sql + "where IsNew=1";
- }
- else if(num == 2) {
- sql = sql + "ORDER BY RAND() LIMIT 6";
- }
-
- System.out.println("SQL==>"+sql);
-
- ps = conn.prepareStatement(sql);
-
- rs = ps.executeQuery();
-
- while(rs.next()) {
- CakeEntity cake = new CakeEntity();
- cake.setDId(rs.getInt("DId"));
- cake.setDImg(rs.getString("DImg"));
- cake.setDName(rs.getString("DName"));
- cake.setFeature1(rs.getString("Feature1"));
- cake.setFeature2(rs.getString("Feature2"));
- cake.setIsNew(rs.getInt("IsNew"));
- cake.setOriginalPrice(rs.getDouble("OriginalPrice"));
- cake.setState(rs.getInt("State"));
- list.add(cake);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- //关闭连接
- DBUtils.close(rs,ps,conn);
- }
-
- return list;
- }
复制代码 }
utils代码:
package com.zuxia.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DBUtils {- //获取数据库连接
- public static Connection getConn() {
- Connection conn=null;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/DessertDB?serverTimezone=UTC&characterEncoding=utf8","root","123456");
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return conn;
- }
- //关闭数据库连接
- public static void close(ResultSet rs,PreparedStatement ps,Connection conn) {
- try {
- if(rs!=null) {
- rs.close();
- }
- if(ps!=null) {
- ps.close();
- }
- if(conn!=null) {
- conn.close();
- }
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
复制代码 }
servlet代码:
package com.zuxia.servlet;
import com.zuxia.entity.;
import com.zuxia.service.;
import com.zuxia.service.imp.*;
import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/cake")
public class CakeServlet extends HttpServlet {- private static final long serialVersionUID = 1L; // 可以是任何长整型值
- //get请求
- protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- //
- request.setCharacterEncoding("utf-8");
- String op = request.getParameter("op");
-
- //System.out.println("==="+op);
-
- if(op.equals("selectbynum")) {
- int num=Integer.parseInt(request.getParameter("num"));
- this.selectCakeByNum(request,response,num);
- }
-
- }
- //post请求
- protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
- doGet(request, response);
- }
- protected void selectCakeByNum(HttpServletRequest request, HttpServletResponse response,int num) throws ServletException, IOException {
- //调用方法
- CakeService cake = new CakeServiceImp();
- List<CakeEntity> list = cake.getCakeAll(num);
-
- HttpSession session = request.getSession();
- session.setAttribute("list", list);
- request.getRequestDispatcher("index.jsp").forward(request, response);
- }
复制代码 }
`
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |