项目实现步骤

本文详细介绍了如何从准备阶段到正式开发,一步步实现一个Java后端项目。涵盖了工具类如PageBean和Layui的使用,控制层、Service、DAO的实现,以及登录、框架、列表、增删改查等页面的构建过程。
摘要由CSDN通过智能技术生成

项目实现步骤

准备

工具类

PageBean
package org.hp.util;

import lombok.Data;

import java.util.List;

@Data
public class PageBean<T>{
   
    private int totalPage;  //总页数
    private int totalCount; //总条数
    private int page;       //当前页数
    private int limit;      //每页记录数
    private List<T> list;   //数据集

    public PageBean(int totalPage, int totalCount, int page, int limit, List<T> list) {
   
        this.totalPage = totalPage;
        this.totalCount = totalCount;
        this.page = page;
        this.limit = limit;
        this.list = list;
    }

    public PageBean() {
   
    }
}
Layui
package org.hp.util;

import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class Layui extends HashMap<String, Object> {
   
    private int code;
    private String msg;
    private int count;
    private Object data;
    public static Layui select(int count,Object data){
   
        Layui layui=new Layui();
        layui.put("code",0);
        layui.put("msg","");
        layui.put("count",count);
        layui.put("data",data);
        return layui;
    }

    public static Layui select(Object data){
   
        Layui layui=new Layui();
        layui.put("code",0);
        layui.put("msg","");
        layui.put("data",data);
        return layui;
    }

    public static Layui update(int code){
   
        Layui layui=new Layui();
        layui.put("code", code);
        layui.put("msg","");
        return layui;
    }
}

正式开发

控制层实现

package org.hp.Servlet;

import com.alibaba.fastjson.JSON;
import org.hp.service.AService;
import org.hp.service.impl.AServiceImpl;
import org.hp.util.Layui;
import org.hp.util.PageBean;

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 java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

//合同信息
@WebServlet("/ContractServlet")
public class ContractServlet extends HttpServlet {
   
    AService as=new AServiceImpl();
    Layui layui=new Layui();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        String method=request.getParameter("method");
        if(method.equals("queryAll")){
   
            queryAll(request,response);
        }else if (method.equals("queryOne")){
   
            queryOne(request,response);
        }else if(method.equals("update")){
   
            update(request,response);
        }else if(method.equals("delete")){
   
            delete(request,response);
        }else if(method.equals("insert")){
   
            insert(request,response);
        }
    }

    private void insert(HttpServletRequest request, HttpServletResponse response) {
   
        String ctime=request.getParameter("ctime");
        String ctaketime=request.getParameter("ctaketime");
        String covertime=request.getParameter("covertime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
   
            Date d1=sdf.parse(ctime);
            Date d2=sdf.parse(ctaketime);
            Date d3=sdf.parse(covertime);
            if (as.renow("insert into contract values(null,1,1,?,?,?,?,?,?,0)",d1,d2,d3,Double.parseDouble(request.getParameter("crental")),Double.parseDouble(request.getParameter("ccash")),request.getParameter("cname"))){
   
                response.getWriter().write(JSON.toJSONString(layui.update(0)));
            }else{
   
                response.getWriter().write(JSON.toJSONString(layui.update(1)));
            }
        } catch (ParseException | IOException e) {
   
            e.printStackTrace();
        }

    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
   
        if(as.renow("delete from contract where cid=?",Integer.valueOf(request.getParameter("cid")))){
   
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        }else{
   
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
   
        if (as.renow("update contract set crental=?,ccash=? where cid=?",request.getParameter("crental"),request.getParameter("ccash"),Integer.valueOf(request.getParameter("cid")))){
   
            response.getWriter().write(JSON.toJSONString(layui.update(0)));
        }else{
   
            response.getWriter().write(JSON.toJSONString(layui.update(1)));
        }
    }

    private void queryOne(HttpServletRequest request, HttpServletResponse response) throws IOException {
   
        List<Map> list=as.query("select * from contract where cid=?",Integer.valueOf(request.getParameter("cid")));
        response.getWriter().write(JSON.toJSONString(layui.select(list.size(),list)));
    }

    private void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException {
   
        Integer page=request.getParameter("page")!=null?Integer.valueOf(request.getParameter("page")):1;
        Integer limit=request.getParameter("limit")!=null?Integer.valueOf(request.getParameter("limit")):10;
        PageBean<Map> list=as.pageFind(page,limit,"select c.*,harea,lname,hunitNumber,hroomNo from contract c,lessee l,house h where c.hid=h.hid and c.lid=l.lid");
        response.getWriter().write(JSON.toJSONString(layui.select(list)));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   
        doPost(request,response);
    }
}

Service实现

package org.hp.service;

import org.hp.util.PageBean;

import java.util.List;
import java.util.Map;

public interface AService {
   
    List<Map> query(String sql,Object...objs);
    boolean renow(String sql,Object...objs);
    PageBean<Map> pageFind(int page, int limit , String sql, String...obj);
}


//Service实现的Impl
DBHelper dbHelper=new DBHelper();
@Override
    public PageBean<Map> pageFind(int page, int limit, String sql, String... obj) {
   
        return dbHelper.pageList(page,limit,sql);
    }package org.hp.service.impl;

import org.hp.service.AService;
import org.hp.util.DBHelper;
import org.hp.util.PageBean;

import java.util.List;
import java.util.Map;

public class AServiceImpl implements AService {
   
    DBHelper dbHelper=new DBHelper();
    @Override
    public List<Map> query(String sql, Object... objs) {
   
        return dbHelper.query(sql,objs);
    }

    @Override
    public boolean renow(String sql, Object... objs) {
   
        if(dbHelper.renow(sql,objs)>0)
            return true;
        else
            return false;
    }

    @Override
    public PageBean<Map> pageFind(int page, int limit, String sql, String... obj) {
   
        return dbHelper.pageList(page,limit,sql);
    }
}

dao实现(这里我封装到工具类里了)

package org.hp.util;

import java.sql.*;
import 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我不是空弦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值