企业工单管理系统--使用mybatis

1,要求

四种框架中的一种来实现企业工单管理系统,Oracle 10g/11g 或 MySQL 5 作为后台数据
库。该系统包括查看所有的项目信息、选择项目后进行工单添加、添加完毕后显示添加后的工单
信息三大功能,具体要求如下:
打开企业工单管理系统首页,下拉框列出所有项目,如图-1 所示。

企业工单管理系统--使用mybatis_第1张图片

选择需要添加工单的项目,点击“确定”按钮,进入工单录入界面,级别下拉列表正确显示 1
级,2 级,3 级。如图-2 所示:

企业工单管理系统--使用mybatis_第2张图片

添加工单界面,需要输入执行人和任务描述,在点击“提交”时,需要执行非空验证,如果
执行人为空,提示“执行人不能为空!”,如果任务描述为空,则提示“任务描述不能为空!”,
页面效果如图-3 所示。

企业工单管理系统--使用mybatis_第3张图片

输入执行人和任务描述后点击“提交”, 注意 : 此工单需要添加到所选择的项目中 。 请在保
存记录的同时 , 自动保存记录的生成时间 。保存成功或者失败都跳转到工单信息列表,列表
下方显示“保存成功”或“保存失败”提示语句,列表按照工单编号降序排列,级别需要根据数
据库值做对应显示(1-1 级,2-2 级,3-3 级),创建时间按年-月-日显示,列表采用隔行变色样
式。如图-4 所示。

企业工单管理系统--使用mybatis_第4张图片

2,我的数据库

   企业工单管理系统--使用mybatis_第5张图片

3,使用 idea 工具和 mybatis 框架实现以上功能

     (1)我的项目最终形成的样式:

   企业工单管理系统--使用mybatis_第6张图片

(2)下面详细介绍我的项目

       entity实体类

 

Project

package cn.happy.entity;

public class Project {
    private int id;//工单编号
    private String projectName;//项目名字

    public Project() {
    }

    public int getId() {
        return id;
    }

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

    public String getProjectName() {
        return projectName;
    }

    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }
}

  workorder

package cn.happy.entity;

import java.util.Date;

public class Workorder {
    private int id;//工单编号
    private int projectId;//项目编号
    private String executor;//执行人名单
    private String discription;//任务描述
    private int orderLevel;//工单等级
    private Date createDate;//添加时间
    private Project project;

    public Workorder(int id, int projectId, String executor, String discription, int orderLevel, Date createDate, Project project) {
        this.id = id;
        this.projectId = projectId;
        this.executor = executor;
        this.discription = discription;
        this.orderLevel = orderLevel;
        this.createDate = createDate;
        this.project = project;
    }

    public int getProjectId() {
        return projectId;
    }

    public void setProjectId(int projectId) {
        this.projectId = projectId;
    }

    public Workorder() {
    }

    public Project getProject() {
        return project;
    }

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

    public int getId() {
        return id;
    }

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



    public String getExecutor() {
        return executor;
    }

    public void setExecutor(String executor) {
        this.executor = executor;
    }

    public String getDiscription() {
        return discription;
    }

    public void setDiscription(String discription) {
        this.discription = discription;
    }

    public int getOrderLevel() {
        return orderLevel;
    }

    public void setOrderLevel(int orderLevel) {
        this.orderLevel = orderLevel;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

  我的 util 层

package cn.happy.util;


import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * SqlSessionFactory的单列类
 * 工具类的目的就是创建sqlSession对象
 */
public class sessionFactory {
    //01.创建需要单例的对象实例
    private static SqlSessionFactory sessionFactory;
    //02.私有化构造
    public sessionFactory() {
    }
    /**
     * 对外提供访问的接口
     * 001.SqlSession的创建依赖SqlSessionFactory
     *     002.SqlSessionFactory依赖于SqlSessionFactoryBuilder
     *     003.SqlSessionFactoryBuilder依赖于配置文件
     *     004.获取配置文件
     */
    public static synchronized SqlSession getSqlSession(){
        try {
            InputStream stream= Resources.getResourceAsStream("mybatis-config.xml");
            //判断sqlSessionFactory是否为空
            if(sessionFactory==null){
                sessionFactory=new SqlSessionFactoryBuilder().build(stream);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }  /**
         * 之前还需要写一个finally用来关闭流!  现在不需要   为什么不需要??
         * 01.查询源码build(stream)
         * 02.SqlSessionFactoryBuilder类中已经关闭reader.close()
         * 03.所以我们如果关闭流  会报错!
         */

        return sessionFactory.openSession(true);//创建session返回
    }


}

  我的dao层

企业工单管理系统--使用mybatis_第7张图片

IProjectDAO

package cn.happy.dao;

import cn.happy.entity.Project;

import java.util.List;

public interface IProjectDAO {
    //01查询所有项目
    public List findAll();

}

 IProjectDAO。xml 文件




    

  IWorkorderDAO接口

package cn.happy.dao;

import cn.happy.entity.Workorder;

import java.util.List;

public interface IWorkorderDAO {
    //添加工单
    public  int addWork(Workorder work);
    //查询所有工单
    public  List findAllWork();


}

  IWorkorderDAO.xml 文件




    
        INSERT into workorder(projectId,executor,discription,orderLevel,createDate) VALUES (#{projectId},#{executor},#{discription},#{orderLevel},#{createDate})
    
    
    
        
        
        
        
        
        
        
    
    

  我的service层

企业工单管理系统--使用mybatis_第8张图片

IProjectService接口

package cn.happy.service;

import cn.happy.entity.Project;

import java.util.List;

public interface IProjectService {
    //01查询所有项目
    public List findAll();

}

  ProjectServiceImpl 实现类文件

package cn.happy.service.impl;

import cn.happy.dao.IProjectDAO;
import cn.happy.entity.Project;
import cn.happy.service.IProjectService;
import cn.happy.util.sessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ProjectServiceImpl implements IProjectService{
 IProjectDAO dao;
 SqlSession session;
    public ProjectServiceImpl() {
        session= sessionFactory.getSqlSession();
        dao=session.getMapper(IProjectDAO.class);
    }
    public List findAll() {

        return dao.findAll();
    }
}

  IWorkorderService接口

package cn.happy.service;

import cn.happy.entity.Workorder;

import java.util.List;

public interface IWorkorderService {
    //添加工单
    public  int addWork(Workorder work);
    //查询所有工单
    public  List findAllWork();


}

  WorkorderServiceImpl实现类文件

package cn.happy.service.impl;

import cn.happy.dao.IWorkorderDAO;
import cn.happy.entity.Workorder;
import cn.happy.service.IWorkorderService;
import cn.happy.util.sessionFactory;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class WorkorderServiceImpl implements IWorkorderService{
 IWorkorderDAO dao;
 SqlSession session;
    public WorkorderServiceImpl() {
        session= sessionFactory.getSqlSession();
        dao=session.getMapper(IWorkorderDAO.class);
    }

    public int addWork(Workorder work) {
        return dao.addWork(work);
    }

    public List findAllWork() {
        return dao.findAllWork();
    }
}

  我的servilet层

企业工单管理系统--使用mybatis_第9张图片

 

 先看看ProjectServlet层

package cn.happy.servlet;

import cn.happy.entity.Project;
import cn.happy.service.IProjectService;
import cn.happy.service.impl.ProjectServiceImpl;

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.util.List;

@WebServlet("/showAll")
public class ProjectServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        IProjectService service=new ProjectServiceImpl();
        List all= service.findAll();
        request.setAttribute("all",all);
        System.out.println("==============="+all);
        request.getRequestDispatcher("index.jsp").forward(request,response);
    }
}

  addProjectServlet

package cn.happy.servlet;

import org.apache.log4j.helpers.FileWatchdog;

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;

@WebServlet("/addProjectServlet")
public class addProjectServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String projectId = request.getParameter("projectId");
        request.setAttribute("projectId",projectId);
        request.getRequestDispatcher("add.jsp").forward(request,response);
        System.out.println("=========addproject=========");
    }

}

  在看看addWorkorderServilet

package cn.happy.servlet;

import cn.happy.entity.Workorder;
import cn.happy.service.IWorkorderService;
import cn.happy.service.impl.WorkorderServiceImpl;

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.util.Date;

@WebServlet( "/addWorkorderServlet")
public class addWorkorderServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("========开始添加工单===========");
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String executor = request.getParameter("name");
        String description = request.getParameter("description");
        String orderLevel = request.getParameter("orderLevel");
        System.out.println("========获取添加内容===========");
        Workorder work=new Workorder();
        work.setCreateDate(new Date());//日期
        work.setProjectId(Integer.parseInt(id));
        work.setExecutor(executor);
        work.setDiscription(description);
        work.setOrderLevel(Integer.parseInt(orderLevel));
        IWorkorderService service=new WorkorderServiceImpl();
        System.out.println("========加入方法中===========");
        int num=service.addWork(work);
        System.out.println("----------"+num);
        if (num>0){
            System.out.println("======if>0=======");
            String add="添加成功!";
            request.setAttribute("add",add);
        }else {
            System.out.println("======if<0=======");
            String add="添加失败!";
            request.setAttribute("add",add);
        }
        System.out.println("======结束=======");
         request.getRequestDispatcher("/findWorkorderServlet").forward(request,response);
    }
}

  最后findWorkorderServlet

package cn.happy.servlet;

import cn.happy.entity.Workorder;
import cn.happy.service.IWorkorderService;
import cn.happy.service.impl.WorkorderServiceImpl;

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.util.List;

@WebServlet("/findWorkorderServlet")
public class findWorkorderServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        IWorkorderService service=new WorkorderServiceImpl();
        List allWork = service.findAllWork();
        request.setAttribute("allWork",allWork);
        request.getRequestDispatcher("show.jsp").forward(request,response);
    }
}

  

  jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///gongdan
jdbc.username=root
jdbc.password=

log4j.properties
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=d:\\mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

### set log levels - for more verbose logging change 'info' to 'debug' ###

log4j.logger.cn.happy.dao=debug, stdout,file

  

mybatis-config.xml



    
    
        
    
    
        
            
            
                
                
                
                
            
        
    
    
        
    

 
   

  企业工单管理系统--使用mybatis_第10张图片

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>




企业工单管理系统

选择项目

  add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>






添加工单

执行人(*)   

任务描述(*)

级别(*)      

  show.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page isELIgnored="false"%>



    

企业工单列表

style="background-color:gray;">>
工单编号 醒目名称 执行人 任务描述 级别 创建时间
${item.id } ${item.project.projectName } ${item.executor} ${item.discription } ${item.orderLevel}级

  

我的pom.xml文件


    
        MyBatis01
        cn.happy
        1.0-SNAPSHOT
    
    4.0.0
    qiye
    war
    qiye Maven Webapp
    http://maven.apache.org
    
        
            junit
            junit
            3.8.1
            test
        
        
        
            org.mybatis
            mybatis
            3.4.1
        
        
        
        
        
            log4j
            log4j
            1.2.17
        
        
        
            mysql
            mysql-connector-java
            5.1.38
        
        
        
            javax
            javaee-api
            6.0
        


        
            javax.servlet
            jstl
            1.2
            runtime
        
        
        
            com.google.code.gson
            gson
            2.2.4
        
        
            junit
            junit
            RELEASE
        
    
    
        
            
                src/main/java
                
                    **/*.xml
                
            
        
    

  4,项目最终运行结果

企业工单管理系统--使用mybatis_第11张图片

企业工单管理系统--使用mybatis_第12张图片

企业工单管理系统--使用mybatis_第13张图片

 

 如果各位看代码 哪里不懂的可以给我留言!!!!!!!!!!!!!!!!!!!!!!!!

我的qq邮箱:[email protected]

 

转载于:https://www.cnblogs.com/bb1008/p/7689631.html

你可能感兴趣的:(数据库,java,开发工具)