javaWeb用maven管理的ajax前后端分离的物业维修管理

javaWeb用maven管理的ajax前后端分离的物业维修管理,第1张

javaWeb用maven管理的ajax前后端分离的物业维修管理

javaWeb用maven管理的ajax前后端分离的物业维修管理

说明:

使用maven项目

前后端分离

使用注解版Servlet(>3版本)

1、效果展示

已维修

待维修

说明:

使用了ajax异步

使用了jQuery动态创建div

未实现翻页

2、新建maven下的javaweb项目 1、新建项目

2、maven骨架选择

3、项目名

4、maven管理(重要)

5、项目结构

6、main下新建java,resource

右键main新建文件夹,一般会自动提示这两个文件夹

新建好后分别右击这两个文件夹

java包设置被javaroot,resources设置为resource root(一般idea自动会设置好的)

3、部分源码

说明:

各层都做了简化处理

pom依赖


    
    
    
      mysql
      mysql-connector-java
      8.0.11
    
    
      javax
      javaee-api
      7.0
    
    
    
    
      javax.servlet.jsp
      jsp-api
      2.2
    
    
    
      javax.servlet
      jstl
      1.2
    
    
    
      com.alibaba
      fastjson
      1.2.14
    
    
      junit
      junit
      4.11
      test
    
  


    
      
        src/main/resources
        
          ***.xml
        
        true
      
      
        src/main/java
        
          ***.xml
        
        true
      
    

jdbc.properties

driver=com.mysql.cj.jdbc.Driver  ==这里是使用的8版本
url=jdbc:mysql://你的ip:3306/数据库名?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
user=root
password=root

pojo

package entity;

public class Wx {
    private Integer id;
    private String username;
    private String project;
    private String address;
    private String ctime;
    private String phonenum;
    private int status;
    private String wxname;
    private String wxphonenum;
    private String wxtime;
    private int wxmoney;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getProject() {
        return project;
    }

    public void setProject(String project) {
        this.project = project;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCtime() {
        return ctime;
    }

    public void setCtime(String ctime) {
        this.ctime = ctime;
    }

    public String getPhonenum() {
        return phonenum;
    }

    public void setPhonenum(String phonenum) {
        this.phonenum = phonenum;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getWxname() {
        return wxname;
    }

    public void setWxname(String wxname) {
        this.wxname = wxname;
    }

    public String getWxphonenum() {
        return wxphonenum;
    }

    public void setWxphonenum(String wxphonenum) {
        this.wxphonenum = wxphonenum;
    }

    public String getWxtime() {
        return wxtime;
    }

    public void setWxtime(String wxtime) {
        this.wxtime = wxtime;
    }

    public int getWxmoney() {
        return wxmoney;
    }

    public void setWxmoney(int wxmoney) {
        this.wxmoney = wxmoney;
    }

    public Wx(Integer id, String username, String project, String address, String ctime, String phonenum, int status, String wxname, String wxphonenum, String wxtime, int wxmoney) {
        this.id = id;
        this.username = username;
        this.project = project;
        this.address = address;
        this.ctime = ctime;
        this.phonenum = phonenum;
        this.status = status;
        this.wxname = wxname;
        this.wxphonenum = wxphonenum;
        this.wxtime = wxtime;
        this.wxmoney = wxmoney;
    }

    public Wx() {
    }
}

dao

package dao;

import entity.Wx;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;


public class WxDaoImpl {
    public static List queryWxs()
    {
        Connection conn=null;
        PreparedStatement pstmt = null;
        ResultSet rt=null;
        try
        {
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            String driver = bundle.getString("driver");
            String url = bundle.getString("url");
            String user = bundle.getString("user");
            String password = bundle.getString("password");
            Class.forName(driver);
            conn = DriverManager.getConnection(url,user,password);
            String sql="select * from wx";
            pstmt=conn.prepareStatement(sql);
            rt=pstmt.executeQuery();

            Listlst=new ArrayList();
            while(rt.next())
            {
                Wx dt=new Wx(rt.getInt("id"),rt.getString("username"),rt.getString("project"),rt.getString("address"),rt.getString("ctime"),rt.getString("phonenum"),rt.getInt("status"),rt.getString("wxname"),rt.getString("wxphonenum"),rt.getString("wxtime"),rt.getInt("wxmoney"));
                lst.add(dt);
            }
            return lst;
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            try
            {
                if(rt!=null)
                {
                    rt.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }

            try
            {
                if(pstmt!=null)
                {
                    pstmt.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            try
            {
                if(conn!=null)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static List queryWxsByStatus(int status)
    {
        Connection conn=null;
        PreparedStatement pstmt = null;
        ResultSet rt=null;
        try
        {
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            String driver = bundle.getString("driver");
            String url = bundle.getString("url");
            String user = bundle.getString("user");
            String password = bundle.getString("password");
            Class.forName(driver);
            conn = DriverManager.getConnection(url,user,password);
            String sql="select * from wx where status=?";
            pstmt=conn.prepareStatement(sql);
            pstmt.setInt(1,status);
            rt=pstmt.executeQuery();

            Listlst=new ArrayList();
            while(rt.next())
            {
                Wx dt=new Wx(rt.getInt("id"),rt.getString("username"),rt.getString("project"),rt.getString("address"),rt.getString("ctime"),rt.getString("phonenum"),rt.getInt("status"),rt.getString("wxname"),rt.getString("wxphonenum"),rt.getString("wxtime"),rt.getInt("wxmoney"));
                lst.add(dt);
            }
            return lst;
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            try
            {
                if(rt!=null)
                {
                    rt.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }

            try
            {
                if(pstmt!=null)
                {
                    pstmt.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            try
            {
                if(conn!=null)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static List queryWxsByName(String name)
    {
        Connection conn=null;
        PreparedStatement pstmt = null;
        ResultSet rt=null;
        try
        {
            ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
            String driver = bundle.getString("driver");
            String url = bundle.getString("url");
            String user = bundle.getString("user");
            String password = bundle.getString("password");
            Class.forName(driver);
            conn = DriverManager.getConnection(url,user,password);
            String sql="select * from wx where username like ?";

            pstmt=conn.prepareStatement(sql);
            pstmt.setString(1,"'%"+name+"%'");
            //System.out.println(sql);
            rt=pstmt.executeQuery();

            Listlst=new ArrayList();
            while(rt.next())
            {
                Wx dt=new Wx(rt.getInt("id"),rt.getString("username"),rt.getString("project"),rt.getString("address"),rt.getString("ctime"),rt.getString("phonenum"),rt.getInt("status"),rt.getString("wxname"),rt.getString("wxphonenum"),rt.getString("wxtime"),rt.getInt("wxmoney"));
                lst.add(dt);
            }
            return lst;
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(SQLException e)
        {
            e.printStackTrace();
            return null;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
        finally
        {
            try
            {
                if(rt!=null)
                {
                    rt.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }

            try
            {
                if(pstmt!=null)
                {
                    pstmt.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
            try
            {
                if(conn!=null)
                {
                    conn.close();
                }
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
    }

}

service

package service;

import dao.WxDaoImpl;
import entity.Wx;

import java.util.List;


public class WxServiceImpl {

    public static List selectAllWx(){
        return WxDaoImpl.queryWxs();
    }

    public static List selectAllWxByStatus(int status){
        return WxDaoImpl.queryWxsByStatus(status);
    }

    public static List selectAllWxByName(String name){
        return WxDaoImpl.queryWxsByName(name);
    }
}

servlet

package servlet;

import com.alibaba.fastjson.JSONObject;
import entity.Wx;
import service.WxServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "test",urlPatterns = "/test")
public class WxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out=response.getWriter();
        try{
            List wxes = WxServiceImpl.selectAllWx();
            out.write(JSONObject.toJSONString(wxes));
            out.flush();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            out.close();
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
4、html

核心在js中,想要源码的私信笔者吧

由于篇幅的原因,这里只展示js

说明:

在页面加载的时候发送ajax请求

根据结果动态渲染



					

保存