项目实现步骤
准备
工具类
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);
}
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