员工管理系统 2.0 橙色版(java实现版,附源码)
🍅 Java学习路线:搬砖工的Java学习路线 🍅 作者:程序员小王 🍅 程序员小王的博客:https://www.wolai.com/wnaghengjie/ahNwvAUPG2Hb1Sy7Z8waaF 🍅 扫描主页左侧二维码,加我微信 一起学习、一起进步 🍅 欢迎点赞 👍 收藏 ?留言 📝
员工管理系统简介:
小型员工管理系统 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 | 备注 |
---|
1 | 7789c9a323de40908d7792be7b1dd7c6 | | 2 | 6063bb8affb24a489b475add6afef275 | | 3 | caa108a516a745998e29f30e02a73d2b | |
// 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) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入你的问题:");
String que = scanner.nextLine();
if (que.equals("退出")) {
break;
}
String ans = HttpUtil.get("http://www.tuling123.com/openapi/api?key=7789c9a323de40908d7792be7b1dd7c6&info=" + que, Charset.forName("UTF-8"));
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 {
public Admin selectByNameAndPassword(@Param("name") String name,@Param("password") String password);
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 {
public List<User> selectPage(@Param ("offset")int offset, @Param("rows") int rows);
public int selectTotalNumber();
public void insertUser(User user);
public void deleteUserById(Integer id);
public void updateById(User user);
public User selectById(Integer id);
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;
public class AdminAction extends ActionSupport {
private Admin admin;
private String password2;
private String code;
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";
}
}
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;
public class UserAction extends ActionSupport {
private Integer totalPage;
private Integer pageNumber;
private User user;
private List<User> users;
private List<Integer> ids;
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.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";
}
public String selectUserById() {
UserServiceImpl userService = new UserServiceImpl();
user = userService.selectById(user.getId());
System.out.println(user);
return "selectOK";
}
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;
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);
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();
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);
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);
if (i % 20 == 0) {
int xInt2 = random.nextInt(imgWidth);
int yInt2 = random.nextInt(imgHeight);
graphics.drawLine(xInt, yInt, xInt2, yInt2);
}
}
}
}
}
5、util层
package com.tjcu.utils;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
private static Properties p = new Properties();
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() {
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();
t.remove();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
public static void close(Connection conn) {
if (conn != null) {
try {
conn.close();
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
|