员工管理系统 2.0 橙色版(java实现版,附源码)

18 篇文章 0 订阅
11 篇文章 0 订阅

员工管理系统 2.0 橙色版(java实现版,附源码)

🍅 Java学习路线:搬砖工的Java学习路线
🍅 作者:程序员小王
🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF
🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步
🍅 欢迎点赞 👍 收藏 ⭐留言 📝

员工管理系统简介:

项目注意事项:struts2中我的action跳转到另外一个action是用的redirect,因为redirect 是重定向到一个URL,我把路径写全之后他也可以跳转action
在这里插入图片描述

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OqTcxhvs-1634643780544)(https://secure.wostatic.cn/static/sjPcGcEBsaYCtxK87nDRg9/image.png)]

小型员工管理系统 1.0 绿色版(java实现版,附源码)https://blog.csdn.net/weixin_44385486/article/details/120832999

员工管理系统 2.0 橙色版 :
整个系统其实很简单,这里主要技术:idea开发工具+JSP + Struts+Mybatis+ Mysql实现
1、管理员的登录,注册,验证码功能
2、员工的增删改查,批量删除,上传文件,下载文件 其中类的设计(实体)很有参考意义。
整个系统设计的目标人群是管理者,系统的主要功能是对员工进行各种信息的操作。
主要是完成对数据库的增删改查的功能。
3、新增功能:展示所有员工数据页面升级为分页(后端mybatis实现,前端使用bootStrap实现分页)展示所有员工,分页的页面新增图灵机器人 实现智能客服,新增跳转QQ客服功能

前提:项目新增功能:

1、智能机器人程序

# 图灵机器人的访问
1. 地址:
   http://www.tuling123.com/openapi/api
2. apikey: 用来区别使用哪个机器人
    key=apikey的值
3. 问题:
   info=要问的问题. 
总结:
   **http://www.tuling123.com/openapi/api?key=apikey的值&info=这里拼接你的问题**

序号apikey备注
17789c9a323de40908d7792be7b1dd7c6
26063bb8affb24a489b475add6afef275
3caa108a516a745998e29f30e02a73d2b
  • http工具
// 1. 定义一个问题
String que = "天津今天天气如何?";
/**
 * 2. 使用hutool的http工具,向图灵发起请求
 * 
 * key=是机器人的apikey
 * info=是向机器人发起的问题
 * 返回值是机器人回答的结果.
 */
String ans = HttpUtil.get("http://www.tuling123.com/openapi/api?key=7789c9a323de40908d7792be7b1dd7c6&info=" + que, Charset.forName("UTF-8"));

System.out.println(ans);
public class RobotTest {
    public static void main(String[] args) {
        System.out.println("--------欢迎使用智能机器人--------");
        //---循环开始
        while(true) {
            //2.提示用户输入问题
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入你的问题:");
            String que = scanner.nextLine();

            //3.如果用户输入“退出”,则退出程序
            if (que.equals("退出")) {
                break;
            }
            //4.创建用来处理问题的机器人对象
            String ans = HttpUtil.get("http://www.tuling123.com/openapi/api?key=7789c9a323de40908d7792be7b1dd7c6&info=" + que, Charset.forName("UTF-8"));
            //5.显示答案给用户
            System.out.println(ans);
        }
    }
}

一. 项目整体目录结构

MVC实现,界面层(Web)主要使用jsp来动态展现数据库中的数据,业务逻辑层(servlets)使用的servlet,数据访问层(dao)主要是连接各个Servlet与数据库之前的通信,从而实现对数据库的各种操作。其中的entity包主要是封装了两个实体:管理员和员工,方便且规范对数据的操作和代码的书写。

二. 界面效果展示:

1、管理员页面

(1)管理员登录页面

(2)管理员注册页面

2、员工页面

(1)员工页面展示
  • 页面展示完成员工的增删改查

(2)员工更新【页面数据回显】

(3)添加员工页面

(4)图灵机器人实现智能回复
在这里插入图片描述

三、数据库设计

1、admin表(管理员的账号和密码)

2、user表(员工信息表)

四、核心jar包(jar包下载参见文章末尾)

五、核心代码展示:(完整代码参见文章末尾)

1、entity类

(1)Admin.java(管理员属性的实体层,这里有管理员的登录账号和密码的定义)

public class Admin {
    private Integer id;
    private String name;
    private  String password;

(2)User.java(和Admin.java差不多主要是实体属性的get和set方法)

public class User {
    private Integer id;
    private String username;
    private String password;
    private Integer age;
    private Date birthday;


2、Dao层

(1)AdminDao接口
  • 管理员功能:登录 注册 退出
public interface AdminDao {
    /**
     * 登录
     * @param name
     * @param password
     * @return
     */
    public Admin selectByNameAndPassword(@Param("name") String name,@Param("password") String password);

    /**
     * 添加管理员
     * @param admin
     */
    public void insertAdmin(Admin admin);
}

(2)AdminDaoImpl.xml接口实现
<mapper namespace="com.tjcu.dao.AdminDao">
    <select id="selectByNameAndPassword" resultType="admin">
        select * from admin where name=#{name} and password=#{password}
    </select>

    <insert id="insertAdmin">
        insert into admin value(#{id},#{name},#{password})
    </insert>
</mapper>

(3)UserDao接口
  • 员工功能:分页展示所有 、 添加 、 删除 、 修改
public interface UserDao {
    /**
     * 分页查询:查询当前页的开始条数,及当前页数
     * offset:表示查询条目的起始下标,
     * rows:表示当前页最多显示多少条
     * @param offset 当前页的开始条数
     * @param rows  页展示的条数
     * @return 用户集合
     */
    public List<User> selectPage(@Param ("offset")int offset, @Param("rows") int rows);

    /**
     * 查询当前数据库用户数据总条数
     * @return
     */
    public int selectTotalNumber();

    /**
     * 添加用户
     * @param user
     */
    public void insertUser(User user);

    /**
     * 删除用户
     * @param id
     */
    public void deleteUserById(Integer id);

    /**
     * 更新用户
     * @param user
     */
    public void updateById(User user);

    /**
     * 根据id查询用户,用户数据回显
     * @param id
     * @return
     */
    public User selectById(Integer id);

    /**
     * 批量删除
     * @param ids
     * @return
     */
    public void batchDelete(List<Integer> ids);
}

(4)UserDaoImpl.xml接口实现
<mapper namespace="com.tjcu.dao.UserDao">
    <!--动态分片-->
    <sql id="selectUser">
        select *
        from t_user
    </sql>
    <!--    
     /**
     分页查询:
      查询当前页的开始条数,及当前页数接口
      offset:表示查询条目的起始下标,
      rows:表示最多显示多少条
      public List<User> selectPage(int beginNumber,int size);

       mysql中分页的sql语句:
       limit关键字用来限制查询结果的条目数,通常用于分页查询。
       语法:
       //sql语句中的最后一行语句
            limit offset,rows;
             offset:表示查询条目的起始下标,
             rows:表示最多显示多少条
       例:获取前10行:
       select * from employees limit 0,10;
       例:获取11行~20行
       select * from employees limit 10,10;
        -->
    <select id="selectPage" resultType="user">
        select *
        from t_user limit #{offset},#{rows}
    </select>

    <!--
     查询当前数据库用户的所有条数
        public int selectTotalPage();
    -->
    <select id="selectTotalNumber" resultType="int">
        select count(*)
        from t_user
    </select>
    <!--
    insertUser:添加用户
    User属性: id,username,password,age,birthday;
    -->
    <insert id="insertUser">
        insert into t_user
        values (#{id}, #{username}, #{password}, #{age}, #{birthday});
    </insert>
    <!--
    删除用户
    deleteUserById
    -->
    <delete id="deleteUserById">
        delete
        from t_user
        where id = #{id}
    </delete>
    <!--
     更新用户
     updateById
     Mybatis中的Sql动态语句
     User属性: id,username,password,age,birthday;
    -->
    <update id="updateById">
        update t_user
        <set>
            <if test="username">
                username=#{username},
            </if>
            <if test="password">
                password=#{password},
            </if>
            <if test="age">
                age=#{age},
            </if>
            <if test="birthday">
                birthday=#{birthday}
            </if>
        </set>
        where id=#{id}
    </update>
    <!--
       根据id查询用户,用户数据回显
        selectById
    -->
    <select id="selectById" resultType="user">
        <include refid="selectUser"/>
        where id=#{id}
    </select>
    <!--
    批量删除
    batchDelete(List<Integer> ids);
    -->
    <!--
  t_user : 表名
  id : 字段名
  collection:表示类型,这里参数是数组,就写成array,如果是集合,就写成list
  item : 是一个变量名,自己随便起名
  -->
    <delete id="batchDelete">
        delete from t_user where id
        <foreach collection="list" open="in(" item="id" separator="," close=")">
            #{id}
        </foreach>

    </delete>

</mapper>


3、service层[略,详情请参见文章末尾]

4、struts2实现Action层

(1)AdminAction:管理员登录、注册
package com.tjcu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.tjcu.entity.Admin;
import com.tjcu.service.AdminServiceImpl;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/10/17 20:37
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class AdminAction extends ActionSupport {
    private Admin admin;
    private String password2;
    private  String code;

    /**
     * 登录
     *
     * @return
     */
    public String login() {
        AdminServiceImpl adminService = new AdminServiceImpl();
        System.out.println("12323r4tfr"+admin);
        HttpSession session = ServletActionContext.getRequest().getSession();
        String code1 = (String) session.getAttribute("code1");
        System.out.println("1234"+code1);
        System.out.println("code"+code);
        if(code.equals(code1)){
            if (admin != null) {
               ServletActionContext.getRequest().getSession().setAttribute("login",admin.getName());
                return "loginOk";
            } else {
                return "loginError";
            }
        }else {
            return "loginError";
        }

    }

    /**
     * 注册
     *
     * @return
     */
    public String register() {
        AdminServiceImpl adminService = new AdminServiceImpl();
        if (admin.getPassword().equals(password2) ) {
            System.out.println("OK");
            HttpSession session = ServletActionContext.getRequest().getSession();
            String code1 = (String) session.getAttribute("code1");
            if(code.equals(code1)) {
                adminService.insertAdmin(admin);
                return "registerOk";
            }else {
                return "registerError";
            }
        } else {
            return "registerError";
        }
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public String getPassword2() {
        return password2;
    }

    public void setPassword2(String password2) {
        this.password2 = password2;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}


(2)UserDao员工:增删改查
package com.tjcu.action;


import com.opensymphony.xwork2.ActionSupport;
import com.tjcu.entity.User;
import com.tjcu.service.UserServiceImpl;

import java.util.List;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/10/13 11:11
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class UserAction extends ActionSupport {
    /**
     * 总页数
     */
    private Integer totalPage;
    /**
     * 第几页
     */
    private Integer pageNumber;
    /**
     * 实体类
     */
    private User user;
    private List<User> users;
    private List<Integer> ids;

    /**
     * 分页实现展示页面
     *
     * @return
     */
    public String page() {
        UserServiceImpl userService = new UserServiceImpl();
        //第几页获取的用户
        users = userService.selectPage(pageNumber);
        System.out.println(users);
        //总页数
        totalPage = userService.selectTotalPage();
        System.out.println(totalPage);
        return "pageOk";
    }

    /**
     * 增
     */
    public String insertUser() {
        UserServiceImpl userService = new UserServiceImpl();
        //User属性: id,username,password,age,birthday;
        user.setId(null);
        userService.insertUser(user);
        return "insertOK";
    }

    /**
     * 删
     */
    public String deleteUser() {
        UserServiceImpl userService = new UserServiceImpl();
        userService.deleteUserById(user.getId());
        return "deleteOk";
    }

    /**
     * 更新
     */
    public String updateUser() {
        UserServiceImpl userService = new UserServiceImpl();
        userService.updateById(user);
        return "updateOk";
    }

    /**
     * 通过id查询用户
     */
    public String selectUserById() {
        UserServiceImpl userService = new UserServiceImpl();
        user = userService.selectById(user.getId());
        System.out.println(user);
        return "selectOK";
    }

    /**
     * 批量删除
     * @return
     */
    public String batchDelete() {
        UserServiceImpl userService = new UserServiceImpl();
        System.out.println(ids);
        userService.batchDelete(ids);
        return "batchDeleteOk";
    }

    public Integer getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(Integer totalPage) {
        this.totalPage = totalPage;
    }

    public Integer getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(Integer pageNumber) {
        this.pageNumber = pageNumber;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
}


(3)ValidationCodeAction:验证码功能
package com.tjcu.action;
/**
 *  用于生成验证码图片  不提供返回页面
 * @author 86151
 */
public class ValidationCodeAction implements Action {
  private static final long serialVersionUID = 5126616339795936447L;

  private ConfigurableCaptchaService configurableCaptchaService = null;
  private ColorFactory colorFactory = null;
  private RandomFontFactory fontFactory = null;
  private RandomWordFactory wordFactory = null;
  private TextRenderer textRenderer = null;

  public void init() throws ServletException {
    configurableCaptchaService = new ConfigurableCaptchaService();

    // 颜色创建工厂,使用一定范围内的随机色
    colorFactory = new RandomColorFactory();
    configurableCaptchaService.setColorFactory(colorFactory);

    // 随机字体生成器
    fontFactory = new RandomFontFactory();
    fontFactory.setMaxSize(32);
    fontFactory.setMinSize(28);
    configurableCaptchaService.setFontFactory(fontFactory);

    // 随机字符生成器,去除掉容易混淆的字母和数字,如o和0等
    wordFactory = new RandomWordFactory();
    wordFactory.setCharacters("abcdefghkmnpqstwxyz23456789");
    wordFactory.setMaxLength(5);
    wordFactory.setMinLength(4);
    configurableCaptchaService.setWordFactory(wordFactory);

    // 自定义验证码图片背景
    MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();
    configurableCaptchaService.setBackgroundFactory(backgroundFactory);

    // 图片滤镜设置
    ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();

    List<BufferedImageOp> filters = new ArrayList<BufferedImageOp>();
    WobbleImageOp wobbleImageOp = new WobbleImageOp();
    wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);
    wobbleImageOp.setxAmplitude(2.0);
    wobbleImageOp.setyAmplitude(1.0);
    filters.add(wobbleImageOp);
    filterFactory.setFilters(filters);

    configurableCaptchaService.setFilterFactory(filterFactory);

    // 文字渲染器设置
    textRenderer = new BestFitTextRenderer();
    textRenderer.setBottomMargin(3);
    textRenderer.setTopMargin(3);
    configurableCaptchaService.setTextRenderer(textRenderer);

    // 验证码图片的大小
    configurableCaptchaService.setWidth(82);
    configurableCaptchaService.setHeight(32);
  }


  @Override
  public String execute() throws Exception {
    init();

    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();


    response.setContentType("image/png");
    response.setHeader("cache", "no-cache");

    HttpSession session = request.getSession(true);
    OutputStream outputStream = response.getOutputStream();

    // 得到验证码对象,有验证码图片和验证码字符串
    Captcha captcha = configurableCaptchaService.getCaptcha();
    // 取得验证码字符串放入Session
    String validationCode = captcha.getChallenge();
    session.setAttribute("code", validationCode);
    // 取得验证码图片并输出
    BufferedImage bufferedImage = captcha.getImage();
    ImageIO.write(bufferedImage, "png", outputStream);

    outputStream.flush();
    outputStream.close();

    destroy();

    return null;
  }


  public void destroy() {
    wordFactory = null;
    colorFactory = null;
    fontFactory = null;
    textRenderer = null;
    configurableCaptchaService = null;
  }

  /**
   * 自定义验证码图片背景,主要画一些噪点和干扰线
   */
  private class MyCustomBackgroundFactory implements BackgroundFactory {
    private Random random = new Random();

    @Override
    public void fillBackground(BufferedImage image) {
      Graphics graphics = image.getGraphics();

      // 验证码图片的宽高
      int imgWidth = image.getWidth();
      int imgHeight = image.getHeight();

      // 填充为白色背景
      graphics.setColor(Color.WHITE);
      graphics.fillRect(0, 0, imgWidth, imgHeight);

      // 画100个噪点(颜色及位置随机)
      for (int i = 0; i < 100; i++) {
        // 随机颜色
        int rInt = random.nextInt(255);
        int gInt = random.nextInt(255);
        int bInt = random.nextInt(255);

        graphics.setColor(new Color(rInt, gInt, bInt));

        // 随机位置
        int xInt = random.nextInt(imgWidth - 3);
        int yInt = random.nextInt(imgHeight - 2);

        // 随机旋转角度
        int sAngleInt = random.nextInt(360);
        int eAngleInt = random.nextInt(360);

        // 随机大小
        int wInt = random.nextInt(6);
        int hInt = random.nextInt(6);

        graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);

        // 画5条干扰线
        if (i % 20 == 0) {
          int xInt2 = random.nextInt(imgWidth);
          int yInt2 = random.nextInt(imgHeight);
          graphics.drawLine(xInt, yInt, xInt2, yInt2);
        }
      }
    }
  }
}

5、util层

  • 手动封装JDBCUtils工具类
package com.tjcu.utils;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * @author 王恒杰
 * @version 1.0
 * @date 2021/9/11 12:38
 * @email 1078993387@qq.com
 * @Address 天津
 * @Description:
 */
public class JdbcUtils {
    //    静态的Properties集合,相当于属性

    private static Properties p = new Properties();
    //    静态的ThreadLocal输性  线程绑定对象

    private static final ThreadLocal<Connection> t = new ThreadLocal();


    static {
        InputStream is = JdbcUtils.class.getResourceAsStream("/com/tjcu/jdbc.properties");
        try {
            p.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        //从ThreadLocal中获取Connection
        Connection conn = t.get();
        try {
            if (conn == null) {
                Class.forName(p.getProperty("driver"));
                conn = DriverManager.getConnection(p.getProperty("url"), p.getProperty("username"), p.getProperty("password"));
                t.set(conn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t.get();
    }

    public static void close(ResultSet rs, PreparedStatement pstm, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (pstm != null) {
            try {
                pstm.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
                t.remove();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }
    }
    public static void close(ResultSet rs, PreparedStatement pstm) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (pstm != null) {
            try {
                pstm.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


    }

    public static void close(PreparedStatement pstm, Connection conn) {
        if (pstm != null) {
            try {
                pstm.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
                //关闭链接后切记使用remove方法,移除ThreadLocal中已关闭的链接对象
                t.remove();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }

    }

    //关闭资源  用于关闭连接
    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
                //关闭链接后切记使用remove方法,移除ThreadLocal中已关闭的链接对象
                t.remove();
            } catch (Exception e) {
            }
        }
    }
}

6、config 配置文件:jdbc.properties

  • mysql-connector-java-8.0.16.jar之后com.mysql.cj.jdbc.Driver
driver=com.mysql.cj.jdbc.Driver
username=root
password=root
url=jdbc:mysql://localhost:3306/msc?useUnicode=true&characterEncoding=UTF-8  & useSSL=false & serverTimezone=Asia/Shanghai
源代码在githee仓库:

程序员小王Gitee: https://gitee.com/wanghengjie563135/EMS.git

  • 34
    点赞
  • 110
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员小王java

学习java的路上,加油!

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

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

打赏作者

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

抵扣说明:

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

余额充值