企业工单管理系统--使用mybatis
1,要求
四种框架中的一种来实现企业工单管理系统,Oracle 10g/11g 或 MySQL 5 作为后台数据
库。该系统包括查看所有的项目信息、选择项目后进行工单添加、添加完毕后显示添加后的工单
信息三大功能,具体要求如下:
打开企业工单管理系统首页,下拉框列出所有项目,如图-1 所示。
选择需要添加工单的项目,点击“确定”按钮,进入工单录入界面,级别下拉列表正确显示 1
级,2 级,3 级。如图-2 所示:
添加工单界面,需要输入执行人和任务描述,在点击“提交”时,需要执行非空验证,如果
执行人为空,提示“执行人不能为空!”,如果任务描述为空,则提示“任务描述不能为空!”,
页面效果如图-3 所示。
输入执行人和任务描述后点击“提交”, 注意 : 此工单需要添加到所选择的项目中 。 请在保
存记录的同时 , 自动保存记录的生成时间 。保存成功或者失败都跳转到工单信息列表,列表
下方显示“保存成功”或“保存失败”提示语句,列表按照工单编号降序排列,级别需要根据数
据库值做对应显示(1-1 级,2-2 级,3-3 级),创建时间按年-月-日显示,列表采用隔行变色样
式。如图-4 所示。
2,我的数据库
3,使用 idea 工具和 mybatis 框架实现以上功能
(1)我的项目最终形成的样式:
(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层
IProjectDAO
package cn.happy.dao; import cn.happy.entity.Project; import java.util.List; public interface IProjectDAO { //01查询所有项目 public List<Project> findAll(); }
IProjectDAO。xml 文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.happy.dao.IProjectDAO"> <select id="findAll" resultType="project"> SELECT * from project </select> </mapper>
IWorkorderDAO接口
package cn.happy.dao; import cn.happy.entity.Workorder; import java.util.List; public interface IWorkorderDAO { //添加工单 public int addWork(Workorder work); //查询所有工单 public List<Workorder> findAllWork(); }
IWorkorderDAO.xml 文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="cn.happy.dao.IWorkorderDAO"> <insert id="addWork" parameterType="Workorder" > INSERT into workorder(projectId,executor,discription,orderLevel,createDate) VALUES (#{projectId},#{executor},#{discription},#{orderLevel},#{createDate}) </insert> <select id="findAllWork" resultMap="workMap"> SELECT id,projectId,executor,discription,orderLevel,createDate from workorder ORDER BY id DESC </select> <resultMap id="workMap" type="Workorder"> <id property="id" column="id"/> <id property="projectId" column="projectId"/> <id property="executor" column="executor"/> <id property="discription" column="discription"/> <id property="createDate" column="createDate"/> <id property="orderLevel" column="orderLevel"/> <association property="project" select="selectProject" javaType="Project" column="projectId"/> </resultMap> <select id="selectProject" resultType="Project"> select * from project where id=#{projectId} </select> </mapper>
我的service层
IProjectService接口
package cn.happy.service; import cn.happy.entity.Project; import java.util.List; public interface IProjectService { //01查询所有项目 public List<Project> 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<Project> 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<Workorder> 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<Workorder> findAllWork() { return dao.findAllWork(); } }
我的servilet层
先看看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<Project> 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<Workorder> 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
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"></properties> <typeAliases> <package name="cn.happy.entity"></package> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <mappers> <package name="cn.happy.dao"></package> </mappers> </configuration>
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+"/"; %> <!DOCTYPE html> <base href="<%=basePath%>"> <html> <body> <form action="addProjectServlet" method="post"> <div><h2 >企业工单管理系统</h2></div> 选择项目<select name="projectId" > <c:forEach var="item" items="${all}"> <option value="${item.id}">${item.projectName}</option> </c:forEach> </select> <input type="submit" value="确定"> </form> </body> </html>
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+"/"; %> <!DOCTYPE html> <html> <body> <script type="text/javascript" src="jquery-1.8.3.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#sub").click(function(){ var flag=true; var $executor=$("#executor"); var $description=$("#description"); if($executor.val()==""){ alert("执行人不能为空"); return false; } if($description.val()==""){ alert("任务描述不能为空"); return false; } if(flag){ $("#myform").submit(); } }); }); </script> <div><h2>添加工单</h2></div> <form action="/addWorkorderServlet" method="post" id="myform" name="myform"> 执行人(*) <input type="text" id="executor" name="name"><br/> <p></p> 任务描述(*)<textarea name="description" id="description"></textarea><br/> <p></p> 级别(*) <select name="orderLevel"> <option value="1">1级</option> <option value="2">2级</option> <option value="3">3级</option> </select><br/> <input type="hidden" name="id" value="${requestScope.projectId}"/> <p></p> <input id="sub" type="submit" value="提交" > </form> </body> </html>
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"%> <!DOCTYPE html> <html> <body> <div > <div > <h1>企业工单列表</h1> </div> <div> <table > <thead> <tr> <th>工单编号</th> <th>醒目名称</th> <th>执行人</th> <th>任务描述</th> <th>级别</th> <th>创建时间</th> </tr> </thead> <c:forEach items="${allWork}" var="item" varStatus="count"> <tr <c:if test="${count.index % 2 ==1 }">style="background-color:gray;"</c:if>>> <td>${item.id }</td> <td>${item.project.projectName }</td> <td>${item.executor}</td> <td >${item.discription }</td> <td > <span >${item.orderLevel}级</span> </td> <td > <fmt:formatDate value="${item.createDate}" pattern="yyyy-MM-dd" /> </td> </tr> </c:forEach> </table> <div></div> </div> </div><!--/span--> </body> </html>
我的pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <artifactId>MyBatis01</artifactId> <groupId>cn.happy</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>qiye</artifactId> <packaging>war</packaging> <name>qiye Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core --> <!--log4j--> <!-- https://mvnrepository.com/artifact/log4j/log4j --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <!--ServletAPI--> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> <scope>runtime</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> </build> </project>
4,项目最终运行结果
如果各位看代码 哪里不懂的可以给我留言!!!!!!!!!!!!!!!!!!!!!!!!
我的qq邮箱:905352007@qq.com