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客服功能
- 图灵机器人网址:http://www.turingapi.com/
# 图灵机器人的访问
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 |
- http工具
// 1. 定义一个问题
String que = "天津今天天气如何?";
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)员工页面展示- 页面展示完成员工的增删改查
(4)图灵机器人实现智能回复
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接口实现
(3)UserDao接口
- 员工功能:分页展示所有 、 添加 、 删除 、 修改
public interface UserDao {
public List 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 ids);
}
(4)UserDaoImpl.xml接口实现
(2)UserDao员工:增删改查select * from t_user 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; } }
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 users;
private List 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属性: 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";
}
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 getUsers() {
return users;
}
public void setUsers(List users) {
this.users = users;
}
public List getIds() {
return ids;
}
public void setIds(List 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);
// 随机字符生成器,去除掉容易混淆的字母和数字,如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 filters = new ArrayList();
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;
public class JdbcUtils {
// 静态的Properties集合,相当于属性
private static Properties p = new Properties();
// 静态的ThreadLocal输性 线程绑定对象
private static final ThreadLocal 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



